Skip to content

Python: Citations for Foundry Hosted Agents + Agent Framework #2789

@sandeepvootoori

Description

@sandeepvootoori

Hello,

We are working on creating hosted agents and using Microsoft Agent Framework, I am unable to extract the citations from Agent Responses. Is there an example for this pattern where we use custom search tool or bing tool and pass the citations back to the user?

import os
from agent_framework import ChatAgent, HostedWebSearchTool
from agent_framework_azure_ai import AzureAIAgentClient
from azure.ai.agentserver.agentframework import from_agent_framework
from azure.identity.aio import DefaultAzureCredential
from dotenv import load_dotenv

load_dotenv()

def get_agent() -> ChatAgent:
    """Create and return a ChatAgent with Bing Grounding search tool."""
    assert "AZURE_AI_PROJECT_ENDPOINT" in os.environ, (
        "AZURE_AI_PROJECT_ENDPOINT environment variable must be set."
    )
    assert "AZURE_AI_MODEL_DEPLOYMENT_NAME" in os.environ, (
        "AZURE_AI_MODEL_DEPLOYMENT_NAME environment variable must be set."
    )
    assert "BING_GROUNDING_CONNECTION_ID" in os.environ, (
        "BING_GROUNDING_CONNECTION_ID environment variable must be set to use HostedWebSearchTool."
    )
    
    chat_client = AzureAIAgentClient(
        endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
        async_credential=DefaultAzureCredential( exclude_environment_credential=True,
                                                exclude_interactive_browser_credential=True,
                                                exclude_visual_studio_credential=True,
                                                exclude_powershell_credential=False,
                                                exclude_shared_token_cache_credential=True,
                                                exclude_visual_studio_code_credential=True,
                                                exclude_cli_credential=True),
    )

    # Create ChatAgent with Bing Grounding tool
    agent = ChatAgent(
        chat_client=chat_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=[{
            "type": "bing_grounding",
            "bing_grounding": {  
                "search_configurations": [  
                    {  
                        "connection_id": os.environ["BING_GROUNDING_CONNECTION_ID"],  
                    }  
                ]  
            }
        }]
    )
    return agent

if __name__ == "__main__":
    # For local debugging with dev UI
    DEBUG_MODE = os.getenv("DEBUG_MODE", "false").lower() == "true"
    
    if DEBUG_MODE:
        from agent_framework.devui import serve
        agent = get_agent()
        serve(entities=[agent], auto_open=True)
        # → Opens browser to http://localhost:8080
    else:
        from_agent_framework(get_agent()).run()Í```

Metadata

Metadata

Assignees

Labels

agentsIssues related to single agentspython

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions