From bcc8866d271363dda1c41fbaffc0a192f3d40e4b Mon Sep 17 00:00:00 2001 From: Howie Leung Date: Sat, 8 Nov 2025 22:30:06 -0800 Subject: [PATCH 1/2] Samples for ai search and fabric --- sdk/ai/azure-ai-projects/.env.template | 4 +- .../agents/tools/sample_agent_ai_search.py | 106 ++++++++++++++++++ .../agents/tools/sample_agent_fabric.py | 81 +++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py create mode 100644 sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py diff --git a/sdk/ai/azure-ai-projects/.env.template b/sdk/ai/azure-ai-projects/.env.template index 5787b379680b..d50436dd4a57 100644 --- a/sdk/ai/azure-ai-projects/.env.template +++ b/sdk/ai/azure-ai-projects/.env.template @@ -40,7 +40,9 @@ AZURE_AI_PROJECTS_TESTS_CONTAINER_INGRESS_SUBDOMAIN_SUFFIX= # Used in tools BING_PROJECT_CONNECTION_ID= MCP_PROJECT_CONNECTION_ID= - +FABRIC_PROJECT_CONNECTION_ID= +AI_SEARCH_PROJECT_CONNECTION_ID= +AI_SEARCH_INDEX_NAME= diff --git a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py new file mode 100644 index 000000000000..7b58b23336c5 --- /dev/null +++ b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py @@ -0,0 +1,106 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +DESCRIPTION: + This sample demonstrates how to create an AI agent with Azure AI Search capabilities + using the AzureAISearchAgentTool and synchronous Azure AI Projects client. The agent can search + indexed content and provide responses with citations from search results. + +USAGE: + python sample_agent_ai_search.py + + Before running the sample: + + pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv + + Set these environment variables with your own values: + 1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview + page of your Azure AI Foundry portal. + 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in + the "Models + endpoints" tab in your Azure AI Foundry project. + 3) AI_SEARCH_PROJECT_CONNECTION_ID - The AI Search project connection ID, as found in the "Connections" tab in your Azure AI Foundry project. + 4) AI_SEARCH_INDEX_NAME - The name of the AI Search index to use for searching. +""" + +import os +from dotenv import load_dotenv +from azure.identity import DefaultAzureCredential +from azure.ai.projects import AIProjectClient +from azure.ai.projects.models import ( + AzureAISearchAgentTool, + PromptAgentDefinition, + AzureAISearchToolResource, + AISearchIndexResource, +) + +load_dotenv() + +project_client = AIProjectClient( + endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], + credential=DefaultAzureCredential(), +) + +openai_client = project_client.get_openai_client() + +with project_client: + agent = project_client.agents.create_version( + agent_name="MyAISearchAgent", + definition=PromptAgentDefinition( + model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], + 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]`.", + tools=[ + AzureAISearchAgentTool( + azure_ai_search=AzureAISearchToolResource( + indexes=[ + AISearchIndexResource( + project_connection_id=os.environ["AI_SEARCH_PROJECT_CONNECTION_ID"], + index_name=os.environ["AI_SEARCH_INDEX_NAME"], + query_type="simple", + ), + ] + ) + ) + ], + ), + description="You are a helpful agent.", + ) + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") + + user_input = input( + "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" + ) + + stream_response = openai_client.responses.create( + stream=True, + tool_choice="required", + input=user_input, + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, + ) + + for event in stream_response: + if event.type == "response.created": + print(f"Follow-up response created with ID: {event.response.id}") + elif event.type == "response.output_text.delta": + print(f"Delta: {event.delta}") + elif event.type == "response.text.done": + print(f"\nFollow-up response done!") + elif event.type == "response.output_item.done": + if event.item.type == "message": + item = event.item + if item.content[-1].type == "output_text": + text_content = item.content[-1] + for annotation in text_content.annotations: + if annotation.type == "url_citation": + print( + f"URL Citation: {annotation.url}, Start index: {annotation.start_index}, End index: {annotation.end_index}" + ) + elif event.type == "response.completed": + print(f"\nFollow-up completed!") + print(f"Full response: {event.response.output_text}") + + print("\nCleaning up...") + project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) + print("Agent deleted") diff --git a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py new file mode 100644 index 000000000000..27047c1d62e7 --- /dev/null +++ b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py @@ -0,0 +1,81 @@ +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +DESCRIPTION: + This sample demonstrates how to create an AI agent with Microsoft Fabric capabilities + using the MicrosoftFabricAgentTool and synchronous Azure AI Projects client. The agent can query + Fabric data sources and provide responses based on data analysis. + +USAGE: + python sample_agent_fabric.py + + Before running the sample: + + pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv + + Set these environment variables with your own values: + 1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview + page of your Azure AI Foundry portal. + 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in + the "Models + endpoints" tab in your Azure AI Foundry project. + 3) FABRIC_PROJECT_CONNECTION_ID - The Fabric project connection ID, as found in the "Connections" tab in your Azure AI Foundry project. +""" + +import os +from dotenv import load_dotenv +from azure.identity import DefaultAzureCredential +from azure.ai.projects import AIProjectClient +from azure.ai.projects.models import ( + PromptAgentDefinition, + MicrosoftFabricAgentTool, + FabricDataAgentToolParameters, + ToolProjectConnection, +) + +load_dotenv() + +project_client = AIProjectClient( + endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], + credential=DefaultAzureCredential(), + headers={ + "x-ms-oai-response-testenv": "tip2-preview1" # TODO: remove this line when the feature goes to production + }, +) + +openai_client = project_client.get_openai_client() + +with project_client: + agent = project_client.agents.create_version( + agent_name="MyFabricAgent4", + definition=PromptAgentDefinition( + model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], + instructions="You are a helpful assistant.", + tools=[ + MicrosoftFabricAgentTool( + fabric_dataagent_preview=FabricDataAgentToolParameters( + project_connections=[ + ToolProjectConnection(project_connection_id=os.environ["FABRIC_PROJECT_CONNECTION_ID"]) + ] + ) + ) + ], + ), + ) + print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") + + user_input = input("Enter your question for Fabric (e.g., 'Tell me about sales records'): \n") + + response = openai_client.responses.create( + tool_choice="required", + input=user_input, + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, + ) + + print(f"Response output: {response.output_text}") + + print("\nCleaning up...") + project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) + print("Agent deleted") From ef76c48402848932195968a0e4c9fc6773fe3481 Mon Sep 17 00:00:00 2001 From: Howie Leung Date: Mon, 10 Nov 2025 09:04:02 -0800 Subject: [PATCH 2/2] Resolved comments --- .../agents/tools/sample_agent_ai_search.py | 18 ++++++++++++------ .../agents/tools/sample_agent_fabric.py | 8 +++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py index 7b58b23336c5..19d95cacb3b8 100644 --- a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py +++ b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py @@ -21,7 +21,8 @@ page of your Azure AI Foundry portal. 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in the "Models + endpoints" tab in your Azure AI Foundry project. - 3) AI_SEARCH_PROJECT_CONNECTION_ID - The AI Search project connection ID, as found in the "Connections" tab in your Azure AI Foundry project. + 3) AI_SEARCH_PROJECT_CONNECTION_ID - The AI Search project connection ID, + as found in the "Connections" tab in your Azure AI Foundry project. 4) AI_SEARCH_INDEX_NAME - The name of the AI Search index to use for searching. """ @@ -34,6 +35,7 @@ PromptAgentDefinition, AzureAISearchToolResource, AISearchIndexResource, + AzureAISearchQueryType, ) load_dotenv() @@ -47,10 +49,11 @@ with project_client: agent = project_client.agents.create_version( - agent_name="MyAISearchAgent", + agent_name="MyAgent", definition=PromptAgentDefinition( model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], - 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]`.", + 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]`.""", tools=[ AzureAISearchAgentTool( azure_ai_search=AzureAISearchToolResource( @@ -58,7 +61,7 @@ AISearchIndexResource( project_connection_id=os.environ["AI_SEARCH_PROJECT_CONNECTION_ID"], index_name=os.environ["AI_SEARCH_INDEX_NAME"], - query_type="simple", + query_type=AzureAISearchQueryType.SIMPLE, ), ] ) @@ -70,7 +73,8 @@ print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})") user_input = input( - "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" + """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""" ) stream_response = openai_client.responses.create( @@ -95,7 +99,9 @@ for annotation in text_content.annotations: if annotation.type == "url_citation": print( - f"URL Citation: {annotation.url}, Start index: {annotation.start_index}, End index: {annotation.end_index}" + f"URL Citation: {annotation.url}, " + f"Start index: {annotation.start_index}, " + f"End index: {annotation.end_index}" ) elif event.type == "response.completed": print(f"\nFollow-up completed!") diff --git a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py index 27047c1d62e7..7967d0a3062e 100644 --- a/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py +++ b/sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py @@ -21,7 +21,8 @@ page of your Azure AI Foundry portal. 2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in the "Models + endpoints" tab in your Azure AI Foundry project. - 3) FABRIC_PROJECT_CONNECTION_ID - The Fabric project connection ID, as found in the "Connections" tab in your Azure AI Foundry project. + 3) FABRIC_PROJECT_CONNECTION_ID - The Fabric project connection ID, + as found in the "Connections" tab in your Azure AI Foundry project. """ import os @@ -40,16 +41,13 @@ project_client = AIProjectClient( endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=DefaultAzureCredential(), - headers={ - "x-ms-oai-response-testenv": "tip2-preview1" # TODO: remove this line when the feature goes to production - }, ) openai_client = project_client.get_openai_client() with project_client: agent = project_client.agents.create_version( - agent_name="MyFabricAgent4", + agent_name="MyAgent", definition=PromptAgentDefinition( model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"], instructions="You are a helpful assistant.",