Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def add(a: int, b: int) -> int:
|
| 6 |
+
"""
|
| 7 |
+
Sums two values and returns the result of the sum
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
a: first number
|
| 11 |
+
b: second number
|
| 12 |
+
"""
|
| 13 |
+
return a + b
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def substract(a: int, b: int) -> int:
|
| 17 |
+
"""
|
| 18 |
+
Subtracts one value from another and returns the result of the sum
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
a: first number
|
| 22 |
+
b: second number
|
| 23 |
+
"""
|
| 24 |
+
return a - b
|
| 25 |
+
|
| 26 |
+
@tool
|
| 27 |
+
def multiply(a: int, b: int) -> int:
|
| 28 |
+
"""
|
| 29 |
+
Multiplies two values and returns the result of the sum
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
a: first number
|
| 33 |
+
b: second number
|
| 34 |
+
"""
|
| 35 |
+
return a * b
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def divide(a: int, b: int) -> int:
|
| 39 |
+
"""
|
| 40 |
+
Divides two values and returns the result of the sum
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
a: numerator
|
| 44 |
+
b: denominator
|
| 45 |
+
"""
|
| 46 |
+
if b == 0:
|
| 47 |
+
raise ValueError("Cannot divide by zero.")
|
| 48 |
+
return a / b
|
| 49 |
+
|
| 50 |
+
@tool
|
| 51 |
+
def web_search(query: str) -> str:
|
| 52 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
query: The search query."""
|
| 56 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 57 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 58 |
+
[
|
| 59 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 60 |
+
for doc in search_docs
|
| 61 |
+
])
|
| 62 |
+
return {"web_results": formatted_search_docs}
|