Skip to content

Commit 0325feb

Browse files
committed
python code samples
1 parent 27ff02a commit 0325feb

File tree

3 files changed

+129
-23
lines changed

3 files changed

+129
-23
lines changed

articles/ai-foundry/agents/how-to/tools/fabric.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ You can add the Microsoft Fabric tool to an agent programmatically using the cod
6565
:::image type="content" source="../../media\tools\fabric-foundry.png" alt-text="A screenshot showing the fabric connection in the Azure AI Foundry portal." lightbox="../../media\tools\fabric-foundry.png":::
6666

6767
:::zone-end
68-
<!--
68+
6969
:::zone pivot="python"
7070

7171
## Create a project client
@@ -74,18 +74,17 @@ Create a client object, which will contain the connection string for connecting
7474

7575
```python
7676
import os
77-
from azure.identity import DefaultAzureCredential
7877
from azure.ai.projects import AIProjectClient
79-
from azure.ai.agents.models import FabricTool
78+
from azure.identity import DefaultAzureCredential
79+
from azure.ai.agents.models import FabricTool, ListSortOrder
8080

8181
# Retrieve the endpoint and credentials
8282
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
8383

8484
# Initialize the AIProjectClient
8585
project_client = AIProjectClient(
86-
endpoint=project_endpoint,
87-
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication
88-
api_version="latest",
86+
endpoint=os.environ["PROJECT_ENDPOINT"],
87+
credential=DefaultAzureCredential(),
8988
)
9089
```
9190

@@ -104,13 +103,14 @@ conn_id = os.environ["FABRIC_CONNECTION_ID"] # Ensure the FABRIC_CONNECTION_ID
104103
fabric = FabricTool(connection_id=conn_id)
105104

