3mpj commited on
Commit
3076e6d
·
verified ·
1 Parent(s): f2845f9

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +105 -0
tools.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+ from langchain_community.tools.tavily_search import TavilySearchResults
3
+ from langchain_community.document_loaders import WikipediaLoader
4
+ from langchain_community.document_loaders import ArxivLoader
5
+
6
+
7
+ @tool
8
+ def multiply(a: int, b: int) -> int:
9
+ """Multiply two numbers.
10
+ Args:
11
+ a: first int
12
+ b: second int
13
+ """
14
+ return a * b
15
+
16
+
17
+ @tool
18
+ def add(a: int, b: int) -> int:
19
+ """Add two numbers.
20
+
21
+ Args:
22
+ a: first int
23
+ b: second int
24
+ """
25
+ return a + b
26
+
27
+
28
+ @tool
29
+ def subtract(a: int, b: int) -> int:
30
+ """Subtract two numbers.
31
+
32
+ Args:
33
+ a: first int
34
+ b: second int
35
+ """
36
+ return a - b
37
+
38
+
39
+ @tool
40
+ def divide(a: int, b: int) -> int:
41
+ """Divide two numbers.
42
+
43
+ Args:
44
+ a: first int
45
+ b: second int
46
+ """
47
+ if b == 0:
48
+ raise ValueError("Cannot divide by zero.")
49
+ return a / b
50
+
51
+
52
+ @tool
53
+ def modulus(a: int, b: int) -> int:
54
+ """Get the modulus of two numbers.
55
+
56
+ Args:
57
+ a: first int
58
+ b: second int
59
+ """
60
+ return a % b
61
+
62
+
63
+ @tool
64
+ def wiki_search(query: str) -> str:
65
+ """Search Wikipedia for a query and return maximum 2 results.
66
+
67
+ Args:
68
+ query: The search query."""
69
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
70
+ formatted_search_docs = "\n\n---\n\n".join(
71
+ [
72
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
73
+ for doc in search_docs
74
+ ])
75
+ return {"wiki_results": formatted_search_docs}
76
+
77
+
78
+ @tool
79
+ def web_search(query: str) -> str:
80
+ """Search Tavily for a query and return maximum 3 results.
81
+
82
+ Args:
83
+ query: The search query."""
84
+ search_docs = TavilySearchResults(max_results=3).invoke(query=query)
85
+ formatted_search_docs = "\n\n---\n\n".join(
86
+ [
87
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
88
+ for doc in search_docs
89
+ ])
90
+ return {"web_results": formatted_search_docs}
91
+
92
+
93
+ @tool
94
+ def arvix_search(query: str) -> str:
95
+ """Search Arxiv for a query and return maximum 3 result.
96
+
97
+ Args:
98
+ query: The search query."""
99
+ search_docs = ArxivLoader(query=query, load_max_docs=3).load()
100
+ formatted_search_docs = "\n\n---\n\n".join(
101
+ [
102
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
103
+ for doc in search_docs
104
+ ])
105
+ return {"arvix_results": formatted_search_docs}