|
| 1 | +# pylint: disable=line-too-long,useless-suppression |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | + |
| 7 | +""" |
| 8 | +DESCRIPTION: |
| 9 | + This sample demonstrates how to create an AI agent with Bing Custom Search capabilities |
| 10 | + using the BingCustomSearchAgentTool and synchronous Azure AI Projects client. The agent can search |
| 11 | + custom search instances and provide responses with relevant results. |
| 12 | +
|
| 13 | +USAGE: |
| 14 | + python sample_agent_bing_custom_search.py |
| 15 | +
|
| 16 | + Before running the sample: |
| 17 | +
|
| 18 | + pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv |
| 19 | +
|
| 20 | + Set these environment variables with your own values: |
| 21 | + 1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview |
| 22 | + page of your Microsoft Foundry portal. |
| 23 | + 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in |
| 24 | + the "Models + endpoints" tab in your Microsoft Foundry project. |
| 25 | + 3) BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID - The Bing Custom Search project connection ID, |
| 26 | + as found in the "Connections" tab in your Microsoft Foundry project. |
| 27 | + 4) BING_CUSTOM_SEARCH_INSTANCE_NAME - The Bing Custom Search instance name |
| 28 | +""" |
| 29 | + |
| 30 | +import os |
| 31 | +from dotenv import load_dotenv |
| 32 | +from azure.identity import DefaultAzureCredential |
| 33 | +from azure.ai.projects import AIProjectClient |
| 34 | +from azure.ai.projects.models import ( |
| 35 | + PromptAgentDefinition, |
| 36 | + BingCustomSearchAgentTool, |
| 37 | + BingCustomSearchToolParameters, |
| 38 | + BingCustomSearchConfiguration, |
| 39 | +) |
| 40 | + |
| 41 | +load_dotenv() |
| 42 | + |
| 43 | +project_client = AIProjectClient( |
| 44 | + endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], |
| 45 | + credential=DefaultAzureCredential(), |
| 46 | +) |
| 47 | + |
| 48 | +# Get the OpenAI client for responses |
| 49 | +openai_client = project_client.get_openai_client() |
| 50 | + |
| 51 | +bing_custom_search_tool = BingCustomSearchAgentTool( |
| 52 | + bing_custom_search_preview=BingCustomSearchToolParameters( |
| 53 | + search_configurations=[ |
| 54 | + BingCustomSearchConfiguration( |
| 55 | + project_connection_id=os.environ["BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID"], |
| 56 | + instance_name=os.environ["BING_CUSTOM_SEARCH_INSTANCE_NAME"], |
| 57 | + ) |
| 58 | + ] |
| 59 | + ) |
| 60 | +) |
| 61 | + |
| 62 | +with project_client: |
| 63 | + agent = project_client.agents.create_version( |
| 64 | + agent_name="MyAgent", |
| 65 | + definition=PromptAgentDefinition( |
| 66 | + model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], |
| 67 | + instructions="""You are a helpful agent that can use Bing Custom Search tools to assist users. |
| 68 | + Use the available Bing Custom Search tools to answer questions and perform tasks.""", |
| 69 | + tools=[bing_custom_search_tool], |
| 70 | + ), |
| 71 | + ) |
| 72 | + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") |
| 73 | + |
| 74 | + user_input = input( |
| 75 | + "Enter your question for the Bing Custom Search agent " "(e.g., 'Tell me more about foundry agent service'): \n" |
| 76 | + ) |
| 77 | + |
| 78 | + # Send initial request that will trigger the Bing Custom Search tool |
| 79 | + stream_response = openai_client.responses.create( |
| 80 | + stream=True, |
| 81 | + input=user_input, |
| 82 | + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, |
| 83 | + ) |
| 84 | + |
| 85 | + for event in stream_response: |
| 86 | + if event.type == "response.created": |
| 87 | + print(f"Follow-up response created with ID: {event.response.id}") |
| 88 | + elif event.type == "response.output_text.delta": |
| 89 | + print(f"Delta: {event.delta}") |
| 90 | + elif event.type == "response.text.done": |
| 91 | + print(f"\nFollow-up response done!") |
| 92 | + elif event.type == "response.output_item.done": |
| 93 | + if event.item.type == "message": |
| 94 | + item = event.item |
| 95 | + if item.content[-1].type == "output_text": |
| 96 | + text_content = item.content[-1] |
| 97 | + for annotation in text_content.annotations: |
| 98 | + if annotation.type == "url_citation": |
| 99 | + print( |
| 100 | + f"URL Citation: {annotation.url}, " |
| 101 | + f"Start index: {annotation.start_index}, " |
| 102 | + f"End index: {annotation.end_index}" |
| 103 | + ) |
| 104 | + elif event.type == "response.completed": |
| 105 | + print(f"\nFollow-up completed!") |
| 106 | + print(f"Full response: {event.response.output_text}") |
| 107 | + |
| 108 | + print("Cleaning up...") |
| 109 | + project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) |
| 110 | + print("Agent deleted") |
0 commit comments