Skip to content

Commit 05a9e34

Browse files
authored
Merge pull request #10 from azure-ai-foundry/python-tools-estraight
Python tools estraight
2 parents bf64b95 + 5c052ab commit 05a9e34

File tree

9 files changed

+1455
-0
lines changed

9 files changed

+1455
-0
lines changed
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>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 agent operations with code interpreter from
10+
the Azure Agents service using a synchronous client.
11+
12+
USAGE:
13+
python sample_agents_code_interpreter.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_ENDPOINT - The project endpoint, 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+
"""
25+
26+
# <imports>
27+
import os
28+
from azure.ai.projects import AIProjectClient
29+
from azure.ai.agents.models import CodeInterpreterTool
30+
from azure.ai.agents.models import FilePurpose, MessageRole
31+
from azure.identity import DefaultAzureCredential
32+
from pathlib import Path
33+
# </imports>
34+
35+
# <client_initialization>
36+
endpoint = os.environ["PROJECT_ENDPOINT"]
37+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"]
38+
39+
with AIProjectClient(
40+
endpoint=endpoint,
41+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False),
42+
) as project_client:
43+
# </client_initialization>
44+
45+
# Upload a file and wait for it to be processed
46+
# [START upload_file_and_create_agent_with_code_interpreter]
47+
# <file_upload>
48+
file = project_client.agents.upload_file_and_poll(
49+
file_path=str(Path(__file__).parent / "nifty_500_quarterly_results.csv"), purpose=FilePurpose.AGENTS
50+
)
51+
print(f"Uploaded file, file ID: {file.id}")
52+
# </file_upload>
53+
54+
# <code_interpreter_setup>
55+
code_interpreter = CodeInterpreterTool(file_ids=[file.id])
56+
# </code_interpreter_setup>
57+
58+
# <agent_creation>
59+
# Create agent with code interpreter tool and tools_resources
60+
agent = project_client.agents.create_agent(
61+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
62+
name="my-assistant",
63+
instructions="You are helpful assistant",
64+
tools=code_interpreter.definitions,
65+
tool_resources=code_interpreter.resources,
66+
)
67+
# [END upload_file_and_create_agent_with_code_interpreter]
68+
print(f"Created agent, agent ID: {agent.id}")
69+
# </agent_creation>
70+
71+
# <thread_management>
72+
thread = project_client.agents.create_thread()
73+
print(f"Created thread, thread ID: {thread.id}")
74+
75+
# Create a message
76+
message = project_client.agents.create_message(
77+
thread_id=thread.id,
78+
role="user",
79+
content="Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?",
80+
)
81+
print(f"Created message, message ID: {message.id}")
82+
# </thread_management>
83+
84+
# <message_processing>
85+
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
86+
print(f"Run finished with status: {run.status}")
87+
88+
if run.status == "failed":
89+
# Check if you got "Rate limit is exceeded.", then you want to get more quota
90+
print(f"Run failed: {run.last_error}")
91+
# </message_processing>
92+
93+
# <file_handling>
94+
project_client.agents.delete_file(file.id)
95+
print("Deleted file")
96+
97+
# [START get_messages_and_save_files]
98+
messages = project_client.agents.list_messages(thread_id=thread.id)
99+
print(f"Messages: {messages}")
100+
101+
for image_content in messages.image_contents:
102+
file_id = image_content.image_file.file_id
103+
print(f"Image File ID: {file_id}")
104+
file_name = f"{file_id}_image_file.png"
105+
project_client.agents.save_file(file_id=file_id, file_name=file_name)
106+
print(f"Saved image file to: {Path.cwd() / file_name}")
107+
108+
for file_path_annotation in messages.file_path_annotations:
109+
print(f"File Paths:")
110+
print(f"Type: {file_path_annotation.type}")
111+
print(f"Text: {file_path_annotation.text}")
112+
print(f"File ID: {file_path_annotation.file_path.file_id}")
113+
print(f"Start Index: {file_path_annotation.start_index}")
114+
print(f"End Index: {file_path_annotation.end_index}")
115+
# [END get_messages_and_save_files]
116+
# </file_handling>
117+
118+
last_msg = messages.get_last_text_message_by_role(MessageRole.AGENT)
119+
if last_msg:
120+
print(f"Last Message: {last_msg.text.value}")
121+
122+
# <cleanup>
123+
project_client.agents.delete_agent(agent.id)
124+
print("Deleted agent")
125+
# </cleanup>

0 commit comments

Comments
 (0)