1# pylint: disable=line-too-long,useless-suppression
2# ------------------------------------
3# Copyright (c) Microsoft Corporation.
4# Licensed under the MIT License.
5# ------------------------------------
6
7"""
8DESCRIPTION:
9 This sample demonstrates how to enable automatic function calls by calling `enable_auto_function_calls`
10 using a synchronous client.
11
12USAGE:
13 python sample_agents_auto_function_call.py
14
15 Before running the sample:
16
17 pip install azure-ai-agents azure-identity
18
19 Set these environment variables with your own values:
20 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21 page of your Azure AI Foundry portal.
22 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23 the "Models + endpoints" tab in your Azure AI Foundry project.
24"""
25
26import os, sys
27from azure.ai.projects import AIProjectClient
28from azure.identity import DefaultAzureCredential
29from azure.ai.agents.models import FunctionTool
30from samples.utils.user_functions import user_functions
31
32project_client = AIProjectClient(
33 endpoint=os.environ["PROJECT_ENDPOINT"],
34 credential=DefaultAzureCredential(),
35)
36
37with project_client:
38 agents_client = project_client.agents
39
40 agent = agents_client.create_agent(
41 model=os.environ["MODEL_DEPLOYMENT_NAME"],
42 name="my-agent",
43 instructions="You are helpful agent",
44 )
45
46 # To enable tool calls executed automatically you can pass a functions in one of these types:
47 # Set[Callable[..., Any]], _models.FunctionTool, or _models.ToolSet
48 # Example 1:
49 # agents_client.enable_auto_function_calls(user_functions)
50 # Example 2:
51 # functions = FunctionTool(user_functions)
52 # agents_client.enable_auto_function_calls(functions)
53 # Example 3:
54 # functions = FunctionTool(user_functions)
55 # toolset = ToolSet()
56 # toolset.add(functions)
57 # agents_client.enable_auto_function_calls(toolset)
58 agents_client.enable_auto_function_calls(user_functions)
59 # Notices that `enable_auto_function_calls` can be made at any time.
60
61 functions = FunctionTool(user_functions)
62
63 # Initialize agent.
64 # Whether you would like the functions to be called automatically or not, it is required to pass functions as tools or toolset.
65 # NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
66 agent = agents_client.create_agent(
67 model=os.environ["MODEL_DEPLOYMENT_NAME"],
68 name="my-agent",
69 instructions="You are a helpful agent",
70 tools=functions.definitions,
71 )
72
73 print(f"Created agent, ID: {agent.id}")
74
75 # Create thread for communication
76 thread = agents_client.threads.create()
77 print(f"Created thread, ID: {thread.id}")
78
79 # Create message to thread
80 message = agents_client.messages.create(
81 thread_id=thread.id,
82 role="user",
83 content="Hello, send an email with the datetime and weather information in New York?",
84 )
85 print(f"Created message, ID: {message.id}")
86
87 # Create and process agent run in thread with tools
88 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
89 print(f"Run finished with status: {run.status}")
90
91 if run.status == "failed":
92 print(f"Run failed: {run.last_error}")
93
94 # Clean-up and delete the agent once the run is finished.
95 # NOTE: Comment out this line if you plan to reuse the agent later.
96 agents_client.delete_agent(agent.id)
97 print("Deleted agent")
98
99 # Fetch and log all messages
100 messages = agents_client.messages.list(thread_id=thread.id)
101 for msg in messages:
102 if msg.text_messages:
103 last_text = msg.text_messages[-1]
104 print(f"{msg.role}: {last_text.text.value}")