Skip to content

Commit ad837d7

Browse files
authored
[AI] [Projects] Add Fabric sample (#40116)
* [AI] [Projects] add fabric sample * update README and sample * use agent_id param in sample * review feedback * more review feedback * Even more feedback * run snippet updater
1 parent 2cdc1dc commit ad837d7

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

sdk/ai/azure-ai-projects/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ To report an issue with the client library, or request additional features, plea
4646
- [Function call](#create-agent-with-function-call)
4747
- [Azure Function Call](#create-agent-with-azure-function-call)
4848
- [OpenAPI](#create-agent-with-openapi)
49+
- [Fabric data](#create-an-agent-with-fabric)
4950
- [Create thread](#create-thread) with
5051
- [Tool resource](#create-thread-with-tool-resource)
5152
- [Create message](#create-message) with:
@@ -810,6 +811,37 @@ with project_client:
810811

811812
<!-- END SNIPPET -->
812813

814+
#### Create an Agent with Fabric
815+
816+
To enable your Agent to answer queries using Fabric data, use `FabricTool` along with a connection to the Fabric resource.
817+
818+
Here is an example:
819+
820+
<!-- SNIPPET:sample_agents_fabric.create_agent_with_fabric_tool -->
821+
822+
```python
823+
fabric_connection = project_client.connections.get(connection_name=os.environ["FABRIC_CONNECTION_NAME"])
824+
conn_id = fabric_connection.id
825+
826+
print(conn_id)
827+
828+
# Initialize an Agent Fabric tool and add the connection id
829+
fabric = FabricTool(connection_id=conn_id)
830+
831+
# Create an Agent with the Fabric tool and process an Agent run
832+
with project_client:
833+
agent = project_client.agents.create_agent(
834+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
835+
name="my-agent",
836+
instructions="You are a helpful agent",
837+
tools=fabric.definitions,
838+
headers={"x-ms-enable-preview": "true"},
839+
)
840+
```
841+
842+
<!-- END SNIPPET -->
843+
844+
813845
#### Create Thread
814846

815847
For each session or conversation, a thread is required. Here is an example:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
FILE: sample_agents_fabric.py
8+
9+
DESCRIPTION:
10+
This sample demonstrates how to use Agent operations with the Microsoft Fabric grounding tool from
11+
the Azure Agents service using a synchronous client.
12+
13+
USAGE:
14+
python sample_agents_fabric.py
15+
16+
Before running the sample:
17+
18+
pip install azure-ai-projects azure-identity
19+
20+
Set this environment variables with your own values:
21+
PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project.
22+
"""
23+
24+
import os
25+
from azure.ai.projects import AIProjectClient
26+
from azure.identity import DefaultAzureCredential
27+
from azure.ai.projects.models import FabricTool
28+
29+
project_client = AIProjectClient.from_connection_string(
30+
credential=DefaultAzureCredential(),
31+
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
32+
)
33+
34+
# [START create_agent_with_fabric_tool]
35+
fabric_connection = project_client.connections.get(connection_name=os.environ["FABRIC_CONNECTION_NAME"])
36+
conn_id = fabric_connection.id
37+
38+
print(conn_id)
39+
40+
# Initialize an Agent Fabric tool and add the connection id
41+
fabric = FabricTool(connection_id=conn_id)
42+
43+
# Create an Agent with the Fabric tool and process an Agent run
44+
with project_client:
45+
agent = project_client.agents.create_agent(
46+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
47+
name="my-agent",
48+
instructions="You are a helpful agent",
49+
tools=fabric.definitions,
50+
headers={"x-ms-enable-preview": "true"},
51+
)
52+
# [END create_agent_with_fabric_tool]
53+
print(f"Created Agent, ID: {agent.id}")
54+
55+
# Create thread for communication
56+
thread = project_client.agents.create_thread()
57+
print(f"Created thread, ID: {thread.id}")
58+
59+
# Create message to thread
60+
message = project_client.agents.create_message(
61+
thread_id=thread.id,
62+
role="user",
63+
content="<User query against Fabric resource>",
64+
)
65+
print(f"Created message, ID: {message.id}")
66+
67+
# Create and process an Agent run in thread with tools
68+
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
69+
print(f"Run finished with status: {run.status}")
70+
71+
if run.status == "failed":
72+
print(f"Run failed: {run.last_error}")
73+
74+
# Delete the Agent when done
75+
project_client.agents.delete_agent(agent.id)
76+
print("Deleted agent")
77+
78+
# Fetch and log all messages
79+
messages = project_client.agents.list_messages(thread_id=thread.id)
80+
print(f"Messages: {messages}")

0 commit comments

Comments
 (0)