1# pylint: disable=line-too-long,useless-suppression
2# ------------------------------------
3# Copyright (c) Microsoft Corporation.
4# Licensed under the MIT License.
5# ------------------------------------
6
7"""
8FILE: sample_agents_sharepoint.py
9
10DESCRIPTION:
11 This sample demonstrates how to use agent operations with the
12 Sharepoint tool from the Azure Agents service using a synchronous client.
13 The sharepoint tool is currently available only to whitelisted customers.
14 For access and onboarding instructions, please contact azureagents-preview@microsoft.com.
15
16USAGE:
17 python sample_agents_sharepoint.py
18
19 Before running the sample:
20
21 pip install azure-identity
22 pip install --pre azure-ai-projects
23
24 Set this environment variables with your own values:
25 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
26 page of your Azure AI Foundry portal.
27 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
28 the "Models + endpoints" tab in your Azure AI Foundry project.
29 3) SHAREPOINT_CONNECTION_NAME - The name of a connection to the SharePoint resource as it is
30 listed in Azure AI Foundry connected resources.
31"""
32
33import os
34from azure.ai.projects import AIProjectClient
35from azure.identity import DefaultAzureCredential
36from azure.ai.agents.models import ListSortOrder, SharepointTool
37
38project_client = AIProjectClient(
39 endpoint=os.environ["PROJECT_ENDPOINT"],
40 credential=DefaultAzureCredential(),
41)
42
43conn_id = project_client.connections.get(os.environ["SHAREPOINT_CONNECTION_NAME"]).id
44
45# Initialize Sharepoint tool with connection id
46sharepoint = SharepointTool(connection_id=conn_id)
47
48# Create agent with Sharepoint tool and process agent run
49with project_client:
50 agents_client = project_client.agents
51
52 agent = agents_client.create_agent(
53 model=os.environ["MODEL_DEPLOYMENT_NAME"],
54 name="my-agent",
55 instructions="You are a helpful agent",
56 tools=sharepoint.definitions,
57 )
58 print(f"Created agent, ID: {agent.id}")
59
60 # Create thread for communication
61 thread = agents_client.threads.create()
62 print(f"Created thread, ID: {thread.id}")
63
64 # Create message to thread
65 message = agents_client.messages.create(
66 thread_id=thread.id,
67 role="user",
68 content="Hello, summarize the key points of the <sharepoint_resource_document>",
69 )
70 print(f"Created message, ID: {message.id}")
71
72 # Create and process agent run in thread with tools
73 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
74 print(f"Run finished with status: {run.status}")
75
76 if run.status == "failed":
77 print(f"Run failed: {run.last_error}")
78
79 # Delete the agent when done
80 agents_client.delete_agent(agent.id)
81 print("Deleted agent")
82
83 # Fetch and log all messages
84 messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
85 for msg in messages:
86 if msg.text_messages:
87 responses = []
88 for text_message in msg.text_messages:
89 responses.append(text_message.text.value)
90 message = " ".join(responses)
91 for annotation in msg.url_citation_annotations:
92 message = message.replace(
93 annotation.text, f" [{annotation.url_citation.title}]({annotation.url_citation.url})"
94 )
95 print(f"{msg.role}: {message}")