afouda commited on
Commit
ffe126a
·
verified ·
1 Parent(s): e61ab70

Update web_search.py

Browse files
Files changed (1) hide show
  1. 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
- load_dotenv()
13
- TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
14
- if not TAVILY_API_KEY:
15
- raise ValueError("TAVILY_API_KEY is not set.")
16
-
17
- # Initialize Tavily client and FastMCP
18
- tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
19
- mcp = FastMCP("tavily_search")
20
-
21
- # Define the search tool
22
- @mcp.tool()
23
- async def search_autism(query: str) -> dict:
24
- """Performs a Tavily web search for information about autism."""
25
- try:
26
- # Execute a search query
27
- response = tavily_client.search(
28
- query=query,
29
- max_results=5,
30
- search_depth="advanced",
31
- topic="general",
32
- name="live_search",
33
- description="""Using the latest information from reputable online sources, provide a concise AI-generated overview of the query related to autism spectrum """,
34
- include_answer=True
35
- )
36
- return {
37
- "results": response.get("results", []),
38
- "answer": response.get("answer", "")
39
- }
40
- except Exception as e:
41
- return {"error": f"Search failed: {str(e)}"}
42
-
43
- # Main entry point
44
- # async def main():
45
- # query = "autism symptoms and treatments" # Replace with dynamic input if needed
46
- # result = await search_autism(query)
47
- # print("Search Results:")
48
- # for res in result.get("results", []):
49
- # print(f"- {res.get('title')} ({res.get('url')})")
50
- # print("\nAnswer:")
51
- # print(result.get("answer", "No answer provided."))
52
-
53
- # Run the script
54
- if __name__ == "__main__":
55
- asyncio.run(main())
 
 
 
 
 
 
 
 
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())