-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathbing-python.py
More file actions
86 lines (65 loc) · 2.89 KB
/
bing-python.py
File metadata and controls
86 lines (65 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
"""
FILE: sample_agents_bing_grounding.py
DESCRIPTION:
This sample demonstrates how to use agent operations with the Grounding with Bing Search tool from
the Azure Agents service using a synchronous client.
USAGE:
python sample_agents_bing_grounding.py
Before running the sample:
pip install azure.ai.projects azure-identity
Set this environment variables with your own values:
PROJECT_CONNECTION_STRING - the Azure AI Project connection string, as found in your AI Studio Project.
BING_CONNECTION_NAME - the name of the connection of Grounding with Bing Search
"""
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.projects import BingGroundingTool
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
# connection string should be copied from the project in AI Studio
project_client = AIProjectClient.from_connection_string(
credential=DefaultAzureCredential(),
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
)
bing_connection = project_client.connections.get(connection_name=os.environ["CONNECTION_NAME"])
conn_id = bing_connection.id
print(conn_id)
# Initialize agent bing tool and add the connection id
bing = BingGroundingTool(connection_id=conn_id)
# Create agent with the bing tool and process assistant run
with project_client:
agent = project_client.agents.create_agent(
model="gpt-4o",
name="my-assistant",
instructions="You are a helpful assistant",
tools=bing.definitions,
headers={"x-ms-enable-preview": "true"},
)
print(f"Created agent, ID: {agent.id}")
# Create thread for communication
thread = project_client.agents.create_thread()
print(f"Created thread, ID: {thread.id}")
# Create message to thread
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
content="How is the weather in Seattle today?",
)
print(f"Created message, ID: {message.id}")
# Create and process agent run in thread with tools
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
print(f"Run finished with status: {run.status}")
if run.status == "failed":
print(f"Run failed: {run.last_error}")
# Delete the assistant when done
project_client.agents.delete_agent(agent.id)
print("Deleted agent")
# Fetch and log all messages
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")