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_fabric.py
9
10DESCRIPTION:
11 This sample demonstrates how to use Agent operations with the Microsoft Fabric grounding tool from
12 the Azure Agents service using a synchronous client.
13
14USAGE:
15 python sample_agents_fabric.py
16
17 Before running the sample:
18
19 pip install azure-identity
20 pip install --pre azure-ai-projects
21
22 Set this environment variables with your own values:
23 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
24 page of your Azure AI Foundry portal.
25 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
26 the "Models + endpoints" tab in your Azure AI Foundry project.
27 3) FABRIC_CONNECTION_NAME - The name of a connection to the Microsoft Fabric resource as it is
28 listed in Azure AI Foundry connected resources.
29"""
30
31import os
32from azure.ai.projects import AIProjectClient
33from azure.identity import DefaultAzureCredential
34from azure.ai.agents.models import FabricTool, ListSortOrder
35
36project_client = AIProjectClient(
37 endpoint=os.environ["PROJECT_ENDPOINT"],
38 credential=DefaultAzureCredential(),
39)
40
41# [START create_agent_with_fabric_tool]
42conn_id = project_client.connections.get(os.environ["FABRIC_CONNECTION_NAME"]).id
43
44print(conn_id)
45
46# Initialize an Agent Fabric tool and add the connection id
47fabric = FabricTool(connection_id=conn_id)
48
49# Create an Agent with the Fabric tool and process an Agent run
50with project_client:
51 agents_client = project_client.agents
52
53 agent = agents_client.create_agent(
54 model=os.environ["MODEL_DEPLOYMENT_NAME"],
55 name="my-agent",
56 instructions="You are a helpful agent",
57 tools=fabric.definitions,
58 )
59 # [END create_agent_with_fabric_tool]
60 print(f"Created Agent, ID: {agent.id}")
61
62 # Create thread for communication
63 thread = agents_client.threads.create()
64 print(f"Created thread, ID: {thread.id}")
65
66 # Create message to thread
67 message = agents_client.messages.create(
68 thread_id=thread.id,
69 role="user",
70 content="<User query against Fabric resource>",
71 )
72 print(f"Created message, ID: {message.id}")
73
74 # Create and process an Agent run in thread with tools
75 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
76 print(f"Run finished with status: {run.status}")
77
78 if run.status == "failed":
79 print(f"Run failed: {run.last_error}")
80
81 # Delete the Agent when done
82 agents_client.delete_agent(agent.id)
83 print("Deleted agent")
84
85 # Fetch and log all messages
86 messages = agents_client.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
87 for msg in messages:
88 if msg.text_messages:
89 last_text = msg.text_messages[-1]
90 print(f"{msg.role}: {last_text.text.value}")