Skip to content

Commit eb5a0cb

Browse files
authored
Merge pull request #52 from umangsehgal/main
Foundry ProjectSDK Samples - umangsehgal
2 parents c0a90d6 + d65b052 commit eb5a0cb

File tree

10 files changed

+1000
-115
lines changed

10 files changed

+1000
-115
lines changed

doc-samples/agents/python/project-client/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ This table tracks the current status of code samples for each supported tool in
88

99
| Sample | Sample Description | Status | Notes / Known Issues |
1010
|--------------------|--------------------------------------------------|--------------------------|--------------------------------------------|
11-
| **Bing** | Using Bing in an agent | ⚠️ Doesn't work | |
12-
| **File Search** | Uploading files | ❌ Doesn't exist | |
13-
| | Using blob storage (project data assets) | ❌ Doesn't exist | |
11+
| **Bing** | Using Bing in an agent | ✅ Exists and works | |
12+
| **File Search** | Uploading files | ✅ Exists and works | |
13+
| | Using blob storage (project data assets) | ✅ Exists and works | |
1414
| | Managing files | ❌ Doesn't exist | |
15-
| **Azure AI Search**| Using a knowledge store | ❌ Doesn't exist | |
16-
| **Fabric** | Grounding with Fabric data | ❌ Doesn't exist | |
17-
| **SharePoint** | Grounding with SharePoint files | ❌ Doesn't exist | |
18-
| **TripAdvisor** | Using licensed TripAdvisor data | ❌ Doesn't exist | |
15+
| **Azure AI Search**| Using a knowledge store | ✅ Exists and works | |
16+
| **Fabric** | Grounding with Fabric data | ✅ Exists and works | |
17+
| **SharePoint** | Grounding with SharePoint files | ⚠️ Exists and yet to work | |
18+
| **TripAdvisor** | Using licensed TripAdvisor data | ⚠️ Exists and doesn't work | |
1919
| **Function Calling**| Calling local functions | ❌ Doesn't exist | |
2020
| **Azure Functions**| Calling durable Azure Functions | ❌ Doesn't exist | |
2121
| **Logic Apps** | Calling Logic Apps workflows | ❌ Doesn't exist | |
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
"""
6+
FILE: agent_sharepoint.py
7+
8+
DESCRIPTION:
9+
This sample demonstrates how to use agent operations with the SharePoint tool from
10+
the Azure Agents service using a synchronous client.
11+
12+
USAGE:
13+
python agent_sharepoint.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+
PROJECT_ENDPOINT - the Azure AI Project endpoint, as found in your AI Studio Project.
21+
MODEL_DEPLOYMENT_NAME - the deployment name of the AI model.
22+
CONNECTION_NAME - the name of the SharePoint connection.
23+
"""
24+
25+
import os
26+
from azure.identity import DefaultAzureCredential
27+
from azure.ai.projects import AIProjectClient
28+
from azure.ai.projects import AIProjectClient
29+
from azure.ai.agents.models import SharepointTool
30+
from azure.identity import DefaultAzureCredential
31+
32+
# Retrieve endpoint and model deployment name from environment variables
33+
endpoint = os.environ["PROJECT_ENDPOINT"], # Ensure the PROJECT_ENDPOINT environment variable is set
34+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Ensure the MODEL_DEPLOYMENT_NAME environment variable is set
35+
36+
# Initialize the AIProjectClient with the endpoint and credentials
37+
with AIProjectClient(
38+
endpoint=endpoint,
39+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
40+
) as project_client:
41+
# Initialize SharePoint tool with connection ID
42+
sharepoint_connection = project_client.connections.get(
43+
name="CONNECTION_NAME", # Replace with your actual connection name
44+
)
45+
conn_id = sharepoint_connection.id # Retrieve the connection ID
46+
print(conn_id)
47+
sharepoint = SharepointTool(connection_id=conn_id) # Initialize the SharePoint tool with the connection ID
48+
49+
# Access the agents client from the project client
50+
agents_client = project_client.agents
51+
52+
# Create an agent with the specified model, name, instructions, and tools
53+
agent = agents_client.create_agent(
54+
model=model_deployment_name, # Model deployment name
55+
name="my-agent", # Name of the agent
56+
instructions="You are a helpful agent", # Instructions for the agent
57+
tools=sharepoint.definitions, # Tools available to the agent
58+
)
59+
print(f"Created agent, ID: {agent.id}")
60+
61+
# Create a thread for communication with the agent
62+
thread = agents_client.create_thread()
63+
print(f"Created thread, ID: {thread.id}")
64+
65+
# Send a message to the thread
66+
message = agents_client.create_message(
67+
thread_id=thread.id, # ID of the thread
68+
role="user", # Role of the message sender (e.g., user)
69+
content="Hello, summarize the key points of the <sharepoint_resource_document>", # Message content
70+
)
71+
print(f"Created message, ID: {message.id}")
72+
73+
# Create and process an agent run in the thread using the tools
74+
run = agents_client.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
75+
print(f"Run finished with status: {run.status}")
76+
77+
if run.status == "failed":
78+
# Log the error if the run fails
79+
print(f"Run failed: {run.last_error}")
80+
81+
# Delete the agent when done to clean up resources
82+
agents_client.delete_agent(agent.id)
83+
print("Deleted agent")
84+
85+
# Fetch and log all messages from the thread
86+
messages = agents_client.list_message(thread_id=thread.id)
87+
for msg in messages:
88+
if msg.text_messages: # Check if there are text messages
89+
last_text = msg.text_messages[-1] # Get the last text message
90+
print(f"{msg.role}: {last_text.text.value}") # Print the role and message content
91+
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
"""
6+
FILE: azure_ai_search.py
7+
8+
DESCRIPTION:
9+
This sample demonstrates how to use agent operations with the Azure AI Search tool from
10+
the Azure Agents service using a synchronous client.
11+
12+
USAGE:
13+
python azure_ai_search.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+
PROJECT_ENDPOINT - the Azure AI Project endpoint, as found in your AI Studio Project.
21+
MODEL_DEPLOYMENT_NAME - the deployment name of the AI model.
22+
AZURE_AI_CONNECTION_ID - the connection ID for the Azure AI Search tool.
23+
"""
24+
25+
# Import necessary libraries and modules
26+
import os
27+
from azure.identity import DefaultAzureCredential
28+
from azure.ai.projects import AIProjectClient
29+
from azure.ai.projects import AIProjectClient
30+
from azure.identity import DefaultAzureCredential
31+
from azure.ai.agents.models import AzureAISearchQueryType, AzureAISearchTool, ListSortOrder, MessageRole
32+
from azure.identity import DefaultAzureCredential
33+
34+
# Define the Azure AI Search connection ID (replace with your actual connection ID)
35+
azure_ai_conn_id = os.environ["AZURE_AI_CONNECTION_ID"]
36+
37+
# Initialize the Azure AI Search tool with the required parameters
38+
ai_search = AzureAISearchTool(
39+
index_connection_id=azure_ai_conn_id, # Connection ID for the Azure AI Search index
40+
index_name="sample_index", # Name of the search index
41+
query_type=AzureAISearchQueryType.SIMPLE, # Query type (e.g., SIMPLE, FULL)
42+
top_k=3, # Number of top results to retrieve
43+
filter="" # Optional filter for search results
44+
)
45+
46+
# Define the endpoint and model deployment name (replace with your actual values)
47+
endpoint = os.environ["PROJECT_ENDPOINT"],
48+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"]
49+
50+
# Create an AIProjectClient instance to interact with the Azure AI service
51+
with AIProjectClient(
52+
endpoint=endpoint, # Azure AI service endpoint
53+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Authentication credentials
54+
) as project_client:
55+
# Access the agents client from the project client
56+
agents_client = project_client.agents
57+
58+
# Create an agent with the specified model, name, instructions, and tools
59+
agent = agents_client.create_agent(
60+
model=model_deployment_name, # Model deployment name
61+
name="my-agent", # Name of the agent
62+
instructions="You are a helpful agent", # Instructions for the agent
63+
tools=ai_search.definitions, # Tools available to the agent
64+
tool_resources=ai_search.resources, # Resources for the tools
65+
)
66+
# [END create_agent_with_azure_ai_search_tool]
67+
print(f"Created agent, ID: {agent.id}")
68+
69+
# Create a thread for communication with the agent
70+
thread = agents_client.create_thread()
71+
print(f"Created thread, ID: {thread.id}")
72+
73+
# Create a message in the thread to interact with the agent
74+
message = agents_client.create_message(
75+
thread_id=thread.id, # ID of the thread
76+
role="user", # Role of the message sender (e.g., user)
77+
content="What is the temperature rating of the cozynights sleeping bag?", # Message content
78+
)
79+
print(f"Created message, ID: {message.id}")
80+
81+
# Create and process an agent run in the thread using the tools
82+
run = agents_client.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
83+
print(f"Run finished with status: {run.status}")
84+
85+
# Check if the run failed and log the error if applicable
86+
if run.status == "failed":
87+
print(f"Run failed: {run.last_error}")
88+
89+
# Fetch and log the details of the agent run steps
90+
run_steps = agents_client.list_run_steps(thread_id=thread.id, run_id=run.id)
91+
for step in run_steps.data:
92+
print(f"Step {step['id']} status: {step['status']}")
93+
step_details = step.get("step_details", {})
94+
tool_calls = step_details.get("tool_calls", [])
95+
96+
# Log details of tool calls if available
97+
if tool_calls:
98+
print(" Tool calls:")
99+
for call in tool_calls:
100+
print(f" Tool Call ID: {call.get('id')}")
101+
print(f" Type: {call.get('type')}")
102+
103+
azure_ai_search_details = call.get("azure_ai_search", {})
104+
if azure_ai_search_details:
105+
print(f" azure_ai_search input: {azure_ai_search_details.get('input')}")
106+
print(f" azure_ai_search output: {azure_ai_search_details.get('output')}")
107+
print() # Add an extra newline between steps
108+
109+
# Delete the agent when done to clean up resources
110+
agents_client.delete_agent(agent.id)
111+
print("Deleted agent")
112+
113+
# [START populate_references_agent_with_azure_ai_search_tool]
114+
# Fetch and log all messages in the thread
115+
messages = agents_client.list_messages(thread_id=thread.id, order=ListSortOrder.ASCENDING)
116+
for message in messages.data:
117+
# Log agent messages with URL citation annotations
118+
if message.role == MessageRole.AGENT and message.url_citation_annotations:
119+
placeholder_annotations = {
120+
annotation.text: f" [see {annotation.url_citation.title}] ({annotation.url_citation.url})"
121+
for annotation in message.url_citation_annotations
122+
}
123+
for message_text in message.text_messages:
124+
message_str = message_text.text.value
125+
for k, v in placeholder_annotations.items():
126+
message_str = message_str.replace(k, v)
127+
print(f"{message.role}: {message_str}")
128+
else:
129+
# Log other messages without annotations
130+
for message_text in message.text_messages:
131+
print(f"{message.role}: {message_text.text.value}")
132+
# [END populate_references_agent_with_azure_ai_search_tool]

0 commit comments

Comments
 (0)