Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -4,7 +4,122 @@ from langchain_community.tools import DuckDuckGoSearchRun
|
|
| 4 |
from langchain_community.document_loaders.wikipedia import WikipediaLoader
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@tool
|
| 10 |
def wiki_loader(query: str, lang: str='en', load_max_docs: int=3):
|
|
|
|
| 4 |
from langchain_community.document_loaders.wikipedia import WikipediaLoader
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
|
| 7 |
+
|
| 8 |
+
@tool
|
| 9 |
+
def youtube_video_loader(url: str) -> str:
|
| 10 |
+
"""Load a Youtube video and return the transcript.
|
| 11 |
+
Args:
|
| 12 |
+
url (str): The Youtube video URL."""
|
| 13 |
+
loader = YoutubeLoader.from_youtube_url(str(url), add_video_info=False)
|
| 14 |
+
docs = loader.load()
|
| 15 |
+
print(docs)
|
| 16 |
+
return "\n\n".join(map(repr, docs))
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@tool
|
| 20 |
+
def multiply(x: int, y: int) -> int:
|
| 21 |
+
"""Multiply two numbers."""
|
| 22 |
+
return x * y
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@tool
|
| 26 |
+
def add(x: int, y: int) -> int:
|
| 27 |
+
"""Add two numbers."""
|
| 28 |
+
return x + y
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@tool
|
| 32 |
+
def subtract(x: int, y: int) -> int:
|
| 33 |
+
"""Subtract two numbers."""
|
| 34 |
+
return x - y
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def divide(x: int, y: int) -> float:
|
| 39 |
+
"""Divide two numbers."""
|
| 40 |
+
return x / y
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@tool
|
| 44 |
+
def modulus(x: int, y: int) -> int:
|
| 45 |
+
"""Get the modulus of two numbers."""
|
| 46 |
+
return x % y
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@tool
|
| 50 |
+
def wiki_search(query: str) -> str:
|
| 51 |
+
"""Search Wikipedia for a query. Returns the first result 3.
|
| 52 |
+
Args:
|
| 53 |
+
query (str): The search query."""
|
| 54 |
+
loader = WikipediaLoader(query=query, load_max_docs=3)
|
| 55 |
+
docs = loader.load()
|
| 56 |
+
|
| 57 |
+
if docs:
|
| 58 |
+
# For each document Extract the content and metadata from the documents
|
| 59 |
+
results = []
|
| 60 |
+
for doc in docs:
|
| 61 |
+
content = doc.page_content
|
| 62 |
+
metadata = doc.metadata
|
| 63 |
+
# Handle missing metadata fields gracefully
|
| 64 |
+
result = {
|
| 65 |
+
"source": metadata.get('source', 'Unknown source'),
|
| 66 |
+
"page": metadata.get('page', 'Unknown page'),
|
| 67 |
+
"content": content
|
| 68 |
+
}
|
| 69 |
+
results.append(result)
|
| 70 |
+
|
| 71 |
+
return results.__str__() if results else "No results found."
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def arxiv_search(query: str) -> str:
|
| 76 |
+
"""Search Arxiv for a query. Returns the first result 3.
|
| 77 |
+
Args:
|
| 78 |
+
query (str): The search query."""
|
| 79 |
+
loader = ArxivLoader(query=query, load_max_docs=3)
|
| 80 |
+
docs = loader.load()
|
| 81 |
+
|
| 82 |
+
if docs:
|
| 83 |
+
# For each document Extract the content and metadata from the documents
|
| 84 |
+
results = []
|
| 85 |
+
for doc in docs:
|
| 86 |
+
content = doc.page_content
|
| 87 |
+
metadata = doc.metadata
|
| 88 |
+
# Format the result as json object
|
| 89 |
+
result = {
|
| 90 |
+
"source": metadata['source'],
|
| 91 |
+
"page": metadata['page'],
|
| 92 |
+
"content": content[:1000] # Limit content to 1000 characters
|
| 93 |
+
}
|
| 94 |
+
results.append(result)
|
| 95 |
+
|
| 96 |
+
return results.__str__() if results else "No results found."
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@tool
|
| 100 |
+
def tavily_search(query: str) -> str:
|
| 101 |
+
"""Search Tavily for a query. Returns the first result 3.
|
| 102 |
+
Args:
|
| 103 |
+
query (str): The search query."""
|
| 104 |
+
docs = TavilySearchResults(max_results=2).invoke(query)
|
| 105 |
+
|
| 106 |
+
if docs:
|
| 107 |
+
# For each document Extract the content and metadata from the documents
|
| 108 |
+
results = []
|
| 109 |
+
for doc in docs:
|
| 110 |
+
content = doc['content']
|
| 111 |
+
titel = doc['title']
|
| 112 |
+
url = doc['url']
|
| 113 |
+
# Format the result as json object
|
| 114 |
+
result = {
|
| 115 |
+
"source": url,
|
| 116 |
+
"titel": titel,
|
| 117 |
+
"content": content # Limit content to 1000 characters
|
| 118 |
+
}
|
| 119 |
+
results.append(result)
|
| 120 |
+
|
| 121 |
+
return results.__str__() if results else "No results found."
|
| 122 |
+
|
| 123 |
|
| 124 |
@tool
|
| 125 |
def wiki_loader(query: str, lang: str='en', load_max_docs: int=3):
|