|
| 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