madsc13nt1st commited on
Commit
48355ac
·
verified ·
1 Parent(s): c6aa09a

Create agent_tools.py

Browse files
Files changed (1) hide show
  1. agent_tools.py +112 -0
agent_tools.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import TextLoader, PyPDFLoader, Docx2txtLoader, CSVLoader
2
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
3
+ from langchain_community.retrievers import BM25Retriever
4
+
5
+ @tool
6
+ def addition_tool(a:float, b:float)->float:
7
+ """
8
+ Takes two floating point numbers and returns the results of their addition.
9
+ Args:
10
+ a: First number of the addition operation
11
+ b: Second number of the addition operation
12
+ """
13
+ return f"The result of addition between {a} and {b} is {a+b}"
14
+
15
+ @tool
16
+ def subtraction_tool(a:float, b:float)->float:
17
+ """
18
+ Takes two floating point numbers and returns the results of their subtraction.
19
+ Args:
20
+ a: First number of the subtraction operation
21
+ b: Second number of the subtraction operation
22
+ """
23
+ return f"The result of subtraction between {a} and {b} is {a-b}"
24
+
25
+ @tool
26
+ def multiplication_tool(a:float, b:float)->float:
27
+ """
28
+ Takes two floating point numbers and returns the results of their addition.
29
+ Args:
30
+ a: First number of the multiplication operation
31
+ b: Second number of the multiplication operation
32
+ """
33
+ return f"The result of multiplication between {a} and {b} is {a*b}"
34
+
35
+ @tool
36
+ def division_tool(a:float, b:float)->float:
37
+ """
38
+ Takes two floating point numbers and returns the results of their addition.
39
+ Args:
40
+ a: The dividend of the division operation
41
+ b: The divisor of the division operation
42
+ """
43
+ return f"The result of division between {a} and {b} is {a/b}"
44
+
45
+ @tool
46
+ def exponent_tool(a:float, b:int)->float:
47
+ """
48
+ Takes a floating point number and an integer expoent and returns the results of their exponential operation.
49
+ Args:
50
+ a: Base number of the addition operation
51
+ b: Exponent number of the addition operation
52
+ """
53
+ return f"The result of exponential operation between {a} and {b} is {a**b}"
54
+
55
+
56
+ @tool
57
+ def modulus_tool(a:int, b:int)->float:
58
+ """Get the modulus of two numbers.
59
+ Args:
60
+ a: first int
61
+ b: second int
62
+ """
63
+ return f"The result of modulus operation between {a} and {b} is {a%b}"
64
+
65
+
66
+ class RagTool(Tool):
67
+ name = "document_retrieval_tool"
68
+ description = "Loads documents and retrieves important information from them."
69
+ inputs = {
70
+ "doc_path": {
71
+ "type": "string",
72
+ "description": "The local document path"
73
+ },
74
+ "query": {
75
+ "type": "string",
76
+ "description": "The information that needs to be retrieved from the document."
77
+ },
78
+ }
79
+ output_type = "string"
80
+
81
+ class GeneralLoader:
82
+ def __init__(self, doc_path: str):
83
+ self.doc_path = doc_path
84
+
85
+ def get_doc_format(self):
86
+ doc_format = self.doc_path.split(".")[-1]
87
+ return "docx" if doc_format == "doc" else doc_format
88
+
89
+ def load_doc(self, path, format):
90
+ loaders = {
91
+ "txt": TextLoader,
92
+ "pdf": PyPDFLoader,
93
+ "docx": Docx2txtLoader,
94
+ "csv": CSVLoader
95
+ }
96
+ doc_loader_cls = loaders.get(format)
97
+ if not doc_loader_cls:
98
+ raise ValueError(f"Unsupported document format: {format}")
99
+ loader = doc_loader_cls(path)
100
+ splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=30)
101
+ docs = loader.load_and_split(text_splitter=splitter)
102
+ return docs
103
+
104
+ def get_docs(self):
105
+ format = self.get_doc_format()
106
+ return self.load_doc(self.doc_path, format)
107
+
108
+ def forward(self, doc_path: str, query: str):
109
+ docs = self.GeneralLoader(doc_path).get_docs()
110
+ retriever = BM25Retriever.from_documents(docs)
111
+ results = retriever.get_relevant_documents(query)
112
+ return "\n\n".join([doc.page_content for doc in results[:3]]) if results else "No matching information found"