bstraehle commited on
Commit
ee32d98
·
verified ·
1 Parent(s): 24acc43

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +0 -262
tools.py CHANGED
@@ -16,268 +16,6 @@ ARITHMETIC_MODEL = "gemini-2.5-flash-preview-04-17"
16
  CODE_GENERATION_MODEL = "gemini-2.5-flash-preview-04-17"
17
  CODE_EXECUTION_MODEL = "gemini-2.5-flash-preview-04-17"
18
 
19
- @tool("Web Search Tool")
20
- def web_search_tool(question: str) -> str:
21
- """Given a question only, search the web to answer the question.
22
-
23
- Args:
24
- question (str): Question to answer
25
-
26
- Returns:
27
- str: Answer to the question
28
-
29
- Raises:
30
- RuntimeError: If processing fails"""
31
- try:
32
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
33
-
34
- response = client.models.generate_content(
35
- model=WEB_SEARCH_MODEL,
36
- contents=question,
37
- config=types.GenerateContentConfig(
38
- tools=[types.Tool(google_search=types.GoogleSearchRetrieval())]
39
- )
40
- )
41
-
42
- return response.text
43
- except Exception as e:
44
- raise RuntimeError(f"Processing failed: {str(e)}")
45
-
46
- @tool("Image Analysis Tool")
47
- def image_analysis_tool(question: str, file_path: str) -> str:
48
- """Given a question and image file, analyze the image to answer the question.
49
-
50
- Args:
51
- question (str): Question about an image file
52
- file_path (str): The image file path
53
-
54
- Returns:
55
- str: Answer to the question about the image file
56
-
57
- Raises:
58
- RuntimeError: If processing fails"""
59
- try:
60
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
61
-
62
- file = client.files.upload(file=file_path)
63
-
64
- response = client.models.generate_content(
65
- model=IMAGE_ANALYSIS_MODEL,
66
- contents=[file, question]
67
- )
68
-
69
- return response.text
70
- except Exception as e:
71
- raise RuntimeError(f"Processing failed: {str(e)}")
72
-
73
- @tool("Audio Analysis Tool")
74
- def audio_analysis_tool(question: str, file_path: str) -> str:
75
- """Given a question and audio file, analyze the audio to answer the question.
76
-
77
- Args:
78
- question (str): Question about an audio file
79
- file_path (str): The audio file path
80
-
81
- Returns:
82
- str: Answer to the question about the audio file
83
-
84
- Raises:
85
- RuntimeError: If processing fails"""
86
- try:
87
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
88
-
89
- file = client.files.upload(file=file_path)
90
-
91
- response = client.models.generate_content(
92
- model=AUDIO_ANALYSIS_MODEL,
93
- contents=[file, question]
94
- )
95
-
96
- return response.text
97
- except Exception as e:
98
- raise RuntimeError(f"Processing failed: {str(e)}")
99
-
100
- @tool("Video Analysis Tool")
101
- def video_analysis_tool(question: str, file_path: str) -> str:
102
- """Given a question and video file, analyze the video to answer the question.
103
-
104
- Args:
105
- question (str): Question about a video file
106
- file_path (str): The video file path
107
-
108
- Returns:
109
- str: Answer to the question about the video file
110
-
111
- Raises:
112
- RuntimeError: If processing fails"""
113
- try:
114
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
115
-
116
- file = client.files.upload(file=file_path)
117
-
118
- response = client.models.generate_content(
119
- model=VIDEO_ANALYSIS_MODEL,
120
- contents=[file, question]
121
- )
122
-
123
- return response.text
124
- except Exception as e:
125
- raise RuntimeError(f"Processing failed: {str(e)}")
126
-
127
- @tool("YouTube Analysis Tool")
128
- def youtube_analysis_tool(question: str, url: str) -> str:
129
- """Given a question and YouTube URL, analyze the video to answer the question.
130
-
131
- Args:
132
- question (str): Question about a YouTube video
133
- url (str): The YouTube URL
134
-
135
- Returns:
136
- str: Answer to the question about the YouTube video
137
-
138
- Raises:
139
- RuntimeError: If processing fails"""
140
- try:
141
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
142
-
143
- return client.models.generate_content(
144
- model=YOUTUBE_ANALYSIS_MODEL,
145
- contents=types.Content(
146
- parts=[types.Part(file_data=types.FileData(file_uri=url)),
147
- types.Part(text=question)]
148
- )
149
- )
150
- except Exception as e:
151
- raise RuntimeError(f"Processing failed: {str(e)}")
152
-
153
- @tool("Document Analysis Tool")
154
- def document_analysis_tool(question: str, file_path: str) -> str:
155
- """Given a question and document file, analyze the document to answer the question.
156
-
157
- Args:
158
- question (str): Question about a document file
159
- file_path (str): The document file path
160
-
161
- Returns:
162
- str: Answer to the question about the document file
163
-
164
- Raises:
165
- RuntimeError: If processing fails"""
166
- try:
167
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
168
-
169
- contents = []
170
-
171
- if is_ext(file_path, ".docx"):
172
- text_data = read_docx_text(file_path)
173
- contents = [f"{question}\n{text_data}"]
174
- print(f"=> Text data:\n{text_data}")
175
- elif is_ext(file_path, ".pptx"):
176
- text_data = read_pptx_text(file_path)
177
- contents = [f"{question}\n{text_data}"]
178
- print(f"=> Text data:\n{text_data}")
179
- else:
180
- file = client.files.upload(file=file_path)
181
- contents = [file, question]
182
-
183
- response = client.models.generate_content(
184
- model=DOCUMENT_ANALYSIS_MODEL,
185
- contents=contents
186
- )
187
-
188
- return response.text
189
- except Exception as e:
190
- raise RuntimeError(f"Processing failed: {str(e)}")
191
-
192
- @tool("Arithmetic Tool")
193
- def arithmetic_tool(question: str, a: float, b: float) -> float:
194
- """Given a question and two numbers, perform the calculation to answer the question.
195
-
196
- Args:
197
- question (str): Question to answer
198
- a (float): First number
199
- b (float): Second number
200
-
201
- Returns:
202
- float: Result number
203
-
204
- Raises:
205
- RuntimeError: If processing fails"""
206
- try:
207
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
208
-
209
- response = client.models.generate_content(
210
- model=ARITHMETIC_MODEL,
211
- contents=question,
212
- config=types.GenerateContentConfig(
213
- tools=[add, subtract, multiply, divide, modulus]
214
- )
215
- )
216
-
217
- return response.text
218
- except Exception as e:
219
- raise RuntimeError(f"Processing failed: {str(e)}")
220
-
221
- @tool("Code Execution Tool")
222
- def code_execution_tool(question: str, file_path: str) -> str:
223
- """Given a question and Python file, execute the file to answer the question.
224
-
225
- Args:
226
- question (str): Question to answer
227
- file_path (str): The Python file path
228
-
229
- Returns:
230
- str: Answer to the question
231
-
232
- Raises:
233
- RuntimeError: If processing fails"""
234
- try:
235
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
236
-
237
- file = client.files.upload(file=file_path)
238
-
239
- response = client.models.generate_content(
240
- model=CODE_EXECUTION_MODEL,
241
- contents=[file, question],
242
- config=types.GenerateContentConfig(
243
- tools=[types.Tool(code_execution=types.ToolCodeExecution)]
244
- ),
245
- )
246
-
247
- for part in response.candidates[0].content.parts:
248
- if part.code_execution_result is not None:
249
- return part.code_execution_result.output
250
- except Exception as e:
251
- raise RuntimeError(f"Processing failed: {str(e)}")
252
-
253
- @tool("Code Generation Tool")
254
- def code_generation_tool(question: str, json_data: str) -> str:
255
- """Given a question and JSON data, generate and execute code to answer the question.
256
- Args:
257
- question (str): Question to answer
258
- file_path (str): The JSON data
259
- Returns:
260
- str: Answer to the question
261
-
262
- Raises:
263
- RuntimeError: If processing fails"""
264
- try:
265
- client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
266
-
267
- response = client.models.generate_content(
268
- model=CODE_GENERATION_MODEL,
269
- contents=[f"{question}\n{json_data}"],
270
- config=types.GenerateContentConfig(
271
- tools=[types.Tool(code_execution=types.ToolCodeExecution)]
272
- ),
273
- )
274
-
275
- for part in response.candidates[0].content.parts:
276
- if part.code_execution_result is not None:
277
- return part.code_execution_result.output
278
- except Exception as e:
279
- raise RuntimeError(f"Processing failed: {str(e)}")
280
-
281
  # Arithmetic functions
282
 
283
  @tool("Addition Tool")
 
16
  CODE_GENERATION_MODEL = "gemini-2.5-flash-preview-04-17"
17
  CODE_EXECUTION_MODEL = "gemini-2.5-flash-preview-04-17"
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Arithmetic functions
20
 
21
  @tool("Addition Tool")