Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,10 @@ import re
|
|
| 7 |
import time
|
| 8 |
import base64
|
| 9 |
import json
|
|
|
|
|
|
|
| 10 |
#from tavily import TavilyClient
|
|
|
|
| 11 |
from langchain_tavily import TavilySearch
|
| 12 |
from langgraph.prebuilt import create_react_agent
|
| 13 |
from langgraph.graph.message import add_messages
|
|
@@ -53,6 +56,7 @@ prompt_recomendado = """You are a general AI assistant. I will ask you a questio
|
|
| 53 |
To assist in your task, you can supervise other agents who perform specific tasks that could not be handled by tools, since they require the processing of another LLM. Below, I will inform you about your assistants:
|
| 54 |
- web_research_agent. Assign web research related tasks to this agent, prioritizing the use of Wikipedia sources
|
| 55 |
- chess_position_review_agent. Assign chess position review related tasks to this agent
|
|
|
|
| 56 |
Assign work to one agent at a time, do not call agents in parallel.
|
| 57 |
Priorize the use of tools and another agents to help in reasoning.
|
| 58 |
When a file or URL is entered at the prompt, use it in tools or other agents, both are prepared to handle files and URLs."""
|
|
@@ -70,12 +74,48 @@ prompt_chess = """You are a chess position reviewing agent.
|
|
| 70 |
After you're done with your tasks, respond to the supervisor directly
|
| 71 |
Respond ONLY with the results of your work, do NOT include ANY other text."""
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
#TOOLS
|
| 74 |
web_search = TavilySearch(
|
| 75 |
max_results=5,
|
| 76 |
topic="general",
|
| 77 |
)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
def chess_image_to_fen_tool(task_id:str, current_player: Literal["black", "white"]) -> Dict[str,str]:
|
| 80 |
"""
|
| 81 |
Convert chess image to FEN (Forsyth-Edwards Notation) notation.
|
|
@@ -214,6 +254,21 @@ def download_file_as_base64(task_id: str) -> str:
|
|
| 214 |
return encoded_str
|
| 215 |
else:
|
| 216 |
raise Exception(f"Failed to download the file. Status code: {response.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
|
|
@@ -247,6 +302,13 @@ chess_position_review_agent = create_react_agent(
|
|
| 247 |
name="chess_position_review_agent"
|
| 248 |
)
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
supervisor = create_supervisor(
|
| 251 |
model=gemini_llm,
|
| 252 |
agents=[web_research_agent,chess_position_review_agent],
|
|
|
|
| 7 |
import time
|
| 8 |
import base64
|
| 9 |
import json
|
| 10 |
+
import sys
|
| 11 |
+
import contextlib
|
| 12 |
#from tavily import TavilyClient
|
| 13 |
+
from io import StringIO
|
| 14 |
from langchain_tavily import TavilySearch
|
| 15 |
from langgraph.prebuilt import create_react_agent
|
| 16 |
from langgraph.graph.message import add_messages
|
|
|
|
| 56 |
To assist in your task, you can supervise other agents who perform specific tasks that could not be handled by tools, since they require the processing of another LLM. Below, I will inform you about your assistants:
|
| 57 |
- web_research_agent. Assign web research related tasks to this agent, prioritizing the use of Wikipedia sources
|
| 58 |
- chess_position_review_agent. Assign chess position review related tasks to this agent
|
| 59 |
+
- python_code_runner_agent. Assign python code execution related tasks to this agent
|
| 60 |
Assign work to one agent at a time, do not call agents in parallel.
|
| 61 |
Priorize the use of tools and another agents to help in reasoning.
|
| 62 |
When a file or URL is entered at the prompt, use it in tools or other agents, both are prepared to handle files and URLs."""
|
|
|
|
| 74 |
After you're done with your tasks, respond to the supervisor directly
|
| 75 |
Respond ONLY with the results of your work, do NOT include ANY other text."""
|
| 76 |
|
| 77 |
+
prompt_python_execute = """You are a python code execution agent.
|
| 78 |
+
INSTRUCTIONS:
|
| 79 |
+
Assist ONLY with tasks related to running python code, DO NOT do any math
|
| 80 |
+
After you're done with your tasks, respond to the supervisor directly
|
| 81 |
+
Respond ONLY with the results of your work, do NOT include ANY other text."""
|
| 82 |
+
|
| 83 |
#TOOLS
|
| 84 |
web_search = TavilySearch(
|
| 85 |
max_results=5,
|
| 86 |
topic="general",
|
| 87 |
)
|
| 88 |
|
| 89 |
+
@contextlib.contextmanager
|
| 90 |
+
def stdoutIO(stdout=None):
|
| 91 |
+
old = sys.stdout
|
| 92 |
+
if stdout is None:
|
| 93 |
+
stdout = StringIO()
|
| 94 |
+
sys.stdout = stdout
|
| 95 |
+
yield stdout
|
| 96 |
+
sys.stdout = old
|
| 97 |
+
|
| 98 |
+
def python_code_runner_tool(task_id:str) -> str:
|
| 99 |
+
"""
|
| 100 |
+
Download and run python code, capturing the output.
|
| 101 |
+
Args:
|
| 102 |
+
task_id: Task ID necessary to retrieve the python code to be run.
|
| 103 |
+
Returns:
|
| 104 |
+
String with the output of the python code.
|
| 105 |
+
"""
|
| 106 |
+
print(f"python code runner invocada com os seguintes parametros:")
|
| 107 |
+
print(f"task_id: {task_id}")
|
| 108 |
+
|
| 109 |
+
python_code = download_file_as_string(task_id)
|
| 110 |
+
|
| 111 |
+
with stdoutIO() as s:
|
| 112 |
+
exec(python_code)
|
| 113 |
+
|
| 114 |
+
output = s.getvalue()
|
| 115 |
+
print(f"Captured output: {output}")
|
| 116 |
+
|
| 117 |
+
return output
|
| 118 |
+
|
| 119 |
def chess_image_to_fen_tool(task_id:str, current_player: Literal["black", "white"]) -> Dict[str,str]:
|
| 120 |
"""
|
| 121 |
Convert chess image to FEN (Forsyth-Edwards Notation) notation.
|
|
|
|
| 254 |
return encoded_str
|
| 255 |
else:
|
| 256 |
raise Exception(f"Failed to download the file. Status code: {response.status_code}")
|
| 257 |
+
|
| 258 |
+
def download_file_as_string(task_id: str) -> str:
|
| 259 |
+
# Construct the URL
|
| 260 |
+
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 261 |
+
|
| 262 |
+
# Send the request to download the file
|
| 263 |
+
response = requests.get(url)
|
| 264 |
+
|
| 265 |
+
if response.status_code == 200:
|
| 266 |
+
# Encode the content to Base64
|
| 267 |
+
bytes = response.content
|
| 268 |
+
encoded_str = bytes.decode('utf-8') # Convert bytes to string
|
| 269 |
+
return encoded_str
|
| 270 |
+
else:
|
| 271 |
+
raise Exception(f"Failed to download the file. Status code: {response.status_code}")
|
| 272 |
|
| 273 |
|
| 274 |
|
|
|
|
| 302 |
name="chess_position_review_agent"
|
| 303 |
)
|
| 304 |
|
| 305 |
+
python_code_runner_agent = create_react_agent(
|
| 306 |
+
model=gemini_llm,
|
| 307 |
+
tools=[python_code_runner_tool],
|
| 308 |
+
prompt=prompt_python_execute,
|
| 309 |
+
name="python_code_runner_agent"
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
supervisor = create_supervisor(
|
| 313 |
model=gemini_llm,
|
| 314 |
agents=[web_research_agent,chess_position_review_agent],
|