Skip to content

Commit 9fe57d9

Browse files
authored
Add get_thread_run span, add missing ids to end run, update tracing samples to use toolset (Azure#40998)
1 parent 8bc0fd6 commit 9fe57d9

15 files changed

+200
-303
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ agent = await agents_client.create_agent(
418418

419419
<!-- END SNIPPET -->
420420

421-
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/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_functions.py) or
421+
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/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_stream_eventhandler_with_functions.py) or
422422
[`sample_agents_functions.py`](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/ai/azure-ai-projects/samples/agents/sample_agents_functions.py)
423423

424424
### Create Agent With Azure Function Call
@@ -1169,7 +1169,7 @@ from opentelemetry import trace
11691169
from azure.monitor.opentelemetry import configure_azure_monitor
11701170

11711171
# Enable Azure Monitor tracing
1172-
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
1172+
application_insights_connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
11731173
configure_azure_monitor(connection_string=application_insights_connection_string)
11741174

11751175
# enable additional instrumentations

sdk/ai/azure-ai-agents/azure/ai/agents/telemetry/_ai_agents_instrumentor.py

Lines changed: 114 additions & 155 deletions
Large diffs are not rendered by default.

sdk/ai/azure-ai-agents/azure/ai/agents/telemetry/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class OperationName(Enum):
6161
CREATE_THREAD = "create_thread"
6262
CREATE_MESSAGE = "create_message"
6363
START_THREAD_RUN = "start_thread_run"
64+
GET_THREAD_RUN = "get_thread_run"
6465
EXECUTE_TOOL = "execute_tool"
6566
LIST_MESSAGES = "list_messages"
6667
LIST_RUN_STEPS = "list_run_steps"

sdk/ai/azure-ai-agents/samples/agents_multiagent/utils/agent_trace_configurator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def __init__(self, agents_client: AgentsClient):
1818
self.agents_client = agents_client
1919

2020
def enable_azure_monitor_tracing(self):
21-
application_insights_connection_string = os.environ.get("AI_APPINSIGHTS_CONNECTION_STRING")
21+
application_insights_connection_string = os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING")
2222
if not application_insights_connection_string:
23-
print("AI_APPINSIGHTS_CONNECTION_STRING environment variable was not set.")
24-
print("Please create AI_APPINSIGHTS_CONNECTION_STRING with the Application Insights,")
23+
print("APPLICATIONINSIGHTS_CONNECTION_STRING environment variable was not set.")
24+
print("Please create APPLICATIONINSIGHTS_CONNECTION_STRING with the Application Insights,")
2525
print("connection string. It should be enabled for this project.")
2626
print("Enable it via the 'Tracing' tab in your AI Foundry project page.")
2727
exit()

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_async_with_azure_monitor_tracing.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
messages, which may contain personal data. False by default.
2323
* AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
2424
messages, which may contain personal data. False by default.
25-
* AI_APPINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
25+
* APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2626
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically
2727
from AIProjectClient using the `telemetry.get_connection_string` method. A code sample showing how to do this
2828
can be found in the `sample_telemetry_async.py` file in the azure-ai-projects telemetry samples.
@@ -49,7 +49,7 @@ async def main() -> None:
4949
)
5050

5151
# Enable Azure Monitor tracing
52-
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
52+
application_insights_connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
5353
configure_azure_monitor(connection_string=application_insights_connection_string)
5454

5555
# enable additional instrumentations
@@ -74,16 +74,7 @@ async def main() -> None:
7474
)
7575
print(f"Created message, message ID: {message.id}")
7676

77-
run = await agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)
78-
79-
# Poll the run as long as run status is queued or in progress
80-
while run.status in ["queued", "in_progress", "requires_action"]:
81-
# Wait for a second
82-
time.sleep(1)
83-
run = await agents_client.runs.get(thread_id=thread.id, run_id=run.id)
84-
85-
print(f"Run status: {run.status}")
86-
77+
run = await agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
8778
print(f"Run completed with status: {run.status}")
8879

8980
await agents_client.delete_agent(agent.id)

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_async_with_console_tracing.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,7 @@ async def main() -> None:
7676
)
7777
print(f"Created message, message ID: {message.id}")
7878

