@@ -74,17 +74,17 @@ pip install openai azure-identity
7474
7575Entra 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
8080import os
8181from azure.ai.projects import AIProjectClient
8282from 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
9090To construct an asynchronous client, install the additional package [ aiohttp] ( https://pypi.org/project/aiohttp/ ) :
@@ -101,10 +101,10 @@ import asyncio
101101from azure.ai.projects.aio import AIProjectClient
102102from 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
191189project_client.agents.delete_version(agent_name = agent.name, agent_version = agent.version)
192190print (" 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: " )
682678index = project_client.indexes.create_or_update(
683679 name = index_name,
684680 version = index_version,
0 commit comments