Skip to content

Commit a31fb98

Browse files
committed
azure functions projects
1 parent ebcaa85 commit a31fb98

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# pylint: disable=line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to use azure function agent operations from
10+
the Azure Agents service using a synchronous client.
11+
12+
USAGE:
13+
python sample_agents_azure_functions.py
14+
15+
Before running the sample:
16+
17+
pip install azure-ai-projects azure-identity
18+
19+
Set these environment variables with your own values:
20+
1) PROJECT_CONNECTION_STRING - The project connection string, as found in the overview page of your
21+
Azure AI Foundry project.
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+
3) STORAGE_SERVICE_ENDPONT - the storage service queue endpoint, triggering Azure function.
25+
Please see Getting Started with Azure Functions page for more information on Azure Functions:
26+
https://learn.microsoft.com/azure/azure-functions/functions-get-started
27+
"""
28+
29+
# <imports>
30+
import os
31+
from azure.ai.projects import AIProjectClient
32+
from azure.ai.agents.models import AzureFunctionStorageQueue, AzureFunctionTool, MessageRole
33+
from azure.identity import DefaultAzureCredential
34+
# </imports>
35+
36+
# <client_initialization>
37+
endpoint = os.environ["PROJECT_ENDPOINT"]
38+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"]
39+
with AIProjectClient(
40+
endpoint=endpoint,
41+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False),
42+
) as project_client:
43+
# </client_initialization>
44+
storage_service_endpoint = os.environ["STORAGE_SERVICE_ENDPOINT"]
45+
46+
# [START create_agent_with_azure_function_tool]
47+
# <azure_function_tool_setup>
48+
azure_function_tool = AzureFunctionTool(
49+
name="foo",
50+
description="Get answers from the foo bot.",
51+
parameters={
52+
"type": "object",
53+
"properties": {
54+
"query": {"type": "string", "description": "The question to ask."},
55+
"outputqueueuri": {"type": "string", "description": "The full output queue uri."},
56+
},
57+
},
58+
input_queue=AzureFunctionStorageQueue(
59+
queue_name="azure-function-foo-input",
60+
storage_service_endpoint=storage_service_endpoint,
61+
),
62+
output_queue=AzureFunctionStorageQueue(
63+
queue_name="azure-function-tool-output",
64+
storage_service_endpoint=storage_service_endpoint,
65+
),
66+
)
67+
# </azure_function_tool_setup>
68+
# <agent_creation>
69+
agent = project_client.agents.create_agent(
70+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
71+
name="azure-function-agent-foo",
72+
instructions=f"You are a helpful support agent. Use the provided function any time the prompt contains the string 'What would foo say?'. When you invoke the function, ALWAYS specify the output queue uri parameter as '{storage_service_endpoint}/azure-function-tool-output'. Always responds with \"Foo says\" and then the response from the tool.",
73+
tools=azure_function_tool.definitions,
74+
)
75+
print(f"Created agent, agent ID: {agent.id}")
76+
# [END create_agent_with_azure_function_tool]
77+
# </agent_creation>
78+
79+
# <thread_management>
80+
# Create a thread
81+
thread = project_client.agents.create_thread()
82+
print(f"Created thread, thread ID: {thread.id}")
83+
84+
# Create a message
85+
message = project_client.agents.create_message(
86+
thread_id=thread.id,
87+
role="user",
88+
content="What is the most prevalent element in the universe? What would foo say?",
89+
)
90+
print(f"Created message, message ID: {message.id}")
91+
# </thread_management>
92+
93+
# <message_processing>
94+
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
95+
if run.status == "failed":
96+
print(f"Run failed: {run.last_error}")
97+
98+
# Get messages from the thread
99+
messages = project_client.agents.list_messages(thread_id=thread.id)
100+
print(f"Messages: {messages}")
101+
102+
# Get the last message from agent
103+
last_msg = messages.get_last_text_message_by_role(MessageRole.AGENT)
104+
if last_msg:
105+
print(f"Last Message: {last_msg.text.value}")
106+
# </message_processing>
107+
108+
# <cleanup>
109+
# Delete the agent once done
110+
result = project_client.agents.delete_agent(agent.id)
111+
if result.deleted:
112+
print(f"Deleted agent {result.id}")
113+
else:
114+
print(f"Failed to delete agent {result.id}")
115+
# </cleanup>

0 commit comments

Comments
 (0)