-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb_search.py
More file actions
38 lines (29 loc) · 1020 Bytes
/
Copy pathweb_search.py
File metadata and controls
38 lines (29 loc) · 1020 Bytes
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
import os
import chainlit as cl
from dotenv import load_dotenv, find_dotenv
from tavily import AsyncTavilyClient
load_dotenv(find_dotenv())
from agents import function_tool
tavily_api_key = os.getenv("TAVILY_API_KEY")
if not tavily_api_key:
raise ValueError("Tavily API key is not set. Please ensure TAVILY_API_KEY is defined in your .env file.")
# Initialize Tavily client for web search
tavily_client = AsyncTavilyClient(api_key=tavily_api_key)
# --- Tool Definitions ---
@function_tool
@cl.step(type="Web Search Tool")
async def web_search(query: str):
"""Search the web using Tavily."""
response = await tavily_client.search(query)
formatted_results = []
for result in response['results']:
result_text = f"""
### {result['title']}
{result['content']}
##### [Source]({result['url']})
---
"""
formatted_results.append(result_text)
# Join all results and send as one message
all_results = "\n".join(formatted_results)
return all_results