Skip to content

Commit e7ab1ce

Browse files
authored
Merge pull request #72 from umangsehgal/umangsehgal-python-samples-project-sdk
python samples for project sdk 1.0.0a20250506002
2 parents dabede9 + 2ba1d8c commit e7ab1ce

File tree

14 files changed

+642
-364
lines changed

14 files changed

+642
-364
lines changed

samples/microsoft/python/getting-started-agents/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ This table tracks the current status of code samples for each supported tool in
1414
| | Managing files | ❌ Doesn't exist | |
1515
| **Azure AI Search**| Using a knowledge store | ✅ Exists and works | |
1616
| **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 | |
19-
| **Function Calling**| Calling local functions | ❌ Doesn't exist | |
20-
| **Azure Functions**| Calling durable Azure Functions | ❌ Doesn't exist | |
21-
| **Logic Apps** | Calling Logic Apps workflows | ❌ Doesn't exist | |
17+
| **SharePoint** | Grounding with SharePoint files | Exists and works | |
18+
| **TripAdvisor** | Using licensed TripAdvisor data | Exists | |
19+
| **Function Calling**| Calling local functions | ✅ Exists and works | |
20+
| **Azure Functions**| Calling durable Azure Functions | ⚠️ Exists | To be tested |
21+
| **Logic Apps** | Calling Logic Apps workflows | ⚠️ Exists | To be tested |
2222
| **Code Interpreter**| Using Code Interpreter | ❌ Doesn't exist | |
2323
| **OpenAPI** | Calling external APIs with OpenAPI | ❌ Doesn't exist | |
24-
| **Basic Agent** | Using agent with no tools | ❌ Doesn't exist | |
25-
| **Quickstart** | Basic setup and usage example | ❌ Doesn't exist | |
24+
| **Basic Agent** | Using agent with no tools | ✅ Exists and works | |
25+
| **Quickstart** | Basic setup and usage example | ✅ Exists and works | |
2626

2727
---
2828

samples/microsoft/python/getting-started-agents/agent_sharepoint.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
2525
import os
2626
from azure.identity import DefaultAzureCredential
2727
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
28+
from azure.ai.agents.models import MessageRole, SharepointTool
3129

3230
# Retrieve endpoint and model deployment name from environment variables
33-
endpoint = os.environ["PROJECT_ENDPOINT"], # Ensure the PROJECT_ENDPOINT environment variable is set
31+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
3432
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Ensure the MODEL_DEPLOYMENT_NAME environment variable is set
3533

