Skip to content

Commit 8e49d4a

Browse files
authored
combine context manager (#44017)
* async with * update * update * update * update * update readme * Resolved comments * better description for image gen
1 parent a22b06f commit 8e49d4a

File tree

97 files changed

+5517
-5692
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+5517
-5692
lines changed

sdk/ai/azure-ai-projects/README.md

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ pip install openai azure-identity
7474

7575
Entra ID is the only authentication method supported at the moment by the client.
7676

77-
To construct a synchronous client:
77+
To construct a synchronous client as a context manager:
7878

7979
```python
8080
import os
8181
from azure.ai.projects import AIProjectClient
8282
from azure.identity import DefaultAzureCredential
8383

84-
project_client = AIProjectClient(
85-
credential=DefaultAzureCredential(),
86-
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
87-
)
84+
with (
85+
DefaultAzureCredential() as credential,
86+
AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as project_client,
87+
):
8888
```
8989

9090
To construct an asynchronous client, install the additional package [aiohttp](https://pypi.org/project/aiohttp/):
@@ -101,10 +101,10 @@ import asyncio
101101
from azure.ai.projects.aio import AIProjectClient
102102
from azure.identity.aio import DefaultAzureCredential
103103

104-
project_client = AIProjectClient(
105-
credential=DefaultAzureCredential(),
106-
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
107-
)
104+
async with (
105+
DefaultAzureCredential() as credential,
106+
AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as project_client,
107+
):
108108
```
109109

110110
## Examples
@@ -120,20 +120,19 @@ See the "responses" folder in the [package samples][samples] for additional samp
120120
<!-- SNIPPET:sample_responses_basic.responses -->
121121

122122
```python
123-
openai_client = project_client.get_openai_client()
124-
125-
response = openai_client.responses.create(
126-
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
127-
input="What is the size of France in square miles?",
128-
)
129-
print(f"Response output: {response.output_text}")
123+
with project_client.get_openai_client() as openai_client:
124+
response = openai_client.responses.create(
125+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
126+
input="What is the size of France in square miles?",
127+
)
128+
print(f"Response output: {response.output_text}")
130129

131-
response = openai_client.responses.create(
132-
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
133-
input="And what is the capital city?",
134-
previous_response_id=response.id,
135-
)
136-
print(f"Response output: {response.output_text}")
130+
response = openai_client.responses.create(
131+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
132+
input="And what is the capital city?",
133+
previous_response_id=response.id,
134+
)
135+
print(f"Response output: {response.output_text}")
137136
```
138137

139138
<!-- END SNIPPET -->
@@ -149,44 +148,43 @@ See the "agents" folder in the [package samples][samples] for an extensive set o
149148
<!-- SNIPPET:sample_agent_basic.prompt_agent_basic -->
150149

151150
```python
152-
openai_client = project_client.get_openai_client()
153-
154-
agent = project_client.agents.create_version(
155-
agent_name="MyAgent",
156-
definition=PromptAgentDefinition(
157-
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
158-
instructions="You are a helpful assistant that answers general questions",
159-
),
160-
)
161-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
151+
with project_client.get_openai_client() as openai_client:
152+
agent = project_client.agents.create_version(
153+
agent_name="MyAgent",
154+
definition=PromptAgentDefinition(
155+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
156+
instructions="You are a helpful assistant that answers general questions",
157+
),
158+
)
159+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
162160

163-
conversation = openai_client.conversations.create(
164-
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
165-
)
166-
print(f"Created conversation with initial user message (id: {conversation.id})")
161+
conversation = openai_client.conversations.create(
162+
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
163+
)
164+
print(f"Created conversation with initial user message (id: {conversation.id})")
167165

168-
response = openai_client.responses.create(
169-
conversation=conversation.id,
170-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
171-
input="",
172-
)
173-
print(f"Response output: {response.output_text}")
166+
response = openai_client.responses.create(
167+
conversation=conversation.id,
168+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
169+
input="",
170+
)
171+
print(f"Response output: {response.output_text}")
174172

175-
openai_client.conversations.items.create(
176-
conversation_id=conversation.id,
177-
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
178-
)
179-
print(f"Added a second user message to the conversation")
173+
openai_client.conversations.items.create(
174+
conversation_id=conversation.id,
175+
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
176+
)
177+
print(f"Added a second user message to the conversation")
180178

181-
response = openai_client.responses.create(
182-
conversation=conversation.id,
183-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
184-
input="",
185-
)
186-
print(f"Response output: {response.output_text}")
179+
response = openai_client.responses.create(
180+
conversation=conversation.id,
181+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
182+
input="",
183+
)
184+
print(f"Response output: {response.output_text}")
187185

188-
openai_client.conversations.delete(conversation_id=conversation.id)
189-
print("Conversation deleted")
186+
openai_client.conversations.delete(conversation_id=conversation.id)
187+
print("Conversation deleted")
190188

191189
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
192190
print("Agent deleted")
@@ -676,9 +674,7 @@ folder in the [package samples][samples].
676674
<!-- SNIPPET:sample_indexes.indexes_sample-->
677675

678676
```python
679-
print(
680-
f"Create Index `{index_name}` with version `{index_version}`, referencing an existing AI Search resource:"
681-
)
677+
print(f"Create Index `{index_name}` with version `{index_version}`, referencing an existing AI Search resource:")
682678
index = project_client.indexes.create_or_update(
683679
name=index_name,
684680
version=index_version,

sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,52 +34,51 @@
3434

3535
load_dotenv()
3636

37-
project_client = AIProjectClient(
38-
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
39-
credential=DefaultAzureCredential(),
40-
)
37+
endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
4138

42-
with project_client:
39+
with (
40+
DefaultAzureCredential() as credential,
41+
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
42+
):
4343

4444
# [START prompt_agent_basic]
45-
openai_client = project_client.get_openai_client()
46-
47-
agent = project_client.agents.create_version(
48-
agent_name="MyAgent",
49-
definition=PromptAgentDefinition(
50-
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
51-
instructions="You are a helpful assistant that answers general questions",
52-
),
53-
)
54-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
55-
56-
conversation = openai_client.conversations.create(
57-
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
58-
)
59-
print(f"Created conversation with initial user message (id: {conversation.id})")
60-
61-
response = openai_client.responses.create(
62-
conversation=conversation.id,
63-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
64-
input="",
65-
)
66-
print(f"Response output: {response.output_text}")
67-
68-
openai_client.conversations.items.create(
69-
conversation_id=conversation.id,
70-
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
71-
)
72-
print(f"Added a second user message to the conversation")
73-
74-
response = openai_client.responses.create(
75-
conversation=conversation.id,
76-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
77-
input="",
78-
)
79-
print(f"Response output: {response.output_text}")
80-
81-
openai_client.conversations.delete(conversation_id=conversation.id)
82-
print("Conversation deleted")
45+
with project_client.get_openai_client() as openai_client:
46+
agent = project_client.agents.create_version(
47+
agent_name="MyAgent",
48+
definition=PromptAgentDefinition(
49+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
50+
instructions="You are a helpful assistant that answers general questions",
51+
),
52+
)
53+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
54+
55+
conversation = openai_client.conversations.create(
56+
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
57+
)
58+
print(f"Created conversation with initial user message (id: {conversation.id})")
59+
60+
response = openai_client.responses.create(
61+
conversation=conversation.id,
62+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
63+
input="",
64+
)
65+
print(f"Response output: {response.output_text}")
66+
67+
openai_client.conversations.items.create(
68+
conversation_id=conversation.id,
69+
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
70+
)
71+
print(f"Added a second user message to the conversation")
72+
73+
response = openai_client.responses.create(
74+
conversation=conversation.id,
75+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
76+
input="",
77+
)
78+
print(f"Response output: {response.output_text}")
79+
80+
openai_client.conversations.delete(conversation_id=conversation.id)
81+
print("Conversation deleted")
8382

8483
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
8584
print("Agent deleted")

sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic_async.py

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,58 +35,55 @@
3535

3636
load_dotenv()
3737

38+
endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
3839

39-
async def main() -> None:
40-
41-
credential = DefaultAzureCredential()
42-
43-
async with credential:
44-
45-
project_client = AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential)
46-
47-
async with project_client:
48-
49-
openai_client = project_client.get_openai_client()
50-
51-
agent = await project_client.agents.create_version(
52-
agent_name="MyAgent",
53-
definition=PromptAgentDefinition(
54-
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
55-
instructions="You are a helpful assistant that answers general questions.",
56-
),
57-
)
58-
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.version})")
5940

60-
conversation = await openai_client.conversations.create(
61-
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
62-
)
63-
print(f"Created conversation with initial user message (id: {conversation.id})")
64-
65-
response = await openai_client.responses.create(
66-
conversation=conversation.id,
67-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
68-
input="",
69-
)
70-
print(f"Response output: {response.output_text}")
71-
72-
await openai_client.conversations.items.create(
73-
conversation_id=conversation.id,
74-
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
75-
)
76-
print(f"Added a second user message to the conversation")
77-
78-
response = await openai_client.responses.create(
79-
conversation=conversation.id,
80-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
81-
input="",
82-
)
83-
print(f"Response output: {response.output_text}")
84-
85-
await openai_client.conversations.delete(conversation_id=conversation.id)
86-
print("Conversation deleted")
87-
88-
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
89-
print("Agent deleted")
41+
async def main() -> None:
42+
async with (
43+
DefaultAzureCredential() as credential,
44+
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
45+
project_client.get_openai_client() as openai_client,
46+
):
47+
48+
agent = await project_client.agents.create_version(
49+
agent_name="MyAgent",
50+
definition=PromptAgentDefinition(
51+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
52+
instructions="You are a helpful assistant that answers general questions.",
53+
),
54+
)
55+
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.version})")
56+
57+
conversation = await openai_client.conversations.create(
58+
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
59+
)
60+
print(f"Created conversation with initial user message (id: {conversation.id})")
61+
62+
response = await openai_client.responses.create(
63+
conversation=conversation.id,
64+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
65+
input="",
66+
)
67+
print(f"Response output: {response.output_text}")
68+
69+
await openai_client.conversations.items.create(
70+
conversation_id=conversation.id,
71+
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
72+
)
73+
print(f"Added a second user message to the conversation")
74+
75+
response = await openai_client.responses.create(
76+
conversation=conversation.id,
77+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
78+
input="",
79+
)
80+
print(f"Response output: {response.output_text}")
81+
82+
await openai_client.conversations.delete(conversation_id=conversation.id)
83+
print("Conversation deleted")
84+
85+
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
86+
print("Agent deleted")
9087

9188

9289
if __name__ == "__main__":

sdk/ai/azure-ai-projects/samples/agents/sample_agent_retrieve_basic.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
3737
agent_name = os.environ["AGENT_NAME"]
3838
conversation_id = os.environ["CONVERSATION_ID"]
3939

40-
project_client = AIProjectClient(
41-
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
42-
credential=DefaultAzureCredential(),
43-
)
40+
endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
4441

45-
with project_client:
46-
47-
openai_client = project_client.get_openai_client()
42+
with (
43+
DefaultAzureCredential() as credential,
44+
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
45+
project_client.get_openai_client() as openai_client,
46+
):
4847

4948
# Retrieves latest version of an existing Agent
5049
agent = project_client.agents.get(agent_name=agent_name)

0 commit comments

Comments
 (0)