Lucas-C-R commited on
Commit
c65bd6f
·
1 Parent(s): 331b11e

feat: create tools for agents

Browse files
Files changed (3) hide show
  1. tools/__init__.py +13 -0
  2. tools/math_tools.py +77 -0
  3. tools/search_tools.py +99 -0
tools/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tools.math_tools import add, div, mod, mult, sub
2
+ from tools.search_tools import arxiv_search, internet_search, wiki_search
3
+
4
+ __all__ = [
5
+ "add",
6
+ "div",
7
+ "mod",
8
+ "mult",
9
+ "sub",
10
+ "arxiv_search",
11
+ "internet_search",
12
+ "wiki_search",
13
+ ]
tools/math_tools.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.tools import tool
2
+
3
+
4
+ @tool
5
+ def add(a: float, b: float) -> float:
6
+ """Adds two numbers and returns the result rounded to 2 decimal places.
7
+
8
+ Args:
9
+ a (float): First number to be added
10
+ b (float): Second number to be added
11
+
12
+ Returns:
13
+ float: The sum of a and b, rounded to 2 decimal places
14
+ """
15
+ return round((a + b), 2)
16
+
17
+
18
+ @tool
19
+ def sub(a: float, b: float) -> float:
20
+ """Subtracts the second number from the first and returns the result rounded to 2 decimal places.
21
+
22
+ Args:
23
+ a (float): Number to subtract from
24
+ b (float): Number to subtract
25
+
26
+ Returns:
27
+ float: The difference between a and b, rounded to 2 decimal places
28
+ """
29
+ return round((a - b), 2)
30
+
31
+
32
+ @tool
33
+ def mult(a: float, b: float) -> float:
34
+ """Multiplies two numbers and returns the result rounded to 2 decimal places.
35
+
36
+ Args:
37
+ a (float): First number to multiply
38
+ b (float): Second number to multiply
39
+
40
+ Returns:
41
+ float: The product of a and b, rounded to 2 decimal places
42
+ """
43
+ return round((a * b), 2)
44
+
45
+
46
+ @tool
47
+ def div(a: float, b: float) -> float:
48
+ """Divides the first number by the second and returns the result rounded to 2 decimal places.
49
+
50
+ Args:
51
+ a (float): Number to be divided (dividend)
52
+ b (float): Number to divide by (divisor)
53
+
54
+ Raises:
55
+ ValueError: If the divisor (b) is zero
56
+
57
+ Returns:
58
+ float: The quotient of a divided by b, rounded to 2 decimal places
59
+ """
60
+ if b == 0:
61
+ raise ValueError("Cannot divide by zero!")
62
+
63
+ return round((a / b), 2)
64
+
65
+
66
+ @tool
67
+ def mod(a: float, b: float) -> float:
68
+ """Calculates the remainder of dividing the first number by the second and returns the result rounded to 2 decimal places.
69
+
70
+ Args:
71
+ a (float): Number to be divided (dividend)
72
+ b (float): Number to divide by (divisor)
73
+
74
+ Returns:
75
+ float: The remainder of a divided by b, rounded to 2 decimal places
76
+ """
77
+ return round((a % b), 2)
tools/search_tools.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List
2
+
3
+ from langchain_community.document_loaders import ArxivLoader, WikipediaLoader
4
+ from langchain_core.tools import tool
5
+ from langchain_tavily import TavilySearch
6
+
7
+
8
+ @tool
9
+ def internet_search(query: str) -> Dict[str, List[Dict[str, str]]]:
10
+ """Perform a web search using Tavily Search API.
11
+
12
+ This tool searches the web for relevant information based on the provided query.
13
+ It returns up to 2 most relevant results with their sources, titles, and content.
14
+
15
+ Args:
16
+ query (str): The search query to look up on the web.
17
+
18
+ Returns:
19
+ Dict[str, List[Dict[str, str]]]: A dictionary containing a list of search results.
20
+ Each result is a dictionary with keys:
21
+ - Source: URL of the webpage
22
+ - Title: Title of the webpage
23
+ - Content: Main content/text from the webpage
24
+ """
25
+ response = TavilySearch(max_results=1).invoke(query)
26
+
27
+ formatted_answer = [
28
+ {
29
+ "Source": result["url"],
30
+ "Title": result["title"],
31
+ "Content": result["content"],
32
+ }
33
+ for result in response["results"]
34
+ ]
35
+
36
+ return {"web_results": formatted_answer}
37
+
38
+
39
+ @tool
40
+ def wiki_search(query: str) -> Dict[str, List[Dict[str, str]]]:
41
+ """Search Wikipedia articles using the provided query.
42
+
43
+ This tool searches Wikipedia for articles matching the query and returns
44
+ up to 2 most relevant results with their sources, titles, and content.
45
+
46
+ Args:
47
+ query (str): The search query to look up on Wikipedia.
48
+
49
+ Returns:
50
+ Dict[str, List[Dict[str, str]]]: A dictionary containing a list of Wikipedia results.
51
+ Each result is a dictionary with keys:
52
+ - Source: URL of the Wikipedia article
53
+ - Title: Title of the Wikipedia article
54
+ - Content: Main content/text from the article
55
+ """
56
+ docs = WikipediaLoader(query=query, load_max_docs=2).load()
57
+
58
+ formatted_answer = [
59
+ {
60
+ "Source": doc.metadata["source"],
61
+ "Title": doc.metadata["title"],
62
+ "Content": doc.page_content,
63
+ }
64
+ for doc in docs
65
+ ]
66
+
67
+ return {"wiki_results": formatted_answer}
68
+
69
+
70
+ @tool
71
+ def arxiv_search(query: str) -> Dict[str, List[Dict[str, str]]]:
72
+ """Search academic papers on arXiv using the provided query.
73
+
74
+ This tool searches arXiv for academic papers matching the query and returns
75
+ up to 2 most relevant results with their sources, titles, and content.
76
+
77
+ Args:
78
+ query (str): The search query to look up on arXiv.
79
+
80
+ Returns:
81
+ Dict[str, List[Dict[str, str]]]: A dictionary containing a list of arXiv results.
82
+ Each result is a dictionary with keys:
83
+ - Source: URL of the arXiv paper
84
+ - Title: Title of the academic paper
85
+ - Content: Main content/abstract of the paper
86
+ """
87
+ docs = ArxivLoader(query=query, load_max_docs=2).load()
88
+
89
+ formatted_answer = [
90
+ {
91
+ "Published": doc.metadata["Published"],
92
+ "Authors": doc.metadata["Authors"],
93
+ "Title": doc.metadata["Title"],
94
+ "Content": doc.page_content,
95
+ }
96
+ for doc in docs
97
+ ]
98
+
99
+ return {"arxiv_results": formatted_answer}