Skip to content

Commit b21cac1

Browse files
committed
Samples for ai search and fabric
1 parent 14c1d13 commit b21cac1

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed

sdk/ai/azure-ai-projects/.env.template

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ AZURE_AI_PROJECTS_TESTS_CONTAINER_INGRESS_SUBDOMAIN_SUFFIX=
4040
# Used in tools
4141
BING_PROJECT_CONNECTION_ID=
4242
MCP_PROJECT_CONNECTION_ID=
43-
43+
FABRIC_PROJECT_CONNECTION_ID=
44+
AI_SEARCH_PROJECT_CONNECTION_ID=
45+
AI_SEARCH_INDEX_NAME=
4446

4547

4648

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with Azure AI Search capabilities
9+
using the AzureAISearchAgentTool and synchronous Azure AI Projects client. The agent can search
10+
indexed content and provide responses with citations from search results.
11+
12+
USAGE:
13+
python sample_agent_ai_search.py
14+
15+
Before running the sample:
16+
17+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
18+
19+
Set these environment variables with your own values:
20+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
page of your Azure AI Foundry portal.
22+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Azure AI Foundry project.
24+
3) AI_SEARCH_PROJECT_CONNECTION_ID - The AI Search project connection ID, as found in the "Connections" tab in your Azure AI Foundry project.
25+
4) AI_SEARCH_INDEX_NAME - The name of the AI Search index to use for searching.
26+
"""
27+
28+
import os
29+
from dotenv import load_dotenv
30+
from azure.identity import DefaultAzureCredential
31+
from azure.ai.projects import AIProjectClient
32+
from azure.ai.projects.models import (
33+
AzureAISearchAgentTool,
34+
PromptAgentDefinition,
35+
AzureAISearchToolResource,
36+
AISearchIndexResource,
37+
)
38+
39+
load_dotenv()
40+
41+
project_client = AIProjectClient(
42+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
43+
credential=DefaultAzureCredential(),
44+
)
45+
46+
openai_client = project_client.get_openai_client()
47+
48+
with project_client:
49+
agent = project_client.agents.create_version(
50+
agent_name="MyAISearchAgent",
51+
definition=PromptAgentDefinition(
52+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
53+
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】`.",
54+
tools=[
55+
AzureAISearchAgentTool(
56+
azure_ai_search=AzureAISearchToolResource(
57+
indexes=[
58+
AISearchIndexResource(
59+
project_connection_id=os.environ["AI_SEARCH_PROJECT_CONNECTION_ID"],
60+
index_name=os.environ["AI_SEARCH_INDEX_NAME"],
61+
query_type="simple",
62+
),
63+
]
64+
)
65+
)
66+
],
67+
),
68+
description="You are a helpful agent.",
69+
)
70+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
71+
72+
user_input = input(
73+
"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"
74+
)
75+
76+
stream_response = openai_client.responses.create(
77+
stream=True,
78+
tool_choice="required",
79+
input=user_input,
80+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
81+
)
82+
83+
for event in stream_response:
84+
if event.type == "response.created":
85+
print(f"Follow-up response created with ID: {event.response.id}")
86+
elif event.type == "response.output_text.delta":
87+
print(f"Delta: {event.delta}")
88+
elif event.type == "response.text.done":
89+
print(f"\nFollow-up response done!")
90+
elif event.type == "response.output_item.done":
91+
if event.item.type == "message":
92+
item = event.item
93+
if item.content[-1].type == "output_text":
94+
text_content = item.content[-1]
95+
for annotation in text_content.annotations:
96+
if annotation.type == "url_citation":
97+
print(
98+
f"URL Citation: {annotation.url}, Start index: {annotation.start_index}, End index: {annotation.end_index}"
99+
)
100+
elif event.type == "response.completed":
101+
print(f"\nFollow-up completed!")
102+
print(f"Full response: {event.response.output_text}")
103+
104+
print("\nCleaning up...")
105+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
106+
print("Agent deleted")
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with Microsoft Fabric capabilities
9+
using the MicrosoftFabricAgentTool and synchronous Azure AI Projects client. The agent can query
10+
Fabric data sources and provide responses based on data analysis.
11+
12+
USAGE:
13+
python sample_agent_fabric.py
14+
15+
Before running the sample:
16+
17+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
18+
19+
Set these environment variables with your own values:
20+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
page of your Azure AI Foundry portal.
22+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Azure AI Foundry project.
24+
3) FABRIC_PROJECT_CONNECTION_ID - The Fabric project connection ID, as found in the "Connections" tab in your Azure AI Foundry project.
25+
"""
26+
27+
import os
28+
from dotenv import load_dotenv
29+
from azure.identity import DefaultAzureCredential
30+
from azure.ai.projects import AIProjectClient
31+
from azure.ai.projects.models import (
32+
PromptAgentDefinition,
33+
MicrosoftFabricAgentTool,
34+
FabricDataAgentToolParameters,
35+
ToolProjectConnection,
36+
)
37+
38+
load_dotenv()
39+
40+
project_client = AIProjectClient(
41+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
42+
credential=DefaultAzureCredential(),
43+
headers={
44+
"x-ms-oai-response-testenv": "tip2-preview1" # TODO: remove this line when the feature goes to production
45+
},
46+
)
47+
48+
openai_client = project_client.get_openai_client()
49+
50+
with project_client:
51+
agent = project_client.agents.create_version(
52+
agent_name="MyFabricAgent4",
53+
definition=PromptAgentDefinition(
54+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
55+
instructions="You are a helpful assistant.",
56+
tools=[
57+
MicrosoftFabricAgentTool(
58+
fabric_dataagent_preview=FabricDataAgentToolParameters(
59+
project_connections=[
60+
ToolProjectConnection(project_connection_id=os.environ["FABRIC_PROJECT_CONNECTION_ID"])
61+
]
62+
)
63+
)
64+
],
65+
),
66+
)
67+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
68+
69+
user_input = input("Enter your question for Fabric (e.g., 'Tell me about sales records'): \n")
70+
71+
response = openai_client.responses.create(
72+
tool_choice="required",
73+
input=user_input,
74+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
75+
)
76+
77+
print(f"Response output: {response.output_text}")
78+
79+
print("\nCleaning up...")
80+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
81+
print("Agent deleted")

0 commit comments

Comments
 (0)