79-
run = await agent_client.runs.create(thread_id=thread.id, agent_id=agent.id)
80-
81-
# Poll the run as long as run status is queued or in progress
82-
while run.status in ["queued", "in_progress", "requires_action"]:
83-
# Wait for a second
84-
time.sleep(1)
85-
run = await agent_client.runs.get(thread_id=thread.id, run_id=run.id)
86-
87-
print(f"Run status: {run.status}")
88-
79+
run = await agent_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
8980
print(f"Run completed with status: {run.status}")
9081

9182
await agent_client.delete_agent(agent.id)

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_azure_monitor_tracing.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
the "Models + endpoints" tab in your Azure AI Foundry project.
2323
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
2424
messages, which may contain personal data. False by default.
25-
4) AI_APPINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
25+
4) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2626
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically
2727
from AIProjectClient using the `telemetry.get_connection_string` method. A code sample showing how to do this
2828
can be found in the `sample_telemetry.py` file in the azure-ai-projects telemetry samples.
@@ -43,7 +43,7 @@
4343
from azure.monitor.opentelemetry import configure_azure_monitor
4444

4545
# Enable Azure Monitor tracing
46-
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
46+
application_insights_connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
4747
configure_azure_monitor(connection_string=application_insights_connection_string)
4848

4949
# enable additional instrumentations
@@ -70,15 +70,8 @@
7070
)
7171
print(f"Created message, message ID: {message.id}")
7272

73-
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)
74-
75-
# Poll the run as long as run status is queued or in progress
76-
while run.status in ["queued", "in_progress", "requires_action"]:
77-
# Wait for a second
78-
time.sleep(1)
79-
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
80-
81-
print(f"Run status: {run.status}")
73+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
74+
print(f"Run completed with status: {run.status}")
8275

8376
agents_client.delete_agent(agent.id)
8477
print("Deleted agent")

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_console_tracing.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
Set these environment variables with your own values:
2525
1) PROJECT_ENDPOINT - the Azure AI Agents endpoint.
26-
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
26+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2727
the "Models + endpoints" tab in your Azure AI Foundry project.
2828
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
2929
messages, which may contain personal data. False by default.
@@ -71,15 +71,8 @@
7171
message = agents_client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
7272
print(f"Created message, message ID: {message.id}")
7373

74-
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)
75-
76-
# Poll the run as long as run status is queued or in progress
77-
while run.status in ["queued", "in_progress", "requires_action"]:
78-
# Wait for a second
79-
time.sleep(1)
80-
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
81-
82-
print(f"Run status: {run.status}")
74+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
75+
print(f"Run completed with status: {run.status}")
8376

8477
agents_client.delete_agent(agent.id)
8578
print("Deleted agent")

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_basics_with_console_tracing_custom_attributes.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
Set these environment variables with your own values:
2626
1) PROJECT_ENDPOINT - the Azure AI Agents endpoint.
27-
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
27+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2828
the "Models + endpoints" tab in your Azure AI Foundry project.
2929
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
3030
messages, which may contain personal data. False by default.
@@ -99,15 +99,8 @@ def on_end(self, span: ReadableSpan):
9999
message = agents_client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
100100
print(f"Created message, message ID: {message.id}")
101101

102-
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)
103-
104-
# Poll the run as long as run status is queued or in progress
105-
while run.status in ["queued", "in_progress", "requires_action"]:
106-
# Wait for a second
107-
time.sleep(1)
108-
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
109-
110-
print(f"Run status: {run.status}")
102+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
103+
print(f"Run completed with status: {run.status}")
111104

112105
agents_client.delete_agent(agent.id)
113106
print("Deleted agent")

sdk/ai/azure-ai-agents/samples/agents_telemetry/sample_agents_stream_eventhandler_with_azure_monitor_tracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
the "Models + endpoints" tab in your Azure AI Foundry project.
2323
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
2424
messages, which may contain personal data. False by default.
25-
4) AI_APPINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
25+
4) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2626
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically
2727
from AIProjectClient using the `telemetry.get_connection_string` method. A code sample showing how to do this
2828
can be found in the `sample_telemetry.py` file in the azure-ai-projects telemetry samples.
@@ -79,7 +79,7 @@ def on_unhandled_event(self, event_type: str, event_data: Any) -> None:
7979

8080

8181
# Enable Azure Monitor tracing
82-
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
82+
application_insights_connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
8383
configure_azure_monitor(connection_string=application_insights_connection_string)
8484

