Skip to content

Commit 0bc6951

Browse files
authored
Howie/samples 8 (#43949)
* bing custom search and sharepoint * break line * break line
1 parent ce6c8d5 commit 0bc6951

File tree

3 files changed

+214
-2
lines changed

3 files changed

+214
-2
lines changed

sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
7474

7575
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"""
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"
7878
)
7979

8080
stream_response = openai_client.responses.create(
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# pylint: disable=line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to create an AI agent with Bing Custom Search capabilities
10+
using the BingCustomSearchAgentTool and synchronous Azure AI Projects client. The agent can search
11+
custom search instances and provide responses with relevant results.
12+
13+
USAGE:
14+
python sample_agent_bing_custom_search.py
15+
16+
Before running the sample:
17+
18+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
19+
20+
Set these environment variables with your own values:
21+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
22+
page of your Microsoft Foundry portal.
23+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
24+
the "Models + endpoints" tab in your Microsoft Foundry project.
25+
3) BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID - The Bing Custom Search project connection ID,
26+
as found in the "Connections" tab in your Microsoft Foundry project.
27+
4) BING_CUSTOM_SEARCH_INSTANCE_NAME - The Bing Custom Search instance name
28+
"""
29+
30+
import os
31+
from dotenv import load_dotenv
32+
from azure.identity import DefaultAzureCredential
33+
from azure.ai.projects import AIProjectClient
34+
from azure.ai.projects.models import (
35+
PromptAgentDefinition,
36+
BingCustomSearchAgentTool,
37+
BingCustomSearchToolParameters,
38+
BingCustomSearchConfiguration,
39+
)
40+
41+
load_dotenv()
42+
43+
project_client = AIProjectClient(
44+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
45+
credential=DefaultAzureCredential(),
46+
)
47+
48+
# Get the OpenAI client for responses
49+
openai_client = project_client.get_openai_client()
50+
51+
bing_custom_search_tool = BingCustomSearchAgentTool(
52+
bing_custom_search_preview=BingCustomSearchToolParameters(
53+
search_configurations=[
54+
BingCustomSearchConfiguration(
55+
project_connection_id=os.environ["BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID"],
56+
instance_name=os.environ["BING_CUSTOM_SEARCH_INSTANCE_NAME"],
57+
)
58+
]
59+
)
60+
)
61+
62+
with project_client:
63+
agent = project_client.agents.create_version(
64+
agent_name="MyAgent",
65+
definition=PromptAgentDefinition(
66+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
67+
instructions="""You are a helpful agent that can use Bing Custom Search tools to assist users.
68+
Use the available Bing Custom Search tools to answer questions and perform tasks.""",
69+
tools=[bing_custom_search_tool],
70+
),
71+
)
72+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
73+
74+
user_input = input(
75+
"Enter your question for the Bing Custom Search agent " "(e.g., 'Tell me more about foundry agent service'): \n"
76+
)
77+
78+
# Send initial request that will trigger the Bing Custom Search tool
79+
stream_response = openai_client.responses.create(
80+
stream=True,
81+
input=user_input,
82+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
83+
)
84+
85+
for event in stream_response:
86+
if event.type == "response.created":
87+
print(f"Follow-up response created with ID: {event.response.id}")
88+
elif event.type == "response.output_text.delta":
89+
print(f"Delta: {event.delta}")
90+
elif event.type == "response.text.done":
91+
print(f"\nFollow-up response done!")
92+
elif event.type == "response.output_item.done":
93+
if event.item.type == "message":
94+
item = event.item
95+
if item.content[-1].type == "output_text":
96+
text_content = item.content[-1]
97+
for annotation in text_content.annotations:
98+
if annotation.type == "url_citation":
99+
print(
100+
f"URL Citation: {annotation.url}, "
101+
f"Start index: {annotation.start_index}, "
102+
f"End index: {annotation.end_index}"
103+
)
104+
elif event.type == "response.completed":
105+
print(f"\nFollow-up completed!")
106+
print(f"Full response: {event.response.output_text}")
107+
108+
print("Cleaning up...")
109+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
110+
print("Agent deleted")
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# pylint: disable=line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to create an AI agent with SharePoint capabilities
10+
using the SharepointAgentTool and synchronous Azure AI Projects client. The agent can search
11+
SharePoint content and provide responses with relevant information from SharePoint sites.
12+
13+
USAGE:
14+
python sample_agent_sharepoint.py
15+
16+
Before running the sample:
17+
18+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
19+
20+
Set these environment variables with your own values:
21+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
22+
page of your Microsoft Foundry portal.
23+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
24+
the "Models + endpoints" tab in your Microsoft Foundry project.
25+
3) SHAREPOINT_PROJECT_CONNECTION_ID - The SharePoint project connection ID,
26+
as found in the "Connections" tab in your Microsoft Foundry project.
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+
PromptAgentDefinition,
35+
SharepointAgentTool,
36+
SharepointGroundingToolParameters,
37+
ToolProjectConnection,
38+
)
39+
40+
load_dotenv()
41+
42+
project_client = AIProjectClient(
43+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
44+
credential=DefaultAzureCredential(),
45+
)
46+
47+
# Get the OpenAI client for responses
48+
openai_client = project_client.get_openai_client()
49+
50+
sharepoint_tool = SharepointAgentTool(
51+
sharepoint_grounding_preview=SharepointGroundingToolParameters(
52+
project_connections=[
53+
ToolProjectConnection(project_connection_id=os.environ["SHAREPOINT_PROJECT_CONNECTION_ID"])
54+
]
55+
)
56+
)
57+
58+
with project_client:
59+
agent = project_client.agents.create_version(
60+
agent_name="MyAgent",
61+
definition=PromptAgentDefinition(
62+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
63+
instructions="""You are a helpful agent that can use SharePoint tools to assist users.
64+
Use the available SharePoint tools to answer questions and perform tasks.""",
65+
tools=[sharepoint_tool],
66+
),
67+
)
68+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
69+
70+
# Send initial request that will trigger the SharePoint tool
71+
stream_response = openai_client.responses.create(
72+
stream=True,
73+
input="Please summarize the last meeting notes stored in SharePoint.",
74+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
75+
)
76+
77+
for event in stream_response:
78+
if event.type == "response.created":
79+
print(f"Follow-up response created with ID: {event.response.id}")
80+
elif event.type == "response.output_text.delta":
81+
print(f"Delta: {event.delta}")
82+
elif event.type == "response.text.done":
83+
print(f"\nFollow-up response done!")
84+
elif event.type == "response.output_item.done":
85+
if event.item.type == "message":
86+
item = event.item
87+
if item.content[-1].type == "output_text":
88+
text_content = item.content[-1]
89+
for annotation in text_content.annotations:
90+
if annotation.type == "url_citation":
91+
print(
92+
f"URL Citation: {annotation.url}, "
93+
f"Start index: {annotation.start_index}, "
94+
f"End index: {annotation.end_index}"
95+
)
96+
elif event.type == "response.completed":
97+
print(f"\nFollow-up completed!")
98+
print(f"Full response: {event.response.output_text}")
99+
100+
print("Cleaning up...")
101+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
102+
print("Agent deleted")

0 commit comments

Comments
 (0)