-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Howie/samples 6 #43901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
howieleung
merged 2 commits into
feature/azure-ai-projects/2.0.0b1
from
howie/samples-6
Nov 10, 2025
Merged
Howie/samples 6 #43901
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]`.", | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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", | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ), | ||
| ] | ||
| ) | ||
| ) | ||
| ], | ||
| ), | ||
| 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" | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| 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") | ||
81 changes: 81 additions & 0 deletions
81
sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| 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 | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| ) | ||
|
|
||
| openai_client = project_client.get_openai_client() | ||
|
|
||
| with project_client: | ||
| agent = project_client.agents.create_version( | ||
| agent_name="MyFabricAgent4", | ||
howieleung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.