|
14 | 14 | import logging
|
15 | 15 | import typing
|
16 | 16 | import random
|
| 17 | +import re |
17 | 18 |
|
18 | 19 | class APIParams:
|
19 | 20 | def __init__(self):
|
@@ -104,8 +105,7 @@ async def _fetch_tool(self, db_conn) -> dict:
|
104 | 105 | # Check if tool is code execution
|
105 | 106 | if _Tool:
|
106 | 107 | if "gemini-2.0-flash-thinking" in self._model_name:
|
107 |
| - await self._discord_method_send("> ⚠️ The Gemini 2.0 Flash Thinking only supports code execution as a tool, tools won't be used with this model.") |
108 |
| - _tool_schema = None |
| 108 | + raise CustomErrorMessage("⚠️ The Gemini 2.0 Flash Thinking doesn't support tools, please switch to another Gemini model.") |
109 | 109 | else:
|
110 | 110 | if _tool_selection_name == "code_execution":
|
111 | 111 | _tool_schema = [types.Tool(code_execution=types.ToolCodeExecution())]
|
@@ -206,6 +206,35 @@ async def chat_completion(self, prompt, db_conn, system_instruction: str = None)
|
206 | 206 | if _chat_thread is None:
|
207 | 207 | _chat_thread = []
|
208 | 208 |
|
| 209 | + # Check if YouTube link is in the prompt |
| 210 | + if "/youtube:" in prompt: |
| 211 | + _REGEX_YOUTUDOTBE = r"https:\/\/youtu.be\/[\w|-]+" |
| 212 | + _REGEX_YOUTUBEDOTCOM = r"https:\/\/(www.youtube.com|youtube.com)\/watch\?v=[\w|-]+" |
| 213 | + |
| 214 | + # Extract the URL and remove parameters if exists |
| 215 | + if "youtu.be" in prompt: |
| 216 | + _youtube_url = re.search(_REGEX_YOUTUDOTBE, prompt)[0] |
| 217 | + # Remove the URL from the prompt |
| 218 | + prompt = re.sub(fr"\/youtube:{_REGEX_YOUTUDOTBE}", "", prompt) |
| 219 | + else: |
| 220 | + _youtube_url = re.search(_REGEX_YOUTUBEDOTCOM, prompt)[0] |
| 221 | + # Remove the URL from the prompt |
| 222 | + prompt = re.sub(fr"\/youtube:{_REGEX_YOUTUBEDOTCOM}", "", prompt) |
| 223 | + |
| 224 | + if _youtube_url: |
| 225 | + # Add it to part |
| 226 | + logging.info("YouTube URL detected: %s", _youtube_url) |
| 227 | + await self._discord_method_send(f"✅ Watching YouTube Video: **<{_youtube_url}>\nNote: You can only include one YouTube video per conversation. To add more videos, clear the chat history**") |
| 228 | + _chat_thread.append( |
| 229 | + types.Content( |
| 230 | + parts=[types.Part.from_uri( |
| 231 | + file_uri=_youtube_url, |
| 232 | + mime_type="video/*" |
| 233 | + )], |
| 234 | + role="user" |
| 235 | + ).model_dump(exclude_unset=True) |
| 236 | + ) |
| 237 | + |
209 | 238 | # Attach file attachment if it exists
|
210 | 239 | if hasattr(self, "_file_data"): _chat_thread.append(self._file_data)
|
211 | 240 |
|
@@ -250,7 +279,6 @@ async def chat_completion(self, prompt, db_conn, system_instruction: str = None)
|
250 | 279 | elif _response.candidates[0].finish_reason != "STOP":
|
251 | 280 | raise CustomErrorMessage("⚠️ An error has occurred while giving you an answer, please try again later.")
|
252 | 281 |
|
253 |
| - |
254 | 282 | # Iterate through the parts and perform tasks
|
255 | 283 | _toolParts = []
|
256 | 284 | _toHalt = False
|
@@ -309,7 +337,7 @@ async def chat_completion(self, prompt, db_conn, system_instruction: str = None)
|
309 | 337 | _toolParts.append(types.Part.from_function_response(
|
310 | 338 | name=_part.function_call.name,
|
311 | 339 | response=_toolResult
|
312 |
| - ).model_dump(exclude_unset=True) |
| 340 | + ) |
313 | 341 | )
|
314 | 342 |
|
315 | 343 | # Function calling and code execution doesn't mix
|
@@ -339,7 +367,7 @@ async def chat_completion(self, prompt, db_conn, system_instruction: str = None)
|
339 | 367 | await _interstitial.edit(f"✅ Used: **{_Tool['tool_human_name']}**")
|
340 | 368 |
|
341 | 369 | # Append the tool parts to the chat thread
|
342 |
| - _chat_thread.append(types.Content(parts=_toolParts)) |
| 370 | + _chat_thread.append(types.Content(parts=_toolParts).model_dump(exclude_unset=True)) |
343 | 371 |
|
344 | 372 | # Add function call parts to the response
|
345 | 373 | _response = await self.completion(prompt=_chat_thread, system_instruction=system_instruction, return_text=False)
|
|
0 commit comments