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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cmath
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 5 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 6 |
+
from langchain_community.tools import tool
|
| 7 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 8 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@tool
|
| 12 |
+
def wiki_search(query: str) -> str:
|
| 13 |
+
"""Search Wikipedia for a query and return maximum 2 page links.
|
| 14 |
+
Args:
|
| 15 |
+
query: The search query."""
|
| 16 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 17 |
+
# formatted_search_docs = "\n\n---\n\n".join(
|
| 18 |
+
# [
|
| 19 |
+
# f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 20 |
+
# for doc in search_docs
|
| 21 |
+
# ]
|
| 22 |
+
# )
|
| 23 |
+
formatted_search_docs = [
|
| 24 |
+
{"source": doc.metadata.get("source", ""), "page": doc.metadata.get("page", "")}
|
| 25 |
+
for doc in search_docs
|
| 26 |
+
]
|
| 27 |
+
return {"wiki_page_links": json.dumps(formatted_search_docs)}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@tool
|
| 31 |
+
def web_search(query: str) -> str:
|
| 32 |
+
"""Search Tavily for a query and return maximum 3 page links.
|
| 33 |
+
Args:
|
| 34 |
+
query: The search query."""
|
| 35 |
+
search_docs = TavilySearchResults(
|
| 36 |
+
max_results=3,
|
| 37 |
+
tavily_api_key="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",
|
| 38 |
+
).invoke(query)
|
| 39 |
+
# formatted_search_docs = "\n\n---\n\n".join(
|
| 40 |
+
# [
|
| 41 |
+
# f'<Document source="{doc.get("url", "")}" title="{doc.get("title", "")}"/>\n{doc.get("content", "")}\n</Document>'
|
| 42 |
+
# for doc in search_docs
|
| 43 |
+
# ]
|
| 44 |
+
# )
|
| 45 |
+
formatted_search_docs = [
|
| 46 |
+
{"source": doc.get("url", ""), "page": doc.get("title", "")}
|
| 47 |
+
for doc in search_docs
|
| 48 |
+
]
|
| 49 |
+
return {"web_page_links": json.dumps(formatted_search_docs)}
|
| 50 |
+
|
| 51 |
+
@tool
|
| 52 |
+
def visit_webpage(url: str) -> str:
|
| 53 |
+
"""Retrieve content from a webpage using page links. A good option when you need detailed information from a specific webpage.
|
| 54 |
+
Args:
|
| 55 |
+
url: The URL to retrieve content from."""
|
| 56 |
+
search_docs = WebBaseLoader(url).load()
|
| 57 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 58 |
+
[
|
| 59 |
+
f'<Document source="{doc.metadata.get("source", "")}" title="{doc.metadata.get("title", "")}"/>\n{doc.page_content}\n</Document>'
|
| 60 |
+
for doc in search_docs
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
return {"web_page_content": formatted_search_docs}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@tool
|
| 67 |
+
def arxiv_search(query: str) -> str:
|
| 68 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 69 |
+
Args:
|
| 70 |
+
query: The search query."""
|
| 71 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 72 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 73 |
+
[
|
| 74 |
+
f'<Document source="{doc.metadata["Title"]}" page="{doc.metadata.get("Summary", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 75 |
+
for doc in search_docs
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
return {"arxiv_results": formatted_search_docs}
|
| 79 |
+
|
| 80 |
+
@tool
|
| 81 |
+
def translate_to_english(text: str) -> str:
|
| 82 |
+
"""Translate the given text to English.
|
| 83 |
+
Args:
|
| 84 |
+
text: The text to translate to English."""
|
| 85 |
+
from langchain_ollama import ChatOllama
|
| 86 |
+
|
| 87 |
+
llm = ChatOllama(
|
| 88 |
+
model="tinyllama:1.1b",
|
| 89 |
+
api_base="http://localhost:11434",
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
prompt = f"Translate the following text to English:\n\n{text}"
|
| 93 |
+
response = llm.invoke(
|
| 94 |
+
[
|
| 95 |
+
{"role": "system", "content": "You are a helpful assistant that translates text to English."},
|
| 96 |
+
{"role": "user", "content": prompt},
|
| 97 |
+
]
|
| 98 |
+
)
|
| 99 |
+
return {"translated_text": response.content}
|
| 100 |
+
|
| 101 |
+
@tool
|
| 102 |
+
def multiply(a: float, b: float) -> float:
|
| 103 |
+
"""
|
| 104 |
+
Multiplies two numbers.
|
| 105 |
+
Args:
|
| 106 |
+
a (float): the first number
|
| 107 |
+
b (float): the second number
|
| 108 |
+
"""
|
| 109 |
+
return a * b
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@tool
|
| 113 |
+
def add(a: float, b: float) -> float:
|
| 114 |
+
"""
|
| 115 |
+
Adds two numbers.
|
| 116 |
+
Args:
|
| 117 |
+
a (float): the first number
|
| 118 |
+
b (float): the second number
|
| 119 |
+
"""
|
| 120 |
+
return a + b
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@tool
|
| 124 |
+
def subtract(a: float, b: float) -> int:
|
| 125 |
+
"""
|
| 126 |
+
Subtracts two numbers.
|
| 127 |
+
Args:
|
| 128 |
+
a (float): the first number
|
| 129 |
+
b (float): the second number
|
| 130 |
+
"""
|
| 131 |
+
return a - b
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
@tool
|
| 135 |
+
def divide(a: float, b: float) -> float:
|
| 136 |
+
"""
|
| 137 |
+
Divides two numbers.
|
| 138 |
+
Args:
|
| 139 |
+
a (float): the first float number
|
| 140 |
+
b (float): the second float number
|
| 141 |
+
"""
|
| 142 |
+
if b == 0:
|
| 143 |
+
raise ValueError("Cannot divided by zero.")
|
| 144 |
+
return a / b
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
@tool
|
| 148 |
+
def modulus(a: int, b: int) -> int:
|
| 149 |
+
"""
|
| 150 |
+
Get the modulus of two numbers.
|
| 151 |
+
Args:
|
| 152 |
+
a (int): the first number
|
| 153 |
+
b (int): the second number
|
| 154 |
+
"""
|
| 155 |
+
return a % b
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
@tool
|
| 159 |
+
def power(a: float, b: float) -> float:
|
| 160 |
+
"""
|
| 161 |
+
Get the power of two numbers.
|
| 162 |
+
Args:
|
| 163 |
+
a (float): the first number
|
| 164 |
+
b (float): the second number
|
| 165 |
+
"""
|
| 166 |
+
return a**b
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
@tool
|
| 170 |
+
def square_root(a: float) -> float | complex:
|
| 171 |
+
"""
|
| 172 |
+
Get the square root of a number.
|
| 173 |
+
Args:
|
| 174 |
+
a (float): the number to get the square root of
|
| 175 |
+
"""
|
| 176 |
+
if a >= 0:
|
| 177 |
+
return a**0.5
|
| 178 |
+
return cmath.sqrt(a)
|