Skip to content

Commit 7d3f911

Browse files
committed
Use agents SDK
1 parent 6595fdf commit 7d3f911

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

app/backend/approaches/approach.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from openai.types.chat import ChatCompletionMessageParam
2626

2727
from approaches.promptmanager import PromptManager
28-
from app.backend.search_client import WebPage
28+
from search_client import WebPage
2929
from core.authentication import AuthenticationHelper
3030

3131

app/backend/requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ python-dotenv
3333
prompty
3434
rich
3535
typing-extensions
36-
langchain-community
36+
azure-ai-projects

app/backend/search_client.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
import httpx
88
from pydantic import BaseModel, ConfigDict
9+
from azure.ai.projects.aio import AIProjectClient
10+
from azure.identity import DefaultAzureCredential
11+
from azure.ai.projects.models import BingGroundingTool
912

1013

14+
BING_CONNECTION_NAME = 'antbingtesting'
15+
1116
class WebPage(BaseModel):
1217
id: str
1318
name: str
@@ -37,12 +42,67 @@ class WebAnswer(BaseModel):
3742

3843

3944
class AsyncGroundingSearchClient:
45+
project_client: AIProjectClient
46+
bing_tool: BingGroundingTool
47+
agent_id: str = "asst_u8x2Hb9c9stVQwQMbostJ8JK"
48+
4049
def __init__(self, api_key: str, endpoint: Optional[str] = None):
41-
...
50+
self.connection_string = endpoint
51+
4252

4353
async def search(self, query: str, lang="en-US") -> WebAnswer:
44-
...
54+
cred = DefaultAzureCredential()
55+
# endpoint is the connection string
56+
self.project_client = AIProjectClient.from_connection_string(self.connection_string, cred)
57+
58+
async with self.project_client:
59+
# Create thread for communication
60+
thread = await self.project_client.agents.create_thread()
61+
62+
# Create message to thread
63+
message = await self.project_client.agents.create_message(
64+
thread_id=thread.id,
65+
role="user",
66+
content=query,
67+
)
68+
69+
# Create and process agent run in thread with tools
70+
run = await self.project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=self.agent_id)
71+
72+
if run.status == "failed":
73+
raise Exception(run.last_error)
74+
75+
# Fetch and log all messages
76+
messages = await self.project_client.agents.list_messages(thread_id=thread.id)
77+
78+
print(f"Messages: {messages}")
79+
80+
run_steps = await self.project_client.agents.list_run_steps(run_id=run.id, thread_id=thread.id)
81+
run_steps_data = run_steps['data']
82+
print(run_steps_data)
83+
84+
url = messages.data[0].content[0].text.annotations[0].as_dict()['url_citation']['url']
85+
title = messages.data[0].content[0].text.annotations[0].as_dict()['url_citation']['title']
86+
snippet = messages.data[0].content[0].text.value
87+
4588

89+
return WebAnswer(
90+
totalEstimatedMatches=1,
91+
webSearchUrl="https://www.bing.com",
92+
value=[
93+
WebPage(
94+
id="1",
95+
name=title,
96+
url=url,
97+
displayUrl=url,
98+
dateLastCrawled="2021-10-01",
99+
language="en",
100+
snippet=snippet,
101+
isFamilyFriendly=True,
102+
siteName="Bing"
103+
)
104+
],
105+
)
46106

47107
class AsyncBingSearchClient(AsyncGroundingSearchClient):
48108
def __init__(self, api_key: str, bing_endpoint: Optional[str] = "api.bing.microsoft.com"):

0 commit comments

Comments
 (0)