8585
scenario = os.path.basename(__file__)
Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
"""
77
DESCRIPTION:
8-
This sample demonstrates how to use basic agent operations with function tools from
8+
This sample demonstrates how to use agent operations with toolset from
99
the Azure Agents service using a synchronous client with Azure Monitor tracing.
1010
View the results in the "Tracing" tab in your Azure AI Foundry project page.
1111
1212
USAGE:
13-
python sample_agents_functions_with_azure_monitor_tracing.py
13+
python sample_agents_toolset_with_azure_monitor_tracing.py
1414
1515
Before running the sample:
1616
@@ -22,7 +22,7 @@
2222
the "Models + endpoints" tab in your Azure AI Foundry project.
2323
3) AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED - Optional. Set to `true` to trace the content of chat
2424
messages, which may contain personal data. False by default.
25-
4) AI_APPINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
25+
4) APPLICATIONINSIGHTS_CONNECTION_STRING - Set to the connection string of your Application Insights resource.
2626
This is used to send telemetry data to Azure Monitor. You can also get the connection string programmatically
2727
from AIProjectClient using the `telemetry.get_connection_string` method. A code sample showing how to do this
2828
can be found in the `sample_telemetry.py` file in the azure-ai-projects telemetry samples.
@@ -34,9 +34,7 @@
3434
from azure.identity import DefaultAzureCredential
3535
from azure.ai.agents.models import (
3636
FunctionTool,
37-
RequiredFunctionToolCall,
38-
SubmitToolOutputsAction,
39-
ToolOutput,
37+
ToolSet,
4038
ListSortOrder,
4139
)
4240
from opentelemetry import trace
@@ -49,7 +47,7 @@
4947
)
5048

5149
# Enable Azure Monitor tracing
52-
application_insights_connection_string = os.environ["AI_APPINSIGHTS_CONNECTION_STRING"]
50+
application_insights_connection_string = os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
5351
configure_azure_monitor(connection_string=application_insights_connection_string)
5452

5553
# enable additional instrumentations if needed
@@ -90,6 +88,11 @@ def fetch_weather(location: str) -> str:
9088

9189
# Initialize function tool with user function
9290
functions = FunctionTool(functions=user_functions)
91+
toolset = ToolSet()
92+
toolset.add(functions)
93+
94+
# To enable tool calls executed automatically
95+
agents_client.enable_auto_function_calls(toolset)
9396

9497
with tracer.start_as_current_span(scenario):
9598
with agents_client:
@@ -98,7 +101,7 @@ def fetch_weather(location: str) -> str:
98101
model=os.environ["MODEL_DEPLOYMENT_NAME"],
99102
name="my-agent",
100103
instructions="You are a helpful agent",
101-
tools=functions.definitions,
104+
toolset=toolset
102105
)
103106
print(f"Created agent, ID: {agent.id}")
104107

@@ -112,42 +115,7 @@ def fetch_weather(location: str) -> str:
112115
)
113116
print(f"Created message, ID: {message.id}")
114117

115-
run = agents_client.runs.create(thread_id=thread.id, agent_id=agent.id)
116-
print(f"Created run, ID: {run.id}")
117-
118-
while run.status in ["queued", "in_progress", "requires_action"]:
119-
time.sleep(1)
120-
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
121-
122-
if run.status == "requires_action" and isinstance(run.required_action, SubmitToolOutputsAction):
123-
tool_calls = run.required_action.submit_tool_outputs.tool_calls
124-
if not tool_calls:
125-
print("No tool calls provided - cancelling run")
126-
agents_client.runs.cancel(thread_id=thread.id, run_id=run.id)
127-
break
128-
129-
tool_outputs = []
130-
for tool_call in tool_calls:
131-
if isinstance(tool_call, RequiredFunctionToolCall):
132-
try:
133-
output = functions.execute(tool_call)
134-
tool_outputs.append(
135-
ToolOutput(
136-
tool_call_id=tool_call.id,
137-
output=output,
138-
)
139-
)
140-
except Exception as e:
141-
print(f"Error executing tool_call {tool_call.id}: {e}")
142-
143-
print(f"Tool outputs: {tool_outputs}")
144-
if tool_outputs:
145-
agents_client.runs.submit_tool_outputs(
146-
thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs
147-
)
148-
149-
print(f"Current run status: {run.status}")
150-
118+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id, toolset=toolset)
151119
print(f"Run completed with status: {run.status}")
152120

153121
# Delete the agent when done

0 commit comments

Comments
 (0)