|
| 1 | +# ------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# ------------------------------------ |
| 5 | + |
| 6 | +""" |
| 7 | +DESCRIPTION: |
| 8 | + This sample demonstrates how to create an AI agent with Azure AI Search capabilities |
| 9 | + using the AzureAISearchAgentTool and synchronous Azure AI Projects client. The agent can search |
| 10 | + indexed content and provide responses with citations from search results. |
| 11 | +
|
| 12 | +USAGE: |
| 13 | + python sample_agent_ai_search.py |
| 14 | +
|
| 15 | + Before running the sample: |
| 16 | +
|
| 17 | + pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv |
| 18 | +
|
| 19 | + Set these environment variables with your own values: |
| 20 | + 1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview |
| 21 | + page of your Azure AI Foundry portal. |
| 22 | + 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 23 | + the "Models + endpoints" tab in your Azure AI Foundry project. |
| 24 | + 3) AI_SEARCH_PROJECT_CONNECTION_ID - The AI Search project connection ID, as found in the "Connections" tab in your Azure AI Foundry project. |
| 25 | + 4) AI_SEARCH_INDEX_NAME - The name of the AI Search index to use for searching. |
| 26 | +""" |
| 27 | + |
| 28 | +import os |
| 29 | +from dotenv import load_dotenv |
| 30 | +from azure.identity import DefaultAzureCredential |
| 31 | +from azure.ai.projects import AIProjectClient |
| 32 | +from azure.ai.projects.models import ( |
| 33 | + AzureAISearchAgentTool, |
| 34 | + PromptAgentDefinition, |
| 35 | + AzureAISearchToolResource, |
| 36 | + AISearchIndexResource, |
| 37 | +) |
| 38 | + |
| 39 | +load_dotenv() |
| 40 | + |
| 41 | +project_client = AIProjectClient( |
| 42 | + endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], |
| 43 | + credential=DefaultAzureCredential(), |
| 44 | +) |
| 45 | + |
| 46 | +openai_client = project_client.get_openai_client() |
| 47 | + |
| 48 | +with project_client: |
| 49 | + agent = project_client.agents.create_version( |
| 50 | + agent_name="MyAISearchAgent", |
| 51 | + definition=PromptAgentDefinition( |
| 52 | + model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], |
| 53 | + instructions="You are a helpful assistant. You must always provide citations for answers using the tool and render them as: `【message_idx:search_idx†source】`.", |
| 54 | + tools=[ |
| 55 | + AzureAISearchAgentTool( |
| 56 | + azure_ai_search=AzureAISearchToolResource( |
| 57 | + indexes=[ |
| 58 | + AISearchIndexResource( |
| 59 | + project_connection_id=os.environ["AI_SEARCH_PROJECT_CONNECTION_ID"], |
| 60 | + index_name=os.environ["AI_SEARCH_INDEX_NAME"], |
| 61 | + query_type="simple", |
| 62 | + ), |
| 63 | + ] |
| 64 | + ) |
| 65 | + ) |
| 66 | + ], |
| 67 | + ), |
| 68 | + description="You are a helpful agent.", |
| 69 | + ) |
| 70 | + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") |
| 71 | + |
| 72 | + user_input = input( |
| 73 | + "Enter your question for the AI Search agent available in the index (e.g., 'Tell me about the mental health services available from Premera'): \n" |
| 74 | + ) |
| 75 | + |
| 76 | + stream_response = openai_client.responses.create( |
| 77 | + stream=True, |
| 78 | + tool_choice="required", |
| 79 | + input=user_input, |
| 80 | + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, |
| 81 | + ) |
| 82 | + |
| 83 | + for event in stream_response: |
| 84 | + if event.type == "response.created": |
| 85 | + print(f"Follow-up response created with ID: {event.response.id}") |
| 86 | + elif event.type == "response.output_text.delta": |
| 87 | + print(f"Delta: {event.delta}") |
| 88 | + elif event.type == "response.text.done": |
| 89 | + print(f"\nFollow-up response done!") |
| 90 | + elif event.type == "response.output_item.done": |
| 91 | + if event.item.type == "message": |
| 92 | + item = event.item |
| 93 | + if item.content[-1].type == "output_text": |
| 94 | + text_content = item.content[-1] |
| 95 | + for annotation in text_content.annotations: |
| 96 | + if annotation.type == "url_citation": |
| 97 | + print( |
| 98 | + f"URL Citation: {annotation.url}, Start index: {annotation.start_index}, End index: {annotation.end_index}" |
| 99 | + ) |
| 100 | + elif event.type == "response.completed": |
| 101 | + print(f"\nFollow-up completed!") |
| 102 | + print(f"Full response: {event.response.output_text}") |
| 103 | + |
| 104 | + print("\nCleaning up...") |
| 105 | + project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) |
| 106 | + print("Agent deleted") |
0 commit comments