Spaces:
Runtime error
Runtime error
added new examples; fixed python tool
Browse files- app.py +51 -22
- octotools/engine/openai.py +4 -4
- octotools/tools/python_code_generator/tool.py +4 -12
app.py
CHANGED
|
@@ -325,18 +325,6 @@ class Solver:
|
|
| 325 |
break
|
| 326 |
|
| 327 |
# Step 7: Generate Final Output (if needed)
|
| 328 |
-
if 'final' in self.output_types:
|
| 329 |
-
final_output = self.planner.generate_final_output(user_query, img_path, self.memory)
|
| 330 |
-
messages.append(ChatMessage(role="assistant", content=f"π― Final Output:\n{final_output}"))
|
| 331 |
-
yield messages
|
| 332 |
-
|
| 333 |
-
# Save the final output data
|
| 334 |
-
final_output_data = {
|
| 335 |
-
"final_output": final_output,
|
| 336 |
-
"time": round(time.time() - start_time, 5)
|
| 337 |
-
}
|
| 338 |
-
save_module_data(QUERY_ID, "final_output", final_output_data)
|
| 339 |
-
|
| 340 |
if 'direct' in self.output_types:
|
| 341 |
direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory)
|
| 342 |
messages.append(ChatMessage(role="assistant", content=f"πΉ Direct Output:\n{direct_output}"))
|
|
@@ -349,6 +337,19 @@ class Solver:
|
|
| 349 |
}
|
| 350 |
save_module_data(QUERY_ID, "direct_output", direct_output_data)
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
# Step 8: Completion Message
|
| 353 |
messages.append(ChatMessage(role="assistant", content="β
Problem-solving process completed."))
|
| 354 |
yield messages
|
|
@@ -605,18 +606,46 @@ def main(args):
|
|
| 605 |
""")
|
| 606 |
gr.Examples(
|
| 607 |
examples=[
|
| 608 |
-
[ None, "Who is the president of the United States?", ["Google_Search_Tool"]],
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
[None,
|
| 615 |
-
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
],
|
| 619 |
-
inputs=[user_image, user_query, enabled_tools],
|
| 620 |
# label="Try these examples with suggested tools."
|
| 621 |
)
|
| 622 |
|
|
|
|
| 325 |
break
|
| 326 |
|
| 327 |
# Step 7: Generate Final Output (if needed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
if 'direct' in self.output_types:
|
| 329 |
direct_output = self.planner.generate_direct_output(user_query, img_path, self.memory)
|
| 330 |
messages.append(ChatMessage(role="assistant", content=f"πΉ Direct Output:\n{direct_output}"))
|
|
|
|
| 337 |
}
|
| 338 |
save_module_data(QUERY_ID, "direct_output", direct_output_data)
|
| 339 |
|
| 340 |
+
|
| 341 |
+
if 'final' in self.output_types:
|
| 342 |
+
final_output = self.planner.generate_final_output(user_query, img_path, self.memory) # Disabled visibility for now
|
| 343 |
+
# messages.append(ChatMessage(role="assistant", content=f"π― Final Output:\n{final_output}"))
|
| 344 |
+
# yield messages
|
| 345 |
+
|
| 346 |
+
# Save the final output data
|
| 347 |
+
final_output_data = {
|
| 348 |
+
"final_output": final_output,
|
| 349 |
+
"time": round(time.time() - start_time, 5)
|
| 350 |
+
}
|
| 351 |
+
save_module_data(QUERY_ID, "final_output", final_output_data)
|
| 352 |
+
|
| 353 |
# Step 8: Completion Message
|
| 354 |
messages.append(ChatMessage(role="assistant", content="β
Problem-solving process completed."))
|
| 355 |
yield messages
|
|
|
|
| 606 |
""")
|
| 607 |
gr.Examples(
|
| 608 |
examples=[
|
| 609 |
+
# [ None, "Who is the president of the United States?", ["Google_Search_Tool"]],
|
| 610 |
+
[ None,
|
| 611 |
+
"How many r letters are in the word strawberry?",
|
| 612 |
+
["Generalist_Solution_Generator_Tool", "Python_Code_Generator_Tool"],
|
| 613 |
+
"3"],
|
| 614 |
+
|
| 615 |
+
[ None,
|
| 616 |
+
"What's up with the upcoming Apple Launch? Any rumors?",
|
| 617 |
+
["Generalist_Solution_Generator_Tool", "Google_Search_Tool", "Wikipedia_Knowledge_Searcher_Tool", "URL_Text_Extractor_Tool"],
|
| 618 |
+
"Apple's February 19, 2025, event may feature the iPhone SE 4, new iPads, accessories, and rumored iPhone 17 and Apple Watch Series 10."],
|
| 619 |
+
|
| 620 |
+
[ None,
|
| 621 |
+
"Which is bigger, 9.11 or 9.9?",
|
| 622 |
+
["Generalist_Solution_Generator_Tool", "Python_Code_Generator_Tool"],
|
| 623 |
+
"9.9"],
|
| 624 |
+
|
| 625 |
+
[ "examples/baseball.png",
|
| 626 |
+
"How many baseballs are there?",
|
| 627 |
+
["Object_Detector_Tool"],
|
| 628 |
+
"20"],
|
| 629 |
+
|
| 630 |
+
[ None,
|
| 631 |
+
"Using the numbers [1, 1, 6, 9], create an expression that equals 24. You must use basic arithmetic operations (+, -, Γ, /) and parentheses. For example, one solution for [1, 2, 3, 4] is (1+2+3)Γ4.", ["Python_Code_Generator_Tool"],
|
| 632 |
+
"((1 + 1) * 9) + 6"],
|
| 633 |
+
|
| 634 |
+
[None,
|
| 635 |
+
"What are the research trends in tool agents with large language models for scientific discovery? Please consider the latest literature from ArXiv, PubMed, Nature, and news sources.", ["ArXiv_Paper_Searcher_Tool", "Pubmed_Search_Tool", "Nature_News_Fetcher_Tool"],
|
| 636 |
+
"Open-ended question. No reference answer."],
|
| 637 |
+
|
| 638 |
+
[ "examples/rotting_kiwi.png",
|
| 639 |
+
"You are given a 3 x 3 grid in which each cell can contain either no kiwi, one fresh kiwi, or one rotten kiwi. Every minute, any fresh kiwi that is 4-directionally adjacent to a rotten kiwi also becomes rotten. What is the minimum number of minutes that must elapse until no cell has a fresh kiwi?", ["Image_Captioner_Tool"],
|
| 640 |
+
"4 minutes"],
|
| 641 |
+
|
| 642 |
+
[ "examples/lung.jpg",
|
| 643 |
+
"What is the organ on the left side of this image?",
|
| 644 |
+
["Image_Captioner_Tool", "Relevant_Patch_Zoomer_Tool"],
|
| 645 |
+
"Lung"],
|
| 646 |
|
| 647 |
],
|
| 648 |
+
inputs=[user_image, user_query, enabled_tools, gr.Textbox(label="Reference Answer")],
|
| 649 |
# label="Try these examples with suggested tools."
|
| 650 |
)
|
| 651 |
|
octotools/engine/openai.py
CHANGED
|
@@ -41,8 +41,8 @@ class ChatOpenAI(EngineLM, CachedEngine):
|
|
| 41 |
model_string="gpt-4o-mini-2024-07-18",
|
| 42 |
system_prompt=DEFAULT_SYSTEM_PROMPT,
|
| 43 |
is_multimodal: bool=False,
|
| 44 |
-
enable_cache: bool=True,
|
| 45 |
-
|
| 46 |
api_key: str=None,
|
| 47 |
**kwargs):
|
| 48 |
"""
|
|
@@ -124,7 +124,7 @@ class ChatOpenAI(EngineLM, CachedEngine):
|
|
| 124 |
}
|
| 125 |
|
| 126 |
def _generate_text(
|
| 127 |
-
self, prompt, system_prompt=None, temperature=0, max_tokens=4000, top_p=0.99, response_format=None
|
| 128 |
):
|
| 129 |
|
| 130 |
sys_prompt_arg = system_prompt if system_prompt else self.system_prompt
|
|
@@ -210,7 +210,7 @@ class ChatOpenAI(EngineLM, CachedEngine):
|
|
| 210 |
return formatted_content
|
| 211 |
|
| 212 |
def _generate_multimodal(
|
| 213 |
-
self, content: List[Union[str, bytes]], system_prompt=None, temperature=0, max_tokens=4000, top_p=0.99, response_format=None
|
| 214 |
):
|
| 215 |
sys_prompt_arg = system_prompt if system_prompt else self.system_prompt
|
| 216 |
formatted_content = self._format_content(content)
|
|
|
|
| 41 |
model_string="gpt-4o-mini-2024-07-18",
|
| 42 |
system_prompt=DEFAULT_SYSTEM_PROMPT,
|
| 43 |
is_multimodal: bool=False,
|
| 44 |
+
# enable_cache: bool=True,
|
| 45 |
+
enable_cache: bool=False, # NOTE: disable cache for now
|
| 46 |
api_key: str=None,
|
| 47 |
**kwargs):
|
| 48 |
"""
|
|
|
|
| 124 |
}
|
| 125 |
|
| 126 |
def _generate_text(
|
| 127 |
+
self, prompt, system_prompt=None, temperature=0.5, max_tokens=4000, top_p=0.99, response_format=None
|
| 128 |
):
|
| 129 |
|
| 130 |
sys_prompt_arg = system_prompt if system_prompt else self.system_prompt
|
|
|
|
| 210 |
return formatted_content
|
| 211 |
|
| 212 |
def _generate_multimodal(
|
| 213 |
+
self, content: List[Union[str, bytes]], system_prompt=None, temperature=0.5, max_tokens=4000, top_p=0.99, response_format=None
|
| 214 |
):
|
| 215 |
sys_prompt_arg = system_prompt if system_prompt else self.system_prompt
|
| 216 |
formatted_content = self._format_content(content)
|
octotools/tools/python_code_generator/tool.py
CHANGED
|
@@ -6,11 +6,10 @@ import sys
|
|
| 6 |
from io import StringIO
|
| 7 |
import contextlib
|
| 8 |
|
| 9 |
-
|
| 10 |
from octotools.tools.base import BaseTool
|
| 11 |
from octotools.engine.openai import ChatOpenAI
|
| 12 |
|
| 13 |
-
import
|
| 14 |
from contextlib import contextmanager
|
| 15 |
|
| 16 |
# Custom exception for code execution timeout
|
|
@@ -20,19 +19,12 @@ class TimeoutException(Exception):
|
|
| 20 |
# Custom context manager for code execution timeout
|
| 21 |
@contextmanager
|
| 22 |
def timeout(seconds):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Set the timeout handler
|
| 27 |
-
original_handler = signal.signal(signal.SIGALRM, timeout_handler)
|
| 28 |
-
signal.alarm(seconds)
|
| 29 |
-
|
| 30 |
try:
|
| 31 |
yield
|
| 32 |
finally:
|
| 33 |
-
|
| 34 |
-
signal.alarm(0)
|
| 35 |
-
signal.signal(signal.SIGALRM, original_handler)
|
| 36 |
|
| 37 |
|
| 38 |
class Python_Code_Generator_Tool(BaseTool):
|
|
|
|
| 6 |
from io import StringIO
|
| 7 |
import contextlib
|
| 8 |
|
|
|
|
| 9 |
from octotools.tools.base import BaseTool
|
| 10 |
from octotools.engine.openai import ChatOpenAI
|
| 11 |
|
| 12 |
+
import threading
|
| 13 |
from contextlib import contextmanager
|
| 14 |
|
| 15 |
# Custom exception for code execution timeout
|
|
|
|
| 19 |
# Custom context manager for code execution timeout
|
| 20 |
@contextmanager
|
| 21 |
def timeout(seconds):
|
| 22 |
+
timer = threading.Timer(seconds, lambda: (_ for _ in ()).throw(TimeoutException("Code execution timed out")))
|
| 23 |
+
timer.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
yield
|
| 26 |
finally:
|
| 27 |
+
timer.cancel()
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
class Python_Code_Generator_Tool(BaseTool):
|