Spaces:
Sleeping
Sleeping
Update tool.py
Browse files
tool.py
CHANGED
|
@@ -4,9 +4,10 @@ from langchain_community.tools.tavily_search import TavilySearchResults
|
|
| 4 |
from langchain_community.document_loaders.wikipedia import WikipediaLoader
|
| 5 |
from langchain_community.document_loaders.arxiv import ArxivLoader
|
| 6 |
from langchain_community.document_loaders.pubmed import PubMedLoader
|
|
|
|
|
|
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
-
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
import requests
|
|
@@ -17,6 +18,7 @@ import pandas as pd
|
|
| 17 |
import uuid
|
| 18 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 19 |
|
|
|
|
| 20 |
## Simple algebra tools
|
| 21 |
@tool
|
| 22 |
def add(a: float, b: float) -> float:
|
|
@@ -91,6 +93,28 @@ def TavilySearchTool(query: str) -> str:
|
|
| 91 |
])
|
| 92 |
return {"web_results": formatted_search_docs}
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
@tool
|
| 95 |
def WikipediaSearchTool(query: str) -> str:
|
| 96 |
"""Search Wikipedia for a query and return maximum 2 results.
|
|
|
|
| 4 |
from langchain_community.document_loaders.wikipedia import WikipediaLoader
|
| 5 |
from langchain_community.document_loaders.arxiv import ArxivLoader
|
| 6 |
from langchain_community.document_loaders.pubmed import PubMedLoader
|
| 7 |
+
from langchain_community.tools.google_search.tool import GoogleSearchRun
|
| 8 |
+
from langchain_community.tools.duckduckgo_search.tool import DuckDuckGoSearchResults
|
| 9 |
from typing import Optional
|
| 10 |
|
|
|
|
| 11 |
import os
|
| 12 |
import tempfile
|
| 13 |
import requests
|
|
|
|
| 18 |
import uuid
|
| 19 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 20 |
|
| 21 |
+
|
| 22 |
## Simple algebra tools
|
| 23 |
@tool
|
| 24 |
def add(a: float, b: float) -> float:
|
|
|
|
| 93 |
])
|
| 94 |
return {"web_results": formatted_search_docs}
|
| 95 |
|
| 96 |
+
google = GoogleSearchRun()
|
| 97 |
+
duckduckgo = DuckDuckGoSearchResults()
|
| 98 |
+
tavily = TavilySearchResults(max_results=3)
|
| 99 |
+
|
| 100 |
+
@tool
|
| 101 |
+
def combined_web_search(query: str) -> dict:
|
| 102 |
+
"""Search Google, DuckDuckGo, and Tavily for a query and return combined results."""
|
| 103 |
+
|
| 104 |
+
google_docs = google.invoke(query)[:3]
|
| 105 |
+
duck_docs = duckduckgo.invoke(query)[:3]
|
| 106 |
+
tavily_docs = tavily.invoke(query)[:3]
|
| 107 |
+
|
| 108 |
+
all_docs = google_docs + duck_docs + tavily_docs
|
| 109 |
+
|
| 110 |
+
formatted_results = "\n\n---\n\n".join(
|
| 111 |
+
f'<Document source="{doc.metadata.get("source", "unknown")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 112 |
+
for doc in all_docs
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
return {"web_results": formatted_results}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
@tool
|
| 119 |
def WikipediaSearchTool(query: str) -> str:
|
| 120 |
"""Search Wikipedia for a query and return maximum 2 results.
|