1# ------------------------------------
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4# ------------------------------------
5
6"""
7DESCRIPTION:
8 This sample demonstrates how to use agent operations with toolset from
9 the Azure Agents service using a synchronous client.
10
11USAGE:
12 python sample_agents_run_with_toolset.py
13
14 Before running the sample:
15
16 pip install azure-ai-projects azure-ai-agents azure-identity
17
18 Set these environment variables with your own values:
19 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
20 page of your Azure AI Foundry portal.
21 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
22 the "Models + endpoints" tab in your Azure AI Foundry project.
23"""
24
25import os, sys
26from azure.ai.projects import AIProjectClient
27from azure.identity import DefaultAzureCredential
28from azure.ai.agents.models import FunctionTool, ToolSet, CodeInterpreterTool
29
30current_path = os.path.dirname(__file__)
31root_path = os.path.abspath(os.path.join(current_path, os.pardir, os.pardir))
32if root_path not in sys.path:
33 sys.path.insert(0, root_path)
34from samples.utils.user_functions import user_functions
35
36project_client = AIProjectClient(
37 endpoint=os.environ["PROJECT_ENDPOINT"],
38 credential=DefaultAzureCredential(),
39)
40
41# Create agent with toolset and process agent run
42with project_client:
43 agents_client = project_client.agents
44
45 # Initialize agent toolset with user functions and code interpreter
46 # [START create_agent_toolset]
47 functions = FunctionTool(user_functions)
48 code_interpreter = CodeInterpreterTool()
49
50 toolset = ToolSet()
51 toolset.add(functions)
52 toolset.add(code_interpreter)
53
54 # To enable tool calls executed automatically
55 agents_client.enable_auto_function_calls(toolset)
56
57 agent = agents_client.create_agent(
58 model=os.environ["MODEL_DEPLOYMENT_NAME"],
59 name="my-agent",
60 instructions="You are a helpful agent",
61 toolset=toolset,
62 )
63 # [END create_agent_toolset]
64 print(f"Created agent, ID: {agent.id}")
65
66 # Create thread for communication
67 thread = agents_client.threads.create()
68 print(f"Created thread, ID: {thread.id}")
69
70 # Create message to thread
71 message = agents_client.messages.create(
72 thread_id=thread.id,
73 role="user",
74 content="Hello, send an email with the datetime and weather information in New York?",
75 )
76 print(f"Created message, ID: {message.id}")
77
78 # Create and process agent run in thread with tools
79 # [START create_and_process_run]
80 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
81 # [END create_and_process_run]
82 print(f"Run finished with status: {run.status}")
83
84 if run.status == "failed":
85 print(f"Run failed: {run.last_error}")
86
87 # Delete the agent when done
88 agents_client.delete_agent(agent.id)
89 print("Deleted agent")
90
91 # Fetch and log all messages
92 messages = agents_client.messages.list(thread_id=thread.id)
93 for msg in messages:
94 if msg.text_messages:
95 last_text = msg.text_messages[-1]
96 print(f"{msg.role}: {last_text.text.value}")