|
6 | 6 |
|
7 | 7 | import httpx
|
8 | 8 | 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 |
9 | 12 |
|
10 | 13 |
|
| 14 | +BING_CONNECTION_NAME = 'antbingtesting' |
| 15 | + |
11 | 16 | class WebPage(BaseModel):
|
12 | 17 | id: str
|
13 | 18 | name: str
|
@@ -37,12 +42,67 @@ class WebAnswer(BaseModel):
|
37 | 42 |
|
38 | 43 |
|
39 | 44 | class AsyncGroundingSearchClient:
|
| 45 | + project_client: AIProjectClient |
| 46 | + bing_tool: BingGroundingTool |
| 47 | + agent_id: str = "asst_u8x2Hb9c9stVQwQMbostJ8JK" |
| 48 | + |
40 | 49 | def __init__(self, api_key: str, endpoint: Optional[str] = None):
|
41 |
| - ... |
| 50 | + self.connection_string = endpoint |
| 51 | + |
42 | 52 |
|
43 | 53 | 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 | + |
45 | 88 |
|
| 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 | + ) |
46 | 106 |
|
47 | 107 | class AsyncBingSearchClient(AsyncGroundingSearchClient):
|
48 | 108 | def __init__(self, api_key: str, bing_endpoint: Optional[str] = "api.bing.microsoft.com"):
|
|
0 commit comments