okmich commited on
Commit
716e4df
·
verified ·
1 Parent(s): 81917a3

Upload agents.py

Browse files
Files changed (1) hide show
  1. agents.py +190 -0
agents.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ from PIL.Image import Image
4
+ from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
5
+ from langchain_community.tools import DuckDuckGoSearchRun
6
+ from langchain_core.tools import tool
7
+ from pytesseract import pytesseract
8
+ from youtube_transcript_api import YouTubeTranscriptApi
9
+
10
+
11
+ @tool
12
+ def add_numbers(a: float, b: float) -> float:
13
+ """
14
+ Adds two numbers and returns the result.
15
+ Args:
16
+ a (float): the first number
17
+ b (float): the second number
18
+ """
19
+ return a + b
20
+
21
+ @tool
22
+ def subtract_numbers(a: float, b: float) -> float:
23
+ """
24
+ Subtracts the second number from the first and returns the result.
25
+ Args:
26
+ a (float): the first number
27
+ b (float): the second number
28
+ """
29
+ return a - b
30
+
31
+ @tool
32
+ def multiply_numbers(a: float, b: float) -> float:
33
+ """
34
+ Multiplies two numbers and returns the result.
35
+ Args:
36
+ a (float): the first number
37
+ b (float): the second number
38
+ """
39
+ return a * b
40
+
41
+ @tool
42
+ def divide_numbers(a: float, b: float) -> float:
43
+ """
44
+ Divides the first number by the second. Raises error if dividing by zero.
45
+ Args:
46
+ a (float): the first number
47
+ b (float): the second number
48
+ """
49
+ if b == 0:
50
+ raise ValueError("Cannot divide by zero.")
51
+ return a / b
52
+
53
+ @tool
54
+ def modulo_numbers(a: int, b: int) -> int:
55
+ """
56
+ Returns the remainder when the first number is divided by the second.
57
+ Args:
58
+ a (float): the first number
59
+ b (float): the second number
60
+ """
61
+ if b == 0:
62
+ raise ValueError("Cannot perform modulo by zero.")
63
+ return a % b
64
+
65
+ @tool
66
+ def calculate_percentage(a: float, b: float) -> float:
67
+ """
68
+ Returns what percentage the first number is of the second number. Raises error if dividing by zero.
69
+ Args:
70
+ a (float): the first number
71
+ b (float): the second number
72
+ """
73
+ if b == 0:
74
+ raise ValueError("Whole cannot be zero.")
75
+ return (a / b) * 100
76
+
77
+ @tool
78
+ def square_root(value: float) -> float:
79
+ """
80
+ Returns the square root of the given number.
81
+ Args:
82
+ value (float): the given number
83
+ """
84
+ if value < 0:
85
+ raise ValueError("Cannot take the square root of a negative number.")
86
+ return math.sqrt(value)
87
+
88
+ @tool
89
+ def logarithm(value: float, base: float = 10) -> float:
90
+ """
91
+ Returns the logarithm of 'value' with the specified base (default: 10).
92
+ Args:
93
+ value (float): the given number
94
+ base (float): the specified base
95
+ """
96
+ if value <= 0 or base <= 0:
97
+ raise ValueError("Logarithm undefined for non-positive values.")
98
+ return math.log(value, base)
99
+
100
+
101
+ @tool
102
+ def search_wikipedia(query: str) -> str:
103
+ """
104
+ Searches Wikipedia for a given query and returns a short summary of the first matching article.
105
+ Args:
106
+ query (str): the given query
107
+ """
108
+ try:
109
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
110
+ output = []
111
+ for result in search_docs:
112
+ output.append(
113
+ f"Title: {result.metadata['title']}\n"
114
+ f"Summary: {result.metadata['summary'][:1000]}...\n"
115
+ f"Source: {result.metadata['source']}"
116
+ )
117
+ return "\n\n".join(output)
118
+ except Exception as e:
119
+ return f"Wikipedia error: {e}"
120
+
121
+ @tool
122
+ def read_text_from_image(image_path: str) -> str:
123
+ """Extracts text from an image file using OCR."""
124
+ try:
125
+ img = Image.open(image_path)
126
+ text = pytesseract.image_to_string(img)
127
+ return text.strip()
128
+ except Exception as e:
129
+ return f"Error processing image: {e}"
130
+
131
+ @tool
132
+ def get_youtube_transcript(video_id: str) -> str:
133
+ """
134
+ Retrieves and returns the transcript of a YouTube video by video ID.
135
+ Args:
136
+ video_id (str): the video ID
137
+ """
138
+ try:
139
+ transcript = YouTubeTranscriptApi().fetch(video_id)
140
+ full_text = '\n'.join([f"{entry.text}" for entry in transcript.snippets])
141
+ return full_text
142
+ except Exception as e:
143
+ return f"Transcript error: {e}"
144
+
145
+ @tool
146
+ def search_arxiv(query: str, max_results: int = 3) -> str:
147
+ """
148
+ Searches arXiv and returns titles and summaries of top papers.
149
+ Args:
150
+ :param query: the query to search
151
+ :param max_results: the number of top titles and summaries to return
152
+ """
153
+ try:
154
+ results = ArxivLoader(query=query, load_max_docs=3).load()
155
+ output = []
156
+ for result in results:
157
+ output.append(
158
+ f"Authors: {result.metadata['Authors']}\n"
159
+ f"Title: {result.metadata['Title']}\n"
160
+ f"Published: {result.metadata['Published']}\n"
161
+ f"Summary: {result.metadata['Summary'][:1000]}..."
162
+ )
163
+ return "\n\n".join(output)
164
+ except Exception as e:
165
+ return f"arXiv error: {e}"
166
+
167
+
168
+ def tool_list():
169
+ web_search_tool = DuckDuckGoSearchRun()
170
+ return [
171
+ web_search_tool,
172
+ add_numbers,
173
+ subtract_numbers,
174
+ multiply_numbers,
175
+ divide_numbers,
176
+ modulo_numbers,
177
+ calculate_percentage,
178
+ square_root,
179
+ logarithm,
180
+ search_wikipedia,
181
+ get_youtube_transcript,
182
+ search_arxiv,
183
+ read_text_from_image,
184
+ ]
185
+
186
+ # if __name__ == "__main__":
187
+ # # print("\nArxiv Search:\n", search_arxiv.invoke({"query": "Reinforcement learning forex", "max_results": 2}))
188
+ # # print("\nWikipedia Search:\n", search_wikipedia.invoke({"query": "Alexander the Great"}))
189
+ # # print("\nYouTube transcript:\n", get_youtube_transcript.invoke({"video_id": "1htKBjuUWec"}))
190
+ # print("\nText from Image:\n", read_text_from_image.invoke({"image_path": "1htKBjuUWec"}))