Update web_search.py
Browse files- web_search.py +62 -55
web_search.py
CHANGED
@@ -1,55 +1,62 @@
|
|
1 |
-
import os
|
2 |
-
import asyncio
|
3 |
-
import httpx
|
4 |
-
from tavily import TavilyClient
|
5 |
-
from mcp.server.fastmcp import FastMCP
|
6 |
-
import nest_asyncio
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
|
9 |
-
# Apply nested asyncio patch
|
10 |
-
nest_asyncio.apply()
|
11 |
-
|
12 |
-
|
13 |
-
TAVILY_API_KEY
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import asyncio
|
3 |
+
import httpx
|
4 |
+
from tavily import TavilyClient
|
5 |
+
from mcp.server.fastmcp import FastMCP
|
6 |
+
import nest_asyncio
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
# Apply nested asyncio patch
|
10 |
+
nest_asyncio.apply()
|
11 |
+
|
12 |
+
GEMINI_API_KEY="AIzaSyCUCivstFpC9pq_jMHMYdlPrmh9Bx97dFo"
|
13 |
+
TAVILY_API_KEY="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"
|
14 |
+
OPENAI_API_KEY="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"
|
15 |
+
QDRANT_API_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIiwiZXhwIjoxNzUxMDUxNzg4fQ.I9J-K7OM0BtcNKgj2d4uVM8QYAHYfFCVAyP4rlZkK2E"
|
16 |
+
QDRANT_URL="https://6a3aade6-e8ad-4a6c-a579-21f5af90b7e8.us-east4-0.gcp.cloud.qdrant.io"
|
17 |
+
OPENAI_API_KEY="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"
|
18 |
+
WEAVIATE_URL="https://xbvlj5rpqyiswspww0tthq.c0.us-west3.gcp.weaviate.cloud"
|
19 |
+
WEAVIATE_API_KEY="RU9acU1CYnNRTjY1S1ZFc18zNS9tQktaWlcwTzFEUjlscEVCUGF4YU5xRWx2MDhmTUtIdUhnOWdOTGVZPV92MjAw"
|
20 |
+
DEEPINFRA_API_KEY="285LUJulGIprqT6hcPhiXtcrphU04FG4"
|
21 |
+
DEEPINFRA_BASE_URL="https://api.deepinfra.com/v1/openai"
|
22 |
+
|
23 |
+
|
24 |
+
# Initialize Tavily client and FastMCP
|
25 |
+
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
26 |
+
mcp = FastMCP("tavily_search")
|
27 |
+
|
28 |
+
# Define the search tool
|
29 |
+
@mcp.tool()
|
30 |
+
async def search_autism(query: str) -> dict:
|
31 |
+
"""Performs a Tavily web search for information about autism."""
|
32 |
+
try:
|
33 |
+
# Execute a search query
|
34 |
+
response = tavily_client.search(
|
35 |
+
query=query,
|
36 |
+
max_results=5,
|
37 |
+
search_depth="advanced",
|
38 |
+
topic="general",
|
39 |
+
name="live_search",
|
40 |
+
description="""Using the latest information from reputable online sources, provide a concise AI-generated overview of the query related to autism spectrum """,
|
41 |
+
include_answer=True
|
42 |
+
)
|
43 |
+
return {
|
44 |
+
"results": response.get("results", []),
|
45 |
+
"answer": response.get("answer", "")
|
46 |
+
}
|
47 |
+
except Exception as e:
|
48 |
+
return {"error": f"Search failed: {str(e)}"}
|
49 |
+
|
50 |
+
# Main entry point
|
51 |
+
# async def main():
|
52 |
+
# query = "autism symptoms and treatments" # Replace with dynamic input if needed
|
53 |
+
# result = await search_autism(query)
|
54 |
+
# print("Search Results:")
|
55 |
+
# for res in result.get("results", []):
|
56 |
+
# print(f"- {res.get('title')} ({res.get('url')})")
|
57 |
+
# print("\nAnswer:")
|
58 |
+
# print(result.get("answer", "No answer provided."))
|
59 |
+
|
60 |
+
# Run the script
|
61 |
+
if __name__ == "__main__":
|
62 |
+
asyncio.run(main())
|