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