|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | +""" |
| 6 | +FILE: azure_ai_search.py |
| 7 | +
|
| 8 | +DESCRIPTION: |
| 9 | + This sample demonstrates how to use agent operations with the Azure AI Search tool from |
| 10 | + the Azure Agents service using a synchronous client. |
| 11 | +
|
| 12 | +USAGE: |
| 13 | + python azure_ai_search.py |
| 14 | +
|
| 15 | + Before running the sample: |
| 16 | +
|
| 17 | + pip install azure.ai.projects azure-identity |
| 18 | +
|
| 19 | + Set these environment variables with your own values: |
| 20 | + PROJECT_ENDPOINT - the Azure AI Project endpoint, as found in your AI Studio Project. |
| 21 | + MODEL_DEPLOYMENT_NAME - the deployment name of the AI model. |
| 22 | + AZURE_AI_CONNECTION_ID - the connection ID for the Azure AI Search tool. |
| 23 | +""" |
| 24 | + |
| 25 | +# Import necessary libraries and modules |
| 26 | +import os |
| 27 | +from azure.identity import DefaultAzureCredential |
| 28 | +from azure.ai.projects import AIProjectClient |
| 29 | +from azure.ai.projects import AIProjectClient |
| 30 | +from azure.identity import DefaultAzureCredential |
| 31 | +from azure.ai.agents.models import AzureAISearchQueryType, AzureAISearchTool, ListSortOrder, MessageRole |
| 32 | +from azure.identity import DefaultAzureCredential |
| 33 | + |
| 34 | +# Define the Azure AI Search connection ID (replace with your actual connection ID) |
| 35 | +azure_ai_conn_id = os.environ["AZURE_AI_CONNECTION_ID"] |
| 36 | + |
| 37 | +# Initialize the Azure AI Search tool with the required parameters |
| 38 | +ai_search = AzureAISearchTool( |
| 39 | + index_connection_id=azure_ai_conn_id, # Connection ID for the Azure AI Search index |
| 40 | + index_name="sample_index", # Name of the search index |
| 41 | + query_type=AzureAISearchQueryType.SIMPLE, # Query type (e.g., SIMPLE, FULL) |
| 42 | + top_k=3, # Number of top results to retrieve |
| 43 | + filter="" # Optional filter for search results |
| 44 | +) |
| 45 | + |
| 46 | +# Define the endpoint and model deployment name (replace with your actual values) |
| 47 | +endpoint = os.environ["PROJECT_ENDPOINT"], |
| 48 | +model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] |
| 49 | + |
| 50 | +# Create an AIProjectClient instance to interact with the Azure AI service |
| 51 | +with AIProjectClient( |
| 52 | + endpoint=endpoint, # Azure AI service endpoint |
| 53 | + credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Authentication credentials |
| 54 | +) as project_client: |
| 55 | + # Access the agents client from the project client |
| 56 | + agents_client = project_client.agents |
| 57 | + |
| 58 | + # Create an agent with the specified model, name, instructions, and tools |
| 59 | + agent = agents_client.create_agent( |
| 60 | + model=model_deployment_name, # Model deployment name |
| 61 | + name="my-agent", # Name of the agent |
| 62 | + instructions="You are a helpful agent", # Instructions for the agent |
| 63 | + tools=ai_search.definitions, # Tools available to the agent |
| 64 | + tool_resources=ai_search.resources, # Resources for the tools |
| 65 | + ) |
| 66 | + # [END create_agent_with_azure_ai_search_tool] |
| 67 | + print(f"Created agent, ID: {agent.id}") |
| 68 | + |
| 69 | + # Create a thread for communication with the agent |
| 70 | + thread = agents_client.create_thread() |
| 71 | + print(f"Created thread, ID: {thread.id}") |
| 72 | + |
| 73 | + # Create a message in the thread to interact with the agent |
| 74 | + message = agents_client.create_message( |
| 75 | + thread_id=thread.id, # ID of the thread |
| 76 | + role="user", # Role of the message sender (e.g., user) |
| 77 | + content="What is the temperature rating of the cozynights sleeping bag?", # Message content |
| 78 | + ) |
| 79 | + print(f"Created message, ID: {message.id}") |
| 80 | + |
| 81 | + # Create and process an agent run in the thread using the tools |
| 82 | + run = agents_client.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 83 | + print(f"Run finished with status: {run.status}") |
| 84 | + |
| 85 | + # Check if the run failed and log the error if applicable |
| 86 | + if run.status == "failed": |
| 87 | + print(f"Run failed: {run.last_error}") |
| 88 | + |
| 89 | + # Fetch and log the details of the agent run steps |
| 90 | + run_steps = agents_client.list_run_steps(thread_id=thread.id, run_id=run.id) |
| 91 | + for step in run_steps.data: |
| 92 | + print(f"Step {step['id']} status: {step['status']}") |
| 93 | + step_details = step.get("step_details", {}) |
| 94 | + tool_calls = step_details.get("tool_calls", []) |
| 95 | + |
| 96 | + # Log details of tool calls if available |
| 97 | + if tool_calls: |
| 98 | + print(" Tool calls:") |
| 99 | + for call in tool_calls: |
| 100 | + print(f" Tool Call ID: {call.get('id')}") |
| 101 | + print(f" Type: {call.get('type')}") |
| 102 | + |
| 103 | + azure_ai_search_details = call.get("azure_ai_search", {}) |
| 104 | + if azure_ai_search_details: |
| 105 | + print(f" azure_ai_search input: {azure_ai_search_details.get('input')}") |
| 106 | + print(f" azure_ai_search output: {azure_ai_search_details.get('output')}") |
| 107 | + print() # Add an extra newline between steps |
| 108 | + |
| 109 | + # Delete the agent when done to clean up resources |
| 110 | + agents_client.delete_agent(agent.id) |
| 111 | + print("Deleted agent") |
| 112 | + |
| 113 | + # [START populate_references_agent_with_azure_ai_search_tool] |
| 114 | + # Fetch and log all messages in the thread |
| 115 | + messages = agents_client.list_messages(thread_id=thread.id, order=ListSortOrder.ASCENDING) |
| 116 | + for message in messages.data: |
| 117 | + # Log agent messages with URL citation annotations |
| 118 | + if message.role == MessageRole.AGENT and message.url_citation_annotations: |
| 119 | + placeholder_annotations = { |
| 120 | + annotation.text: f" [see {annotation.url_citation.title}] ({annotation.url_citation.url})" |
| 121 | + for annotation in message.url_citation_annotations |
| 122 | + } |
| 123 | + for message_text in message.text_messages: |
| 124 | + message_str = message_text.text.value |
| 125 | + for k, v in placeholder_annotations.items(): |
| 126 | + message_str = message_str.replace(k, v) |
| 127 | + print(f"{message.role}: {message_str}") |
| 128 | + else: |
| 129 | + # Log other messages without annotations |
| 130 | + for message_text in message.text_messages: |
| 131 | + print(f"{message.role}: {message_text.text.value}") |
| 132 | + # [END populate_references_agent_with_azure_ai_search_tool] |
0 commit comments