You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use this SDK, start by creating an `AIProjectClient`. For more information on `azure-ai-projects`, refer to its [README](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-projects/README.md).
95
+
96
+
Here is an example of creating a synchronous `AIProjectClient`:
94
97
95
98
```python
96
99
import os
97
-
from azure.ai.agentsimportAgentsClient
100
+
from azure.ai.projectsimportAIProjectClient
98
101
from azure.identity import DefaultAzureCredential
99
102
100
-
agents_client=AgentsClient(
103
+
project_client=AIProjectClient(
101
104
endpoint=os.environ["PROJECT_ENDPOINT"],
102
105
credential=DefaultAzureCredential(),
103
106
)
104
107
```
105
108
106
-
To construct an asynchronous client, Install the additional package[aiohttp](https://pypi.org/project/aiohttp/):
109
+
To construct an asynchronous client, install the `aiohttp` package:
107
110
108
111
```bash
109
112
pip install aiohttp
110
113
```
111
114
112
-
and update the code above to import `asyncio`, and import `AgentsClient` from the `azure.ai.agents.aio` namespace:
115
+
Then use the code below with `AIProjectClient` and `DefaultAzureCredential` in `aio` packages:
116
+
117
+
```python
118
+
import asyncio
119
+
import os
120
+
from azure.ai.projects.aio import AIProjectClient
121
+
from azure.identity.aio import DefaultAzureCredential
122
+
123
+
asyncdefmain() -> None:
124
+
project_client = AIProjectClient(
125
+
endpoint=os.environ["PROJECT_ENDPOINT"],
126
+
credential=DefaultAzureCredential(),
127
+
)
128
+
129
+
if__name__=="__main__":
130
+
asyncio.run(main())
131
+
```
132
+
133
+
Once you have an `AIProjectClient`, you can obtain an `AgentsClient` like this:
134
+
135
+
**Synchronous Client:**
136
+
```python
137
+
with project_client:
138
+
agents_client = project_client.agents
139
+
```
140
+
141
+
**Asynchronous Client:**
142
+
```python
143
+
asyncwith project_client:
144
+
agents_client = project_client.agents
145
+
```
146
+
147
+
Alternatively, you can instantiate an AgentsClient directly as a standalone approach without using `azure-ai-projects`. However, this is not recommended, as it has limitations and lacks the integrated capabilities provided by using an `AIProjectClient`. Here is is the example:
113
148
149
+
**Synchronous Client:**
114
150
```python
115
151
import os
152
+
from azure.ai.agents import AgentsClient
153
+
from azure.identity import DefaultAzureCredential
154
+
155
+
agents_client = AgentsClient(
156
+
endpoint=os.environ["PROJECT_ENDPOINT"],
157
+
credential=DefaultAzureCredential()
158
+
)
159
+
160
+
with agents_client:
161
+
# your code to consume the client
162
+
pass
163
+
164
+
```
165
+
166
+
**Asynchronous Client:**
167
+
```python
116
168
import asyncio
169
+
import os
117
170
from azure.ai.agents.aio import AgentsClient
118
-
from azure.core.credentialsimportAzureKeyCredential
171
+
from azure.identity.aioimportDefaultAzureCredential
119
172
120
-
agent_client = AgentsClient(
121
-
endpoint=os.environ["PROJECT_ENDPOINT"],
122
-
credential=DefaultAzureCredential(),
123
-
)
173
+
asyncdefmain() -> None:
174
+
agents_client = AgentsClient(
175
+
endpoint=os.environ["PROJECT_ENDPOINT"],
176
+
credential=DefaultAzureCredential()
177
+
)
178
+
asyncwith agents_client:
179
+
# your code to consume the client
180
+
pass
181
+
182
+
if__name__=="__main__":
183
+
asyncio.run(main())
124
184
```
125
185
126
186
## Examples
@@ -320,6 +380,47 @@ with project_client:
320
380
321
381
<!-- END SNIPPET -->
322
382
383
+
### Create Agent with Deep Research
384
+
385
+
To enable your Agent to do a detailed research of a topic, use the `DeepResearchTool` along with a connection to a Bing Grounding resource.
386
+
This scenarios requires you to specify two model deployments. One is the generic chat model that does arbitration, and is
387
+
specified as usual when you call the `create_agent` method. The other is the Deep Research model, which is specified
# Create Agent with the Deep Research tool and process Agent run
404
+
with project_client:
405
+
406
+
with project_client.agents as agents_client:
407
+
408
+
# Create a new agent that has the Deep Research tool attached.
409
+
#NOTE: To add Deep Research to an existing agent, fetch it with `get_agent(agent_id)` and then,
410
+
# update the agent with the Deep Research tool.
411
+
agent = agents_client.create_agent(
412
+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
413
+
name="my-agent",
414
+
instructions="You are a helpful Agent that assists in researching scientific topics.",
415
+
tools=deep_research_tool.definitions,
416
+
)
417
+
```
418
+
419
+
<!-- END SNIPPET -->
420
+
421
+
> **Limitation**: The Deep Research tool is currently recommended **only** in non-streaming scenarios.
422
+
> Using it with streaming can work, but it may occasionally time-out and is therefore not yet recommended.
423
+
323
424
### Create Agent with Azure AI Search
324
425
325
426
Azure AI Search is an enterprise search system for high-performance applications. It integrates with Azure OpenAI Service and Azure Machine Learning, offering advanced search technologies like vector search and full-text search. Ideal for knowledge base insights, information discovery, and automation. Creating an Agent with Azure AI Search requires an existing Azure AI Search Index. For more information and setup guides, see [Azure AI Search Tool Guide](https://learn.microsoft.com/azure/ai-services/agents/how-to/tools/azure-ai-search?tabs=azurecli%2Cpython&pivots=overview-azure-ai-search).
@@ -329,22 +430,24 @@ Here is an example to integrate Azure AI Search:
# Initialize agent AI search tool and add the search index connection id
442
+
ai_search = AzureAISearchTool(
443
+
index_connection_id=conn_id,
444
+
index_name="sample_index",
445
+
query_type=AzureAISearchQueryType.SIMPLE,
446
+
top_k=3,
447
+
filter="",
448
+
)
346
449
347
-
withproject_client:
450
+
# Create agent with AI search tool and process agent run
348
451
agents_client = project_client.agents
349
452
350
453
agent = agents_client.create_agent(
@@ -390,9 +493,9 @@ for message in messages:
390
493
391
494
You can enhance your Agents by defining callback functions as function tools. These can be provided to `create_agent` via either the `toolset` parameter or the combination of `tools` and `tool_resources`. Here are the distinctions:
392
495
393
-
For more details about requirements and specification of functions, refer to [Function Tool Specifications](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/FunctionTool.md)
496
+
For more details about requirements and specification of functions, refer to [Function Tool Specifications](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/FunctionTool.md)
394
497
395
-
Here is an example to use [user functions](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/utils/user_functions.py) in `toolset`:
498
+
Here is an example to use [user functions](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/utils/user_functions.py) in `toolset`:
For asynchronous functions, you must import `AgentsClient` from `azure.ai.agents.aio` and use `AsyncFunctionTool`. Here is an example using [asynchronous user functions](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_functions_async.py):
517
+
For asynchronous functions, you must import `AgentsClient` from `azure.ai.agents.aio` and use `AsyncFunctionTool`. Here is an example using [asynchronous user functions](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_functions_async.py):
Notice that if `enable_auto_function_calls` is called, the SDK will invoke the functions automatically during `create_and_process` or streaming. If you prefer to execute them manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
Notice that if `enable_auto_function_calls` is called, the SDK will invoke the functions automatically during `create_and_process` or streaming. If you prefer to execute them manually, refer to [`sample_agents_stream_eventhandler_with_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or
To process your message, you can use `runs.create`, `runs.create_and_process`, or `runs.stream`.
929
1032
930
-
`runs.create` requests the Agent to process the message without polling for the result. If you are using `function tools`, your code is responsible for polling for the result and acknowledging the status of `Run`. When the status is `requires_action`, your code is responsible for calling the function tools. For a code sample, visit [`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py).
1033
+
`runs.create` requests the Agent to process the message without polling for the result. If you are using `function tools`, your code is responsible for polling for the result and acknowledging the status of `Run`. When the status is `requires_action`, your code is responsible for calling the function tools. For a code sample, visit [`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_tools/sample_agents_functions.py).
931
1034
932
1035
Here is an example of `runs.create` and poll until the run is completed:
933
1036
@@ -967,7 +1070,7 @@ run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.
967
1070
968
1071
<!-- END SNIPPET -->
969
1072
970
-
With streaming, polling need not be considered. If `function tools` were added to the agents, you should decide to have the function tools called manually or automatically. Please visit [`manual function call sample`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or [`automatic function call sample`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_iteration_with_toolset.py).
1073
+
With streaming, polling need not be considered. If `function tools` were added to the agents, you should decide to have the function tools called manually or automatically. Please visit [`manual function call sample`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_eventhandler_with_functions.py) or [`automatic function call sample`](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_iteration_with_toolset.py).
971
1074
972
1075
Here is a basic example of streaming:
973
1076
@@ -1049,7 +1152,7 @@ with agents_client.runs.stream(thread_id=thread.id, agent_id=agent.id, event_han
1049
1152
1050
1153
<!-- END SNIPPET -->
1051
1154
1052
-
As you can see, this SDK parses the events and produces various event types similar to OpenAI agents. In your use case, you might not be interested in handling all these types and may decide to parse the events on your own. To achieve this, please refer to [override base event handler](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_with_base_override_eventhandler.py).
1155
+
As you can see, this SDK parses the events and produces various event types similar to OpenAI agents. In your use case, you might not be interested in handling all these types and may decide to parse the events on your own. To achieve this, please refer to [override base event handler](https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples/agents_streaming/sample_agents_stream_with_base_override_eventhandler.py).
1053
1156
1054
1157
```
1055
1158
Note: Multiple streaming processes may be chained behind the scenes.
@@ -1280,7 +1383,7 @@ To report an issue with the client library, or request additional features, plea
1280
1383
1281
1384
## Next steps
1282
1385
1283
-
Have a look at the [Samples](https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-agents_1.1.0b2/sdk/ai/azure-ai-agents/samples) folder, containing fully runnable Python code for synchronous and asynchronous clients.
1386
+
Have a look at the [Samples](https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-agents_1.1.0b3/sdk/ai/azure-ai-agents/samples) folder, containing fully runnable Python code for synchronous and asynchronous clients.
1284
1387
1285
1388
Explore the [AI Starter Template](https://aka.ms/azsdk/azure-ai-agents/python/ai-starter-template). This template creates an Azure AI Foundry hub, project and connected resources including Azure OpenAI Service, AI Search and more. It also deploys a simple chat application to Azure Container Apps.
1286
1389
@@ -1305,9 +1408,9 @@ additional questions or comments.
0 commit comments