Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- tools/__init__.py +0 -0
- tools/math_tools.py +52 -0
- tools/search_tools.py +53 -0
tools/__init__.py
ADDED
File without changes
|
tools/math_tools.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.tools import tool
|
2 |
+
|
3 |
+
@tool
|
4 |
+
def multiply(a: int, b: int) -> int:
|
5 |
+
"""Multiply two numbers.
|
6 |
+
Args:
|
7 |
+
a: first int
|
8 |
+
b: second int
|
9 |
+
"""
|
10 |
+
return a * b
|
11 |
+
|
12 |
+
@tool
|
13 |
+
def add(a: int, b: int) -> int:
|
14 |
+
"""Add two numbers.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
a: first int
|
18 |
+
b: second int
|
19 |
+
"""
|
20 |
+
return a + b
|
21 |
+
|
22 |
+
@tool
|
23 |
+
def subtract(a: int, b: int) -> int:
|
24 |
+
"""Subtract two numbers.
|
25 |
+
|
26 |
+
Args:
|
27 |
+
a: first int
|
28 |
+
b: second int
|
29 |
+
"""
|
30 |
+
return a - b
|
31 |
+
|
32 |
+
@tool
|
33 |
+
def divide(a: int, b: int) -> int:
|
34 |
+
"""Divide two numbers.
|
35 |
+
|
36 |
+
Args:
|
37 |
+
a: first int
|
38 |
+
b: second int
|
39 |
+
"""
|
40 |
+
if b == 0:
|
41 |
+
raise ValueError("Cannot divide by zero.")
|
42 |
+
return a / b
|
43 |
+
|
44 |
+
@tool
|
45 |
+
def modulus(a: int, b: int) -> int:
|
46 |
+
"""Get the modulus of two numbers.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
a: first int
|
50 |
+
b: second int
|
51 |
+
"""
|
52 |
+
return a % b
|
tools/search_tools.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.tools import tool
|
2 |
+
from langchain_community.document_loaders import WikipediaLoader
|
3 |
+
from langchain_community.document_loaders import ArxivLoader
|
4 |
+
# Search engine specifically for LLMs
|
5 |
+
# from langchain_community.tools.tavily_search import TavilySearchResults
|
6 |
+
from langchain_tavily import TavilySearch
|
7 |
+
|
8 |
+
|
9 |
+
@tool
|
10 |
+
def web_search(query: str) -> str:
|
11 |
+
"""Search Tavily for a query and return maximum 3 results.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
query: The search query."""
|
15 |
+
# print(f"Web search query:::::::::::: {query}")
|
16 |
+
search_docs = TavilySearch(max_results=3).invoke({"query":query})
|
17 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
18 |
+
[
|
19 |
+
f'<Document source="{doc["url"]}" page="{doc["title"]}"/>\n{doc["content"]}\n</Document>'
|
20 |
+
for doc in search_docs['results']
|
21 |
+
])
|
22 |
+
# print(f"Web search result:::::::::::: {formatted_search_docs}")
|
23 |
+
return {"web_results": formatted_search_docs}
|
24 |
+
|
25 |
+
@tool
|
26 |
+
def wiki_search(query: str) -> str:
|
27 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
query: The search query."""
|
31 |
+
|
32 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
33 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
34 |
+
[
|
35 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
36 |
+
for doc in search_docs
|
37 |
+
])
|
38 |
+
|
39 |
+
return {"wiki_results": formatted_search_docs}
|
40 |
+
|
41 |
+
@tool
|
42 |
+
def arvix_search(query: str) -> str:
|
43 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
44 |
+
|
45 |
+
Args:
|
46 |
+
query: The search query."""
|
47 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
48 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
49 |
+
[
|
50 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
51 |
+
for doc in search_docs
|
52 |
+
])
|
53 |
+
return {"arvix_results": formatted_search_docs}
|