|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +import asyncio |
| 6 | + |
| 7 | +""" |
| 8 | +DESCRIPTION: |
| 9 | + This sample demonstrates how to enable automatic function calls by calling `enable_auto_function_calls` |
| 10 | + using an asynchronous client. |
| 11 | +
|
| 12 | +USAGE: |
| 13 | + python sample_agents_auto_function_call_async.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 | + |
| 26 | +import os, sys |
| 27 | +from azure.ai.projects.aio import AIProjectClient |
| 28 | +from azure.identity import DefaultAzureCredential |
| 29 | +from azure.ai.agents.models import AsyncFunctionTool, AsyncToolSet |
| 30 | + |
| 31 | +from utils.user_async_functions import user_async_functions |
| 32 | + |
| 33 | + |
| 34 | +async def main() -> None: |
| 35 | + |
| 36 | + project_client = AIProjectClient( |
| 37 | + endpoint=os.environ["PROJECT_ENDPOINT"], |
| 38 | + credential=DefaultAzureCredential(), |
| 39 | + ) |
| 40 | + |
| 41 | + async with project_client: |
| 42 | + agents_client = project_client.agents |
| 43 | + |
| 44 | + # To enable tool calls executed automatically you can pass a functions in one of these types: |
| 45 | + # Set[Callable[..., Any]], _models.AsyncFunctionTool, or _models.AsyncToolSet |
| 46 | + # Example 1: |
| 47 | + # agents_client.enable_auto_function_calls(user_async_functions) |
| 48 | + # Example 2: |
| 49 | + # functions = AsyncFunctionTool(user_async_functions) |
| 50 | + # agents_client.enable_auto_function_calls(functions) |
| 51 | + # Example 3: |
| 52 | + # functions = AsyncFunctionTool(user_async_functions) |
| 53 | + # toolset = AsyncToolSet() |
| 54 | + # toolset.add(functions) |
| 55 | + # agents_client.enable_auto_function_calls(toolset) |
| 56 | + agents_client.enable_auto_function_calls(user_async_functions) |
| 57 | + # Notices that `enable_auto_function_calls` can be made at any time. |
| 58 | + |
| 59 | + functions = AsyncFunctionTool(user_async_functions) |
| 60 | + |
| 61 | + # Initialize agent. |
| 62 | + # Whether you would like the functions to be called automatically or not, it is required to pass functions as tools or toolset. |
| 63 | + agent = await agents_client.create_agent( |
| 64 | + model=os.environ["MODEL_DEPLOYMENT_NAME"], |
| 65 | + name="my-agent", |
| 66 | + instructions="You are a helpful agent", |
| 67 | + tools=functions.definitions, |
| 68 | + ) |
| 69 | + |
| 70 | + print(f"Created agent, ID: {agent.id}") |
| 71 | + |
| 72 | + # Create thread for communication |
| 73 | + thread = await agents_client.threads.create() |
| 74 | + print(f"Created thread, ID: {thread.id}") |
| 75 | + |
| 76 | + # Create message to thread |
| 77 | + message = await agents_client.messages.create( |
| 78 | + thread_id=thread.id, |
| 79 | + role="user", |
| 80 | + content="Hello, send an email with the datetime and weather information in New York?", |
| 81 | + ) |
| 82 | + print(f"Created message, ID: {message.id}") |
| 83 | + |
| 84 | + # Create and process agent run in thread with tools |
| 85 | + run = await agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id) |
| 86 | + print(f"Run finished with status: {run.status}") |
| 87 | + |
| 88 | + if run.status == "failed": |
| 89 | + print(f"Run failed: {run.last_error}") |
| 90 | + |
| 91 | + # Delete the agent when done |
| 92 | + await agents_client.delete_agent(agent.id) |
| 93 | + print("Deleted agent") |
| 94 | + |
| 95 | + # Fetch and log all messages |
| 96 | + messages = agents_client.messages.list(thread_id=thread.id) |
| 97 | + async for msg in messages: |
| 98 | + if msg.text_messages: |
| 99 | + last_text = msg.text_messages[-1] |
| 100 | + print(f"{msg.role}: {last_text.text.value}") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + asyncio.run(main()) |
0 commit comments