diff --git a/python/samples/getting_started/agents/azure_ai_agent/README.md b/python/samples/getting_started/agents/azure_ai_agent/README.md index c29c068f37..84ed7eeba3 100644 --- a/python/samples/getting_started/agents/azure_ai_agent/README.md +++ b/python/samples/getting_started/agents/azure_ai_agent/README.md @@ -9,6 +9,7 @@ This folder contains examples demonstrating different ways to create and use age | [`azure_ai_basic.py`](azure_ai_basic.py) | The simplest way to create an agent using `ChatAgent` with `AzureAIAgentClient`. It automatically handles all configuration using environment variables. | | [`azure_ai_with_bing_custom_search.py`](azure_ai_with_bing_custom_search.py) | Shows how to use Bing Custom Search with Azure AI agents to find real-time information from the web using custom search configurations. Demonstrates how to set up and use HostedWebSearchTool with custom search instances. | | [`azure_ai_with_bing_grounding.py`](azure_ai_with_bing_grounding.py) | Shows how to use Bing Grounding search with Azure AI agents to find real-time information from the web. Demonstrates web search capabilities with proper source citations and comprehensive error handling. | +| [`azure_ai_with_bing_grounding_citations.py`](azure_ai_with_bing_grounding_citations.py) | Demonstrates how to extract and display citations from Bing Grounding search responses. Shows how to collect citation annotations (title, URL, snippet) during streaming responses, enabling users to verify sources and access referenced content. | | [`azure_ai_with_code_interpreter_file_generation.py`](azure_ai_with_code_interpreter_file_generation.py) | Shows how to retrieve file IDs from code interpreter generated files using both streaming and non-streaming approaches. | | [`azure_ai_with_code_interpreter.py`](azure_ai_with_code_interpreter.py) | Shows how to use the HostedCodeInterpreterTool with Azure AI agents to write and execute Python code. Includes helper methods for accessing code interpreter data from response chunks. | | [`azure_ai_with_existing_agent.py`](azure_ai_with_existing_agent.py) | Shows how to work with a pre-existing agent by providing the agent ID to the Azure AI chat client. This example also demonstrates proper cleanup of manually created agents. | diff --git a/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_grounding_citations.py b/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_grounding_citations.py new file mode 100644 index 0000000000..63245a4d12 --- /dev/null +++ b/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_grounding_citations.py @@ -0,0 +1,86 @@ +# Copyright (c) Microsoft. All rights reserved. + +import asyncio + +from agent_framework import ChatAgent, CitationAnnotation, HostedWebSearchTool +from agent_framework.azure import AzureAIAgentClient +from azure.identity.aio import AzureCliCredential + +""" +This sample demonstrates how to create an Azure AI agent that uses Bing Grounding +search to find real-time information from the web with comprehensive citation support. +It shows how to extract and display citations (title, URL, and snippet) from Bing +Grounding responses, enabling users to verify sources and explore referenced content. + +Prerequisites: +1. A connected Grounding with Bing Search resource in your Azure AI project +2. Set BING_CONNECTION_ID environment variable + Example: BING_CONNECTION_ID="your-bing-connection-id" + +To set up Bing Grounding: +1. Go to Azure AI Foundry portal (https://ai.azure.com) +2. Navigate to your project's "Connected resources" section +3. Add a new connection for "Grounding with Bing Search" +4. Copy the connection ID and set the BING_CONNECTION_ID environment variable +""" + + +async def main() -> None: + """Main function demonstrating Azure AI agent with Bing Grounding search.""" + # 1. Create Bing Grounding search tool using HostedWebSearchTool + # The connection ID will be automatically picked up from environment variable + bing_search_tool = HostedWebSearchTool( + name="Bing Grounding Search", + description="Search the web for current information using Bing", + ) + + # 2. Use AzureAIAgentClient as async context manager for automatic cleanup + async with ( + AzureAIAgentClient(credential=AzureCliCredential()) as client, + ChatAgent( + chat_client=client, + name="BingSearchAgent", + instructions=( + "You are a helpful assistant that can search the web for current information. " + "Use the Bing search tool to find up-to-date information and provide accurate, " + "well-sourced answers. Always cite your sources when possible." + ), + tools=bing_search_tool, + ) as agent, + ): + # 3. Demonstrate agent capabilities with web search + print("=== Azure AI Agent with Bing Grounding Search ===\n") + + user_input = "What is the most popular programming language?" + print(f"User: {user_input}") + print("Agent: ", end="", flush=True) + + # Stream the response and collect citations + citations: list[CitationAnnotation] = [] + async for chunk in agent.run_stream(user_input): + if chunk.text: + print(chunk.text, end="", flush=True) + + # Collect citations from Bing Grounding responses + for content in getattr(chunk, "contents", []): + annotations = getattr(content, "annotations", []) + if annotations: + citations.extend(annotations) + + print() + + # Display collected citations + if citations: + print("\n\nCitations:") + for i, citation in enumerate(citations, 1): + print(f"[{i}] {citation.title}: {citation.url}") + if citation.snippet: + print(f" Snippet: {citation.snippet}") + else: + print("\nNo citations found in the response.") + + print() + + +if __name__ == "__main__": + asyncio.run(main())