Update agents/tools/ai_tools.py
Browse files- agents/tools/ai_tools.py +106 -135
agents/tools/ai_tools.py
CHANGED
|
@@ -7,21 +7,25 @@
|
|
| 7 |
import base64, chess, os
|
| 8 |
from agents.models.llms import (
|
| 9 |
LLM_WEB_SEARCH,
|
| 10 |
-
|
| 11 |
LLM_IMAGE_ANALYSIS,
|
| 12 |
LLM_AUDIO_ANALYSIS,
|
| 13 |
LLM_VIDEO_ANALYSIS,
|
| 14 |
LLM_YOUTUBE_ANALYSIS,
|
| 15 |
LLM_DOCUMENT_ANALYSIS,
|
| 16 |
-
LLM_ARITHMETIC,
|
| 17 |
LLM_CODE_GENERATION,
|
| 18 |
LLM_CODE_EXECUTION,
|
| 19 |
-
|
|
|
|
| 20 |
LLM_FINAL_ANSWER,
|
| 21 |
THINKING_LEVEL_TOOLS,
|
| 22 |
THINKING_LEVEL_FINAL_ANSWER
|
| 23 |
)
|
| 24 |
-
from agents.models.prompts import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
from crewai.tools import tool
|
| 26 |
from crewai_tools import StagehandTool
|
| 27 |
from google import genai
|
|
@@ -106,123 +110,6 @@ class AITools():
|
|
| 106 |
except Exception as e:
|
| 107 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 108 |
|
| 109 |
-
@tool("Image to FEN Tool")
|
| 110 |
-
def img_to_fen_tool(question: str, file_path: str) -> str:
|
| 111 |
-
"""Given a chess question and image file, return the FEN.
|
| 112 |
-
|
| 113 |
-
Args:
|
| 114 |
-
question (str): The chess question
|
| 115 |
-
file_path (str): The image file path
|
| 116 |
-
|
| 117 |
-
Returns:
|
| 118 |
-
str: FEN of the chess position
|
| 119 |
-
|
| 120 |
-
Raises:
|
| 121 |
-
RuntimeError: If processing fails
|
| 122 |
-
"""
|
| 123 |
-
try:
|
| 124 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 125 |
-
|
| 126 |
-
with open(file_path, "rb") as f:
|
| 127 |
-
img_bytes = f.read()
|
| 128 |
-
img_b64 = base64.b64encode(img_bytes).decode("ascii")
|
| 129 |
-
|
| 130 |
-
prompt = PROMPT_IMG_TO_FEN.format(question=question)
|
| 131 |
-
|
| 132 |
-
content = types.Content(
|
| 133 |
-
parts=[
|
| 134 |
-
types.Part(text=prompt),
|
| 135 |
-
types.Part(
|
| 136 |
-
inline_data=types.Blob(
|
| 137 |
-
mime_type="image/png",
|
| 138 |
-
data=base64.b64decode(img_b64),
|
| 139 |
-
)
|
| 140 |
-
)
|
| 141 |
-
]
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
response = client.models.generate_content(
|
| 145 |
-
model=LLM_IMAGE_TO_FEN,
|
| 146 |
-
contents=[content],
|
| 147 |
-
config=types.GenerateContentConfig(
|
| 148 |
-
thinking_config=types.ThinkingConfig(
|
| 149 |
-
thinking_level=THINKING_LEVEL_TOOLS
|
| 150 |
-
)
|
| 151 |
-
)
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
fen = None
|
| 155 |
-
|
| 156 |
-
for part in response.parts:
|
| 157 |
-
if part.text is not None:
|
| 158 |
-
fen = part.text.strip()
|
| 159 |
-
break
|
| 160 |
-
|
| 161 |
-
board = chess.Board(fen) # FEN validation
|
| 162 |
-
|
| 163 |
-
print(f"🤖 FEN: {fen}")
|
| 164 |
-
|
| 165 |
-
return fen;
|
| 166 |
-
except Exception as e:
|
| 167 |
-
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 168 |
-
|
| 169 |
-
@tool("Algebraic Chess Notation Tool")
|
| 170 |
-
def algebraic_chess_notation_tool(question: str, file_path: str, best_move: str) -> str:
|
| 171 |
-
"""Given a chess question, image file, and best move with continuation in UCI notation, answer the question in algebraic notation.
|
| 172 |
-
|
| 173 |
-
Args:
|
| 174 |
-
question (str): The chess question
|
| 175 |
-
file_path (str): The image file path
|
| 176 |
-
best_move (str): The best move with continuation in UCI notation
|
| 177 |
-
|
| 178 |
-
Returns:
|
| 179 |
-
str: Answer to the question in algebraic notation
|
| 180 |
-
|
| 181 |
-
Raises:
|
| 182 |
-
RuntimeError: If processing fails
|
| 183 |
-
"""
|
| 184 |
-
try:
|
| 185 |
-
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 186 |
-
|
| 187 |
-
with open(file_path, "rb") as f:
|
| 188 |
-
img_bytes = f.read()
|
| 189 |
-
img_b64 = base64.b64encode(img_bytes).decode("ascii")
|
| 190 |
-
|
| 191 |
-
prompt = PROMPT_UCI_TO_ALGEBRAIC.format(question=question, best_move=best_move)
|
| 192 |
-
|
| 193 |
-
content = types.Content(
|
| 194 |
-
parts=[
|
| 195 |
-
types.Part(text=prompt),
|
| 196 |
-
types.Part(
|
| 197 |
-
inline_data=types.Blob(
|
| 198 |
-
mime_type="image/png",
|
| 199 |
-
data=base64.b64decode(img_b64),
|
| 200 |
-
)
|
| 201 |
-
)
|
| 202 |
-
]
|
| 203 |
-
)
|
| 204 |
-
|
| 205 |
-
response = client.models.generate_content(
|
| 206 |
-
model=LLM_UCI_TO_ALGEBRAIC,
|
| 207 |
-
contents=[content],
|
| 208 |
-
config=types.GenerateContentConfig(
|
| 209 |
-
thinking_config=types.ThinkingConfig(
|
| 210 |
-
thinking_level=THINKING_LEVEL_TOOLS
|
| 211 |
-
)
|
| 212 |
-
)
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
for part in response.parts:
|
| 216 |
-
if part.text is not None:
|
| 217 |
-
result = part.text.strip()
|
| 218 |
-
break
|
| 219 |
-
|
| 220 |
-
print(f"🤖 Algebraic notation: {result}")
|
| 221 |
-
|
| 222 |
-
return result;
|
| 223 |
-
except Exception as e:
|
| 224 |
-
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 225 |
-
|
| 226 |
@tool("Image Analysis Tool")
|
| 227 |
def image_analysis_tool(question: str, file_path: str) -> str:
|
| 228 |
"""Given a question and image file, analyze the image to answer the question.
|
|
@@ -468,36 +355,120 @@ class AITools():
|
|
| 468 |
except Exception as e:
|
| 469 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 470 |
|
| 471 |
-
@tool("
|
| 472 |
-
def
|
| 473 |
-
"""Given a question and
|
| 474 |
|
| 475 |
Args:
|
| 476 |
-
question (str):
|
| 477 |
-
|
| 478 |
|
| 479 |
Returns:
|
| 480 |
-
str:
|
| 481 |
|
| 482 |
Raises:
|
| 483 |
RuntimeError: If processing fails
|
| 484 |
"""
|
| 485 |
-
try:
|
| 486 |
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
response = client.models.generate_content(
|
| 491 |
-
model=
|
| 492 |
-
contents=[
|
| 493 |
config=types.GenerateContentConfig(
|
| 494 |
thinking_config=types.ThinkingConfig(
|
| 495 |
-
thinking_level=
|
| 496 |
)
|
| 497 |
)
|
| 498 |
)
|
| 499 |
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
except Exception as e:
|
| 502 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 503 |
|
|
|
|
| 7 |
import base64, chess, os
|
| 8 |
from agents.models.llms import (
|
| 9 |
LLM_WEB_SEARCH,
|
| 10 |
+
LLM_WEB_BROWSER,
|
| 11 |
LLM_IMAGE_ANALYSIS,
|
| 12 |
LLM_AUDIO_ANALYSIS,
|
| 13 |
LLM_VIDEO_ANALYSIS,
|
| 14 |
LLM_YOUTUBE_ANALYSIS,
|
| 15 |
LLM_DOCUMENT_ANALYSIS,
|
|
|
|
| 16 |
LLM_CODE_GENERATION,
|
| 17 |
LLM_CODE_EXECUTION,
|
| 18 |
+
LLM_IMAGE_TO_FEN,
|
| 19 |
+
LLM_ALGEBRAIC_CHESS_NOTATION,
|
| 20 |
LLM_FINAL_ANSWER,
|
| 21 |
THINKING_LEVEL_TOOLS,
|
| 22 |
THINKING_LEVEL_FINAL_ANSWER
|
| 23 |
)
|
| 24 |
+
from agents.models.prompts import (
|
| 25 |
+
PROMPT_IMG_TO_FEN,
|
| 26 |
+
PROMPT_ALGEBRAIC_CHESS_NOTATION,
|
| 27 |
+
PROMPT_FINAL_ANSWER
|
| 28 |
+
)
|
| 29 |
from crewai.tools import tool
|
| 30 |
from crewai_tools import StagehandTool
|
| 31 |
from google import genai
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
@tool("Image Analysis Tool")
|
| 114 |
def image_analysis_tool(question: str, file_path: str) -> str:
|
| 115 |
"""Given a question and image file, analyze the image to answer the question.
|
|
|
|
| 355 |
except Exception as e:
|
| 356 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 357 |
|
| 358 |
+
@tool("Image to FEN Tool")
|
| 359 |
+
def img_to_fen_tool(question: str, file_path: str) -> str:
|
| 360 |
+
"""Given a chess question and image file, return the FEN.
|
| 361 |
|
| 362 |
Args:
|
| 363 |
+
question (str): The chess question
|
| 364 |
+
file_path (str): The image file path
|
| 365 |
|
| 366 |
Returns:
|
| 367 |
+
str: FEN of the chess position
|
| 368 |
|
| 369 |
Raises:
|
| 370 |
RuntimeError: If processing fails
|
| 371 |
"""
|
| 372 |
+
try:
|
| 373 |
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 374 |
+
|
| 375 |
+
with open(file_path, "rb") as f:
|
| 376 |
+
img_bytes = f.read()
|
| 377 |
+
img_b64 = base64.b64encode(img_bytes).decode("ascii")
|
| 378 |
+
|
| 379 |
+
prompt = PROMPT_IMG_TO_FEN.format(question=question)
|
| 380 |
+
|
| 381 |
+
content = types.Content(
|
| 382 |
+
parts=[
|
| 383 |
+
types.Part(text=prompt),
|
| 384 |
+
types.Part(
|
| 385 |
+
inline_data=types.Blob(
|
| 386 |
+
mime_type="image/png",
|
| 387 |
+
data=base64.b64decode(img_b64),
|
| 388 |
+
)
|
| 389 |
+
)
|
| 390 |
+
]
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
response = client.models.generate_content(
|
| 394 |
+
model=LLM_IMAGE_TO_FEN,
|
| 395 |
+
contents=[content],
|
| 396 |
+
config=types.GenerateContentConfig(
|
| 397 |
+
thinking_config=types.ThinkingConfig(
|
| 398 |
+
thinking_level=THINKING_LEVEL_TOOLS
|
| 399 |
+
)
|
| 400 |
+
)
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
fen = None
|
| 404 |
+
|
| 405 |
+
for part in response.parts:
|
| 406 |
+
if part.text is not None:
|
| 407 |
+
fen = part.text.strip()
|
| 408 |
+
break
|
| 409 |
+
|
| 410 |
+
board = chess.Board(fen) # FEN validation
|
| 411 |
+
|
| 412 |
+
print(f"🤖 FEN: {fen}")
|
| 413 |
+
|
| 414 |
+
return fen;
|
| 415 |
+
except Exception as e:
|
| 416 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 417 |
+
|
| 418 |
+
@tool("Algebraic Chess Notation Tool")
|
| 419 |
+
def algebraic_chess_notation_tool(question: str, file_path: str, best_move: str) -> str:
|
| 420 |
+
"""Given a chess question, image file, and best move with continuation in UCI notation, answer the question in algebraic notation.
|
| 421 |
+
|
| 422 |
+
Args:
|
| 423 |
+
question (str): The chess question
|
| 424 |
+
file_path (str): The image file path
|
| 425 |
+
best_move (str): The best move with continuation in UCI notation
|
| 426 |
+
|
| 427 |
+
Returns:
|
| 428 |
+
str: Answer to the question in algebraic notation
|
| 429 |
+
|
| 430 |
+
Raises:
|
| 431 |
+
RuntimeError: If processing fails
|
| 432 |
+
"""
|
| 433 |
+
try:
|
| 434 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 435 |
+
|
| 436 |
+
with open(file_path, "rb") as f:
|
| 437 |
+
img_bytes = f.read()
|
| 438 |
+
img_b64 = base64.b64encode(img_bytes).decode("ascii")
|
| 439 |
+
|
| 440 |
+
prompt = PROMPT_ALGEBRAIC_CHESS_NOTATION.format(question=question, best_move=best_move)
|
| 441 |
+
|
| 442 |
+
content = types.Content(
|
| 443 |
+
parts=[
|
| 444 |
+
types.Part(text=prompt),
|
| 445 |
+
types.Part(
|
| 446 |
+
inline_data=types.Blob(
|
| 447 |
+
mime_type="image/png",
|
| 448 |
+
data=base64.b64decode(img_b64),
|
| 449 |
+
)
|
| 450 |
+
)
|
| 451 |
+
]
|
| 452 |
+
)
|
| 453 |
+
|
| 454 |
response = client.models.generate_content(
|
| 455 |
+
model=LLM_ALGEBRAIC_CHESS_NOTATION,
|
| 456 |
+
contents=[content],
|
| 457 |
config=types.GenerateContentConfig(
|
| 458 |
thinking_config=types.ThinkingConfig(
|
| 459 |
+
thinking_level=THINKING_LEVEL_TOOLS
|
| 460 |
)
|
| 461 |
)
|
| 462 |
)
|
| 463 |
|
| 464 |
+
for part in response.parts:
|
| 465 |
+
if part.text is not None:
|
| 466 |
+
result = part.text.strip()
|
| 467 |
+
break
|
| 468 |
+
|
| 469 |
+
print(f"🤖 Algebraic notation: {result}")
|
| 470 |
+
|
| 471 |
+
return result;
|
| 472 |
except Exception as e:
|
| 473 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 474 |
|