Skip to content

Commit e370148

Browse files
authored
Howie/samples 6 (#43901)
* Samples for ai search and fabric * Resolved comments
1 parent e5496ed commit e370148

File tree

3 files changed

+194
-1
lines changed

3 files changed

+194
-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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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,
25+
as found in the "Connections" tab in your Azure AI Foundry project.
26+
4) AI_SEARCH_INDEX_NAME - The name of the AI Search index to use for searching.
27+
"""
28+
29+
import os
30+
from dotenv import load_dotenv
31+
from azure.identity import DefaultAzureCredential
32+
from azure.ai.projects import AIProjectClient
33+
from azure.ai.projects.models import (
34+
AzureAISearchAgentTool,
35+
PromptAgentDefinition,
36+
AzureAISearchToolResource,
37+
AISearchIndexResource,
38+
AzureAISearchQueryType,
39+
)
40+
41+
load_dotenv()
42+
43+
project_client = AIProjectClient(
44+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
45+
credential=DefaultAzureCredential(),
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="MyAgent",
53+
definition=PromptAgentDefinition(
54+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
55+
instructions="""You are a helpful assistant. You must always provide citations for
56+
answers using the tool and render them as: `[message_idx:search_idx†source]`.""",
57+
tools=[
58+
AzureAISearchAgentTool(
59+
azure_ai_search=AzureAISearchToolResource(
60+
indexes=[
61+
AISearchIndexResource(
62+
project_connection_id=os.environ["AI_SEARCH_PROJECT_CONNECTION_ID"],
63+
index_name=os.environ["AI_SEARCH_INDEX_NAME"],
64+
query_type=AzureAISearchQueryType.SIMPLE,
65+
),
66+
]
67+
)
68+
)
69+
],
70+
),
71+
description="You are a helpful agent.",
72+
)
73+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
74+
75+
user_input = input(
76+
"""Enter your question for the AI Search agent available in the index
77+
(e.g., 'Tell me about the mental health services available from Premera'): \n"""
78+
)
79+
80+
stream_response = openai_client.responses.create(
81+
stream=True,
82+
tool_choice="required",
83+
input=user_input,
84+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
85+
)
86+
87+
for event in stream_response:
88+
if event.type == "response.created":
89+
print(f"Follow-up response created with ID: {event.response.id}")
90+
elif event.type == "response.output_text.delta":
91+
print(f"Delta: {event.delta}")
92+
elif event.type == "response.text.done":
93+
print(f"\nFollow-up response done!")
94+
elif event.type == "response.output_item.done":
95+
if event.item.type == "message":
96+
item = event.item
97+
if item.content[-1].type == "output_text":
98+
text_content = item.content[-1]
99+
for annotation in text_content.annotations:
100+
if annotation.type == "url_citation":
101+
print(
102+
f"URL Citation: {annotation.url}, "
103+
f"Start index: {annotation.start_index}, "
104+
f"End index: {annotation.end_index}"
105+
)
106+
elif event.type == "response.completed":
107+
print(f"\nFollow-up completed!")
108+
print(f"Full response: {event.response.output_text}")
109+
110+
print("\nCleaning up...")
111+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
112+
print("Agent deleted")
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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,
25+
as found in the "Connections" tab in your Azure AI Foundry project.
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+
PromptAgentDefinition,
34+
MicrosoftFabricAgentTool,
35+
FabricDataAgentToolParameters,
36+
ToolProjectConnection,
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="MyAgent",
51+
definition=PromptAgentDefinition(
52+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
53+
instructions="You are a helpful assistant.",
54+
tools=[
55+
MicrosoftFabricAgentTool(
56+
fabric_dataagent_preview=FabricDataAgentToolParameters(
57+
project_connections=[
58+
ToolProjectConnection(project_connection_id=os.environ["FABRIC_PROJECT_CONNECTION_ID"])
59+
]
60+
)
61+
)
62+
],
63+
),
64+
)
65+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
66+
67+
user_input = input("Enter your question for Fabric (e.g., 'Tell me about sales records'): \n")
68+
69+
response = openai_client.responses.create(
70+
tool_choice="required",
71+
input=user_input,
72+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
73+
)
74+
75+
print(f"Response output: {response.output_text}")
76+
77+
print("\nCleaning up...")
78+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
79+
print("Agent deleted")

0 commit comments

Comments
 (0)