3634
# Initialize the AIProjectClient with the endpoint and credentials
37-
with AIProjectClient(
38-
endpoint=endpoint,
35+
project_client = AIProjectClient(
36+
endpoint=project_endpoint,
3937
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
40-
) as project_client:
38+
api_version="latest",
39+
)
40+
41+
with project_client:
4142
# Initialize SharePoint tool with connection ID
4243
sharepoint_connection = project_client.connections.get(
4344
name="CONNECTION_NAME", # Replace with your actual connection name
@@ -46,11 +47,8 @@
4647
print(conn_id)
4748
sharepoint = SharepointTool(connection_id=conn_id) # Initialize the SharePoint tool with the connection ID
4849

49-
# Access the agents client from the project client
50-
agents_client = project_client.agents
51-
5250
# Create an agent with the specified model, name, instructions, and tools
53-
agent = agents_client.create_agent(
51+
agent = project_client.agents.create_agent(
5452
model=model_deployment_name, # Model deployment name
5553
name="my-agent", # Name of the agent
5654
instructions="You are a helpful agent", # Instructions for the agent
@@ -59,33 +57,31 @@
5957
print(f"Created agent, ID: {agent.id}")
6058

6159
# Create a thread for communication with the agent
62-
thread = agents_client.create_thread()
60+
thread = project_client.agents.threads.create()
6361
print(f"Created thread, ID: {thread.id}")
6462

6563
# Send a message to the thread
66-
message = agents_client.create_message(
64+
message = project_client.agents.messages.create(
6765
thread_id=thread.id, # ID of the thread
68-
role="user", # Role of the message sender (e.g., user)
66+
role=MessageRole.USER, # Role of the message sender (e.g., user)
6967
content="Hello, summarize the key points of the <sharepoint_resource_document>", # Message content
7068
)
71-
print(f"Created message, ID: {message.id}")
69+
print(f"Created message, ID: {message['id']}")
7270

7371
# 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)
72+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
7573
print(f"Run finished with status: {run.status}")
7674

7775
if run.status == "failed":
7876
# Log the error if the run fails
7977
print(f"Run failed: {run.last_error}")
8078

8179
# Delete the agent when done to clean up resources
82-
agents_client.delete_agent(agent.id)
80+
project_client.agents.delete_agent(agent.id)
8381
print("Deleted agent")
8482

8583
# 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
84+
messages = project_client.agents.messages.list(thread_id=thread.id)
85+
for message in messages:
86+
print(f"Role: {message['role']}, Content: {message['content']}")
9187

samples/microsoft/python/getting-started-agents/azure_ai_search.py

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,107 +26,64 @@
2626
import os
2727
from azure.identity import DefaultAzureCredential
2828
from azure.ai.projects import AIProjectClient
29-
from azure.ai.projects import AIProjectClient
30-
from azure.identity import DefaultAzureCredential
3129
from azure.ai.agents.models import AzureAISearchQueryType, AzureAISearchTool, ListSortOrder, MessageRole
32-
from azure.identity import DefaultAzureCredential
3330

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"]
31+
# Retrieve endpoint and model deployment name from environment variables
32+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
33+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Ensure the MODEL_DEPLOYMENT_NAME environment variable is set
3634

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
35+
# Initialize the AIProjectClient with the endpoint and credentials
36+
project_client = AIProjectClient(
37+
endpoint=project_endpoint,
38+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
39+
api_version="latest",
4440
)
4541

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
42+
with project_client:
43+
# Initialize the Azure AI Search tool with the required parameters
44+
ai_search = AzureAISearchTool(
45+
index_connection_id=os.environ["AZURE_AI_CONNECTION_ID"], # Connection ID for the Azure AI Search index
46+
index_name="sample_index", # Name of the search index
47+
query_type=AzureAISearchQueryType.SIMPLE, # Query type (e.g., SIMPLE, FULL)
48+
top_k=3, # Number of top results to retrieve
49+
filter="", # Optional filter for search results
50+
)
5751

5852
# Create an agent with the specified model, name, instructions, and tools
59-
agent = agents_client.create_agent(
53+
agent = project_client.agents.create_agent(
6054
model=model_deployment_name, # Model deployment name
61-
name="my-agent", # Name of the agent
55+
name="my-agent", # Name of the agent
6256
instructions="You are a helpful agent", # Instructions for the agent
6357
tools=ai_search.definitions, # Tools available to the agent
6458
tool_resources=ai_search.resources, # Resources for the tools
6559
)
66-
# [END create_agent_with_azure_ai_search_tool]
6760
print(f"Created agent, ID: {agent.id}")
6861

6962
# Create a thread for communication with the agent
70-
thread = agents_client.create_thread()
63+
thread = project_client.agents.threads.create()
7164
print(f"Created thread, ID: {thread.id}")
7265

73-
# Create a message in the thread to interact with the agent
74-
message = agents_client.create_message(
66+
# Send a message to the thread
67+
message = project_client.agents.messages.create(
7568
thread_id=thread.id, # ID of the thread
76-
role="user", # Role of the message sender (e.g., user)
69+
role=MessageRole.USER, # Role of the message sender (e.g., user)
7770
content="What is the temperature rating of the cozynights sleeping bag?", # Message content
7871
)
79-
print(f"Created message, ID: {message.id}")
72+
print(f"Created message, ID: {message['id']}")
8073

8174
# 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)
75+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
8376
print(f"Run finished with status: {run.status}")
8477

85-
# Check if the run failed and log the error if applicable
8678
if run.status == "failed":
79+
# Log the error if the run fails
8780
print(f"Run failed: {run.last_error}")
8881

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
82+
# Fetch and log all messages from the thread
83+
messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
84+
for message in messages.data:
85+
print(f"Role: {message.role}, Content: {message.content}")
10886

10987
# Delete the agent when done to clean up resources
110-
agents_client.delete_agent(agent.id)
88+
project_client.agents.delete_agent(agent.id)
11189
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)