forked from microsoft-foundry/foundry-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (52 loc) · 2.49 KB
/
Copy pathmain.py
File metadata and controls
60 lines (52 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import dotenv
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition, MCPTool
from azure.identity import DefaultAzureCredential
dotenv.load_dotenv()
project_client = AIProjectClient(
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
credential=DefaultAzureCredential(),
)
openai_client = project_client.get_openai_client()
tools = [
MCPTool(
# This is just a placeholder. Connection details are in
# the project connection referenced by `project_connection_id`.
server_url="https://localhost",
server_label="python_tool",
require_approval="never",
allowed_tools=[
"launchShell",
"runPythonCodeInRemoteEnvironment",
],
project_connection_id=os.environ["AZURE_AI_CONNECTION_ID"],
),
]
EXAMPLE_DATA_FILE_URL = "https://raw.githubusercontent.com/Azure-Samples/azureai-samples/refs/heads/main/scenarios/Agents/data/nifty_500_quarterly_results.csv"
with project_client:
agent = project_client.agents.create_version(
agent_name="MyAgent",
definition=PromptAgentDefinition(
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
instructions="""\
You are a helpful agent that can use a Python code interpreter to assist users. Use the `python_tool` MCP
server to perform any calculations or numerical analyses. ALWAYS call the `launchShell` tool first before
calling the `runPythonCodeInRemoteEnvironment` tool. If you need to provide any non-text data to the user,
always print a data URI with the contents. NEVER provide a path to a file in the remote environment to the user.
""",
temperature=0,
tools=tools,
),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
# Use the agent to analyze a CSV file and produce a histogram
response = openai_client.responses.create(
input=f"Please analyze the CSV file at {EXAMPLE_DATA_FILE_URL}. Could you please create bar chart in the TRANSPORTATION sector for the operating profit and provide a file to me?",
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
)
print(f"[Response {response.id}]: {response.output_text}")
# Clean up resources by deleting the agent version
# This prevents accumulation of unused agent versions in your project
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
print("Agent deleted")