Skip to content

Commit 6557233

Browse files
authored
Merge pull request #4920 from aahill/rest-update
Agents updates
2 parents a5b4774 + 2ed4397 commit 6557233

File tree

9 files changed

+103
-109
lines changed

9 files changed

+103
-109
lines changed

articles/ai-services/agents/how-to/tools/bing-code-samples.md

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ Create a client object, which will contain the connection string for connecting
5555
import os
5656
from azure.ai.projects import AIProjectClient
5757
from azure.identity import DefaultAzureCredential
58-
from azure.ai.projects.models import BingGroundingTool
58+
from azure.ai.agents.models import BingGroundingTool
5959

60+
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
61+
# You need to login to Azure subscription via Azure CLI and set the environment variables
62+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
6063

61-
# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
62-
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
63-
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
64-
65-
project_client = AIProjectClient.from_connection_string(
66-
credential=DefaultAzureCredential(),
67-
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
64+
# Create an AIProjectClient instance
65+
project_client = AIProjectClient(
66+
endpoint=project_endpoint,
67+
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
68+
api_version="latest",
6869
)
6970
```
7071

@@ -74,42 +75,36 @@ project_client = AIProjectClient.from_connection_string(
7475
To make the Grounding with Bing search tool available to your agent, use a connection to initialize the tool and attach it to the agent. You can find your connection in the **connected resources** section of your project in the [Azure AI Foundry portal](https://ai.azure.com/).
7576

7677
```python
77-
bing_connection = project_client.connections.get(
78-
connection_name=os.environ["BING_CONNECTION_NAME"]
79-
)
80-
conn_id = bing_connection.id
78+
conn_id = os.environ["BING_CONNECTION_NAME"] # Ensure the BING_CONNECTION_NAME environment variable is set
8179

82-
print(conn_id)
83-
84-
# Initialize agent bing tool and add the connection id
80+
# Initialize the Bing Grounding tool
8581
bing = BingGroundingTool(connection_id=conn_id)
8682

87-
# Create agent with the bing tool and process assistant run
8883
with project_client:
84+
# Create an agent with the Bing Grounding tool
8985
agent = project_client.agents.create_agent(
90-
model="gpt-4o",
91-
name="my-assistant",
92-
instructions="You are a helpful assistant",
93-
tools=bing.definitions,
94-
headers={"x-ms-enable-preview": "true"}
86+
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
87+
name="my-agent", # Name of the agent
88+
instructions="You are a helpful agent", # Instructions for the agent
89+
tools=bing.definitions, # Attach the Bing Grounding tool
9590
)
9691
print(f"Created agent, ID: {agent.id}")
9792
```
9893

9994
## Create a thread
10095

10196
```python
102-
# Create thread for communication
103-
thread = project_client.agents.create_thread()
97+
# Create a thread for communication
98+
thread = project_client.agents.threads.create()
10499
print(f"Created thread, ID: {thread.id}")
105100

106-
# Create message to thread
107-
message = project_client.agents.create_message(
101+
# Add a message to the thread
102+
message = project_client.agents.messages.create(
108103
thread_id=thread.id,
109-
role="user",
110-
content="What is the top news today",
104+
role="user", # Role of the message sender
105+
content="What is the weather in Seattle today?", # Message content
111106
)
112-
print(f"Created message, ID: {message.id}")
107+
print(f"Created message, ID: {message['id']}")
113108
```
114109

115110
## Create a run and check the output
@@ -118,26 +113,22 @@ Create a run and observe that the model uses the Grounding with Bing Search tool
118113

119114

120115
```python
121-
# Create and process agent run in thread with tools
122-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
116+
# Create and process an agent run
117+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
123118
print(f"Run finished with status: {run.status}")
124119

125-
# Retrieve run step details to get Bing Search query link
126-
# To render the webpage, we recommend you replace the endpoint of Bing search query URLs with `www.bing.com` and your Bing search query URL would look like "https://www.bing.com/search?q={search query}"
127-
run_steps = project_client.agents.list_run_steps(run_id=run.id, thread_id=thread.id)
128-
run_steps_data = run_steps['data']
129-
print(f"Last run step detail: {run_steps_data}")
130-
120+
# Check if the run failed
131121
if run.status == "failed":
132122
print(f"Run failed: {run.last_error}")
133123

134-
# Delete the assistant when done
124+
# Fetch and log all messages
125+
messages = project_client.agents.messages.list(thread_id=thread.id)
126+
for message in messages.data:
127+
print(f"Role: {message.role}, Content: {message.content}")
128+
129+
# Delete the agent when done
135130
project_client.agents.delete_agent(agent.id)
136131
print("Deleted agent")
137-
138-
# Fetch and log all messages
139-
messages = project_client.agents.list_messages(thread_id=thread.id)
140-
print(f"Messages: {messages}")
141132
```
142133

143134

articles/ai-services/agents/how-to/tools/bing-custom-search-samples.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ zone_pivot_groups: selection-bing-custom-grounding
4242
Create a client object, which will contain the connection string for connecting to your AI project and other resources.
4343

4444
```python
45-
4645
import os
4746
from azure.ai.projects import AIProjectClient
4847
from azure.ai.projects.models import MessageRole, BingCustomSearchTool
4948
from azure.identity import DefaultAzureCredential
5049

5150

52-
# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
53-
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
54-
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
51+
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
52+
# You need to login to Azure subscription via Azure CLI and set the environment variables
53+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
5554

56-
project_client = AIProjectClient.from_connection_string(
57-
credential=DefaultAzureCredential(),
58-
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
55+
# Create an AIProjectClient instance
56+
project_client = AIProjectClient(
57+
endpoint=project_endpoint,
58+
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
59+
api_version="latest",
5960
)
60-
6161
```
6262

6363

@@ -95,8 +95,8 @@ print(f"Created thread, ID: {thread.id}")
9595
# Create message to thread
9696
message = project_client.agents.create_message(
9797
thread_id=thread.id,
98-
role="user",
99-
content="What is the top news today",
98+
role=MessageRole.USER,
99+
content="How many medals did the USA win in the 2024 summer olympics?",
100100
)
101101
print(f"Created message, ID: {message.id}")
102102
```

articles/ai-services/agents/how-to/tools/code-interpreter-samples.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ The code begins by setting up the necessary imports and initializing the AI Proj
4545
```python
4646
import os
4747
from azure.ai.projects import AIProjectClient
48-
from azure.ai.projects.models import CodeInterpreterTool
49-
from azure.ai.projects.models import FilePurpose, MessageRole
5048
from azure.identity import DefaultAzureCredential
51-
from pathlib import Path
49+
from azure.ai.agents.models import CodeInterpreterTool
5250

53-
project_client = AIProjectClient.from_connection_string(
54-
credential=DefaultAzureCredential(),
55-
conn_str=os.environ["PROJECT_CONNECTION_STRING"]
51+
52+
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
53+
# You need to login to Azure subscription via Azure CLI and set the environment variables
54+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
55+
56+
# Create an AIProjectClient instance
57+
project_client = AIProjectClient(
58+
endpoint=project_endpoint,
59+
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
60+
api_version="latest",
5661
)
5762
```
5863

@@ -80,8 +85,8 @@ An agent is created with the Code Interpreter capabilities:
8085
```python
8186
agent = project_client.agents.create_agent(
8287
model=os.environ["MODEL_DEPLOYMENT_NAME"],
83-
name="my-assistant",
84-
instructions="You are helpful assistant",
88+
name="my-agent",
89+
instructions="You are helpful agent",
8590
tools=code_interpreter.definitions,
8691
tool_resources=code_interpreter.resources,
8792
)

articles/ai-services/agents/how-to/tools/overview.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ There are various ways to influence how your AI agent invokes tools:
5050

5151
## Prerequisites
5252

53-
* [A created agent](../../quickstart.md)
53+
* [A created agent](../../quickstart.md)
54+
* Make sure your AI model has enough Tokens-Per-Minute (TPM) allocated. We recommend having a minimum of 30k TPM. You can change the TPM allocation by going to **models + endpoints** in the [AI Foundry portal](https://ai.azure.com) and edit your model.
5455

5556
## Built-in tools
5657

articles/ai-services/agents/includes/quickstart-python.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,62 +51,60 @@ For example, your connection string may look something like:
5151

5252
Set this connection string as an environment variable named `PROJECT_ENDPOINT`.
5353

54-
5554
```python
5655
import os
57-
import os, time
58-
from azure.identity import DefaultAzureCredential
5956
from azure.ai.projects import AIProjectClient
60-
from pathlib import Path
57+
from azure.identity import DefaultAzureCredential
58+
from azure.ai.agents.models import CodeInterpreterTool
6159

62-
# Retrieve the project endpoint from environment variables
63-
project_endpoint = os.environ["PROJECT_ENDPOINT"]
60+
# Create an Azure AI Client from an endpoint, copied from your Azure AI Foundry project.
61+
# You need to login to Azure subscription via Azure CLI and set the environment variables
62+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
6463

65-
# Initialize the AIProjectClient
64+
# Create an AIProjectClient instance
6665
project_client = AIProjectClient(
6766
endpoint=project_endpoint,
68-
credential=DefaultAzureCredential(),
67+
credential=DefaultAzureCredential(), # Use Azure Default Credential for authentication
6968
api_version="latest",
7069
)
7170

72-
# Initialize the FunctionTool with user-defined functions
73-
functions = FunctionTool(functions=user_functions)
74-
71+
code_interpreter = CodeInterpreterTool()
7572
with project_client:
76-
# Create an agent with custom functions
73+
# Create an agent with the Bing Grounding tool
7774
agent = project_client.agents.create_agent(
78-
model=os.environ["MODEL_DEPLOYMENT_NAME"],
79-
name="my-agent",
80-
instructions="You are a helpful agent"
75+
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
76+
name="my-agent", # Name of the agent
77+
instructions="You are a helpful agent", # Instructions for the agent
78+
tools=code_interpreter.definitions, # Attach the tool
8179
)
8280
print(f"Created agent, ID: {agent.id}")
8381

8482
# Create a thread for communication
8583
thread = project_client.agents.threads.create()
8684
print(f"Created thread, ID: {thread.id}")
8785

88-
# Create a message in the thread
86+
# Add a message to the thread
8987
message = project_client.agents.messages.create(
9088
thread_id=thread.id,
9189
role="user", # Role of the message sender
92-
content="When was Microsoft founded?", # Message content
90+
content="What is the weather in Seattle today?", # Message content
9391
)
9492
print(f"Created message, ID: {message['id']}")
9593

96-
# Create and process an agent run in the thread
94+
Create and process an agent run
9795
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
9896
print(f"Run finished with status: {run.status}")
9997

10098
# Check if the run failed
10199
if run.status == "failed":
102100
print(f"Run failed: {run.last_error}")
103101

104-
# Fetch and log all messages from the thread
102+
# Fetch and log all messages
105103
messages = project_client.agents.messages.list(thread_id=thread.id)
106104
for message in messages.data:
107105
print(f"Role: {message.role}, Content: {message.content}")
108106

109-
# Delete the agent after use
107+
# Delete the agent when done
110108
project_client.agents.delete_agent(agent.id)
111109
print("Deleted agent")
112110
```
255 KB
Loading
356 KB
Loading

0 commit comments

Comments
 (0)