106105
# Create an agent with the Fabric tool
106+
# Create an Agent with the Fabric tool and process an Agent run
107107
with project_client:
108-
agent = project_client.agents.create_agent(
109-
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Model deployment name
110-
name="my-agent", # Name of the agent
111-
instructions="You are a helpful agent", # Instructions for the agent
112-
tools=fabric.definitions, # Attach the Fabric tool
113-
headers={"x-ms-enable-preview": "true"}, # Enable preview features
108+
agents_client = project_client.agents
109+
agent = agents_client.create_agent(
110+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
111+
name="my-agent",
112+
instructions="You are a helpful agent",
113+
tools=fabric.definitions,
114114
)
115115
print(f"Created Agent, ID: {agent.id}")
116116
```
@@ -134,26 +134,27 @@ print(f"Created message, ID: {message['id']}")
134134
## Create a run and check the output
135135

136136
```python
137-
# Create and process an agent run in the thread
138-
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
137+
# Create and process an Agent run in thread with tools
138+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
139139
print(f"Run finished with status: {run.status}")
140140

141-
# Check if the run failed
142141
if run.status == "failed":
143142
print(f"Run failed: {run.last_error}")
144143

145-
# Fetch and log all messages from the thread
146-
messages = project_client.agents.messages.list(thread_id=thread.id)
147-
for message in messages.data:
148-
print(f"Role: {message.role}, Content: {message.content}")
144+
# Uncomment the following lines to delete the agent when done
145+
#agents_client.delete_agent(agent.id)
146+
#print("Deleted agent")
149147

150-
# Delete the agent after use
151-
project_client.agents.delete_agent(agent.id)
152-
print("Deleted agent")
148+
# Fetch and log all messages
149+
messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
150+
for msg in messages:
151+
if msg.text_messages:
152+
last_text = msg.text_messages[-1]
153+
print(f"{msg.role}: {last_text.text.value}")
153154
```
154155

155156
:::zone-end
156-
-->
157+
157158

158159
<!--
159160
:::zone pivot="csharp"

articles/ai-foundry/agents/how-to/tools/sharepoint-samples.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,99 @@ zone_pivot_groups: selection-sharepoint
2020
2121
Use this article to find step-by-step instructions and code samples for using the SharePoint tool in Azure AI Foundry Agent Service.
2222

23+
:::zone pivot="python"
24+
25+
26+
## Create a project client
27+
28+
Create a client object, which will contain the connection string for connecting to your AI project and other resources.
29+
30+
```python
31+
import os
32+
from azure.ai.projects import AIProjectClient
33+
from azure.identity import DefaultAzureCredential
34+
from azure.ai.agents.models import SharepointTool
35+
36+
# Retrieve the endpoint and credentials
37+
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
38+
39+
# Initialize the AIProjectClient
40+
project_client = AIProjectClient(
41+
endpoint=os.environ["PROJECT_ENDPOINT"],
42+
credential=DefaultAzureCredential(),
43+
)
44+
```
45+
46+
## Create an agent with the Sharepoint tool enabled
47+
48+
To make the Microsoft Fabric tool available to your agent, use a connection to initialize the tool and attach it to the agent. You can find your connection in the **connected resources** section of your project in the Azure AI Foundry portal.
49+
50+
```python
51+
# The Fabric connection id can be found in the Azure AI Foundry project as a property of the Fabric tool
52+
# Your connection id is in the format /subscriptions/<your-subscription-id>/resourceGroups/<your-resource-group>/providers/Microsoft.MachineLearningServices/workspaces/<your-project-name>/connections/<your-fabric-connection-name>
53+
54+
# Retrieve the Fabric connection ID from environment variables
55+
conn_id = os.environ["SHAREPOINT_CONNECTION_ID"] # Ensure the environment variable is set
56+
57+
# Initialize Sharepoint tool with connection id
58+
sharepoint = SharepointTool(connection_id=conn_id)
59+
60+
# Create an agent with the Fabric tool
61+
# Create an Agent with the Fabric tool and process an Agent run
62+
with project_client:
63+
agents_client = project_client.agents
64+
65+
agent = agents_client.create_agent(
66+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
67+
name="my-agent",
68+
instructions="You are a helpful agent",
69+
tools=sharepoint.definitions,
70+
)
71+
print(f"Created agent, ID: {agent.id}")
72+
```
73+
74+
## Create a thread
75+
76+
```python
77+
# Create thread for communication
78+
thread = agents_client.threads.create()
79+
print(f"Created thread, ID: {thread.id}")
80+
81+
# Create message to thread
82+
message = agents_client.messages.create(
83+
thread_id=thread.id,
84+
role="user",
85+
content="Hello, summarize the key points of the <sharepoint_resource_document>",
86+
)
87+
print(f"Created message, ID: {message.id}")
88+
```
89+
90+
## Create a run and check the output
91+
92+
```python
93+
# Create and process agent run in thread with tools
94+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
95+
print(f"Run finished with status: {run.status}")
96+
97+
if run.status == "failed":
98+
print(f"Run failed: {run.last_error}")
99+
100+
# Uncomment the following lines to delete the agent when done
101+
#agents_client.delete_agent(agent.id)
102+
#print("Deleted agent")
103+
104+
# Fetch and log all messages
105+
messages = agents_client.messages.list(thread_id=thread.id)
106+
for msg in messages:
107+
if msg.text_messages:
108+
last_text = msg.text_messages[-1]
109+
print(f"{msg.role}: {last_text.text.value}")
110+
```
111+
112+
:::zone-end
113+
114+
:::zone pivot="rest"
115+
23116
## Create an agent
24117

25118
Follow the [REST API Quickstart](../../quickstart.md?pivots=rest-api#api-call-information) to set the right values for the environment variables `AGENT_TOKEN`, `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT` and `API_VERSION`.
@@ -98,3 +191,5 @@ curl --request GET \
98191
--url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=$API_VERSION \
99192
-H "Authorization: Bearer $AGENT_TOKEN"
100193
```
194+
195+
:::zone-end

zone-pivots/zone-pivot-groups.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,16 @@ groups:
11031103
pivots:
11041104
- id: portal
11051105
title: Azure AI Foundry portal
1106+
- id: python
1107+
title: Python
1108+
- id: rest
1109+
title: REST API
1110+
- id: selection-agent-sharepoint
1111+
title: Selections
1112+
prompt: What would you like to see?
1113+
pivots:
1114+
- id: python
1115+
title: Python
11061116
- id: rest
11071117
title: REST API
11081118
- id: selection-file-search-upload

0 commit comments

Comments
 (0)