tool-call: cleanup tools.py

This commit is contained in:
ochafik 2024-09-28 18:31:51 +01:00
parent 55cf337560
commit c657857e21
3 changed files with 9 additions and 55 deletions

View file

@ -97,7 +97,7 @@ class OpenAPIMethod:
def main( def main(
goal: Annotated[str, typer.Option()], goal: Annotated[str, typer.Option()],
api_key: Optional[str] = None, api_key: str = '<unset>',
tool_endpoint: Optional[list[str]] = None, tool_endpoint: Optional[list[str]] = None,
max_iterations: Optional[int] = 10, max_iterations: Optional[int] = 10,
verbose: bool = False, verbose: bool = False,

View file

@ -5,12 +5,10 @@
# ] # ]
# /// # ///
import datetime import datetime
import json
from pydantic import BaseModel from pydantic import BaseModel
import sys import sys
import time import time
import types from typing import Optional
from typing import Union, Optional, Dict
class Duration(BaseModel): class Duration(BaseModel):
@ -46,6 +44,7 @@ class Duration(BaseModel):
(self.years or 0)*31536000, (self.years or 0)*31536000,
]) ])
class WaitForDuration(BaseModel): class WaitForDuration(BaseModel):
duration: Duration duration: Duration
@ -53,21 +52,20 @@ class WaitForDuration(BaseModel):
sys.stderr.write(f"Waiting for {self.duration}...\n") sys.stderr.write(f"Waiting for {self.duration}...\n")
time.sleep(self.duration.get_total_seconds) time.sleep(self.duration.get_total_seconds)
@staticmethod
def wait_for_duration(duration: Duration) -> None: def wait_for_duration(duration: Duration) -> None:
'Wait for a certain amount of time before continuing.' 'Wait for a certain amount of time before continuing.'
# sys.stderr.write(f"Waiting for {duration}...\n") # sys.stderr.write(f"Waiting for {duration}...\n")
time.sleep(duration.get_total_seconds) time.sleep(duration.get_total_seconds)
@staticmethod
def wait_for_date(target_date: datetime.date) -> None: def wait_for_date(target_date: datetime.date) -> None:
f''' f'''
Wait until a specific date is reached before continuing. Wait until a specific date is reached before continuing.
Today's date is {datetime.date.today()} Today's date is {datetime.date.today()}
''' '''
# Get the current date
current_date = datetime.date.today() current_date = datetime.date.today()
if target_date < current_date: if target_date < current_date:
@ -79,14 +77,7 @@ def wait_for_date(target_date: datetime.date) -> None:
# sys.stderr.write(f"Waiting for {days} days and {seconds} seconds until {target_date}...\n") # sys.stderr.write(f"Waiting for {days} days and {seconds} seconds until {target_date}...\n")
time.sleep(days * 86400 + seconds) time.sleep(days * 86400 + seconds)
# sys.stderr.write(f"Reached the target date: {target_date}\n")
def _is_serializable(obj) -> bool:
try:
json.dumps(obj)
return True
except Exception as e:
return False
def python(code: str) -> str: def python(code: str) -> str:
""" """
@ -102,55 +93,16 @@ def python(code: str) -> str:
from io import StringIO from io import StringIO
import sys import sys
# Create an isolated IPython shell instance
shell = InteractiveShell() shell = InteractiveShell()
# Redirect stdout to capture output
old_stdout = sys.stdout old_stdout = sys.stdout
sys.stdout = mystdout = StringIO() sys.stdout = out = StringIO()
try: try:
# Execute the code
shell.run_cell(code) shell.run_cell(code)
except Exception as e: except Exception as e:
# Restore stdout before returning
sys.stdout = old_stdout
return f"An error occurred: {e}" return f"An error occurred: {e}"
finally: finally:
# Always restore stdout
sys.stdout = old_stdout sys.stdout = old_stdout
# Retrieve the output return out.getvalue()
output = mystdout.getvalue()
return output
# def python(source: str) -> Union[Dict, str]:
# """
# Evaluate a Python program and return the globals it declared.
# Can be used to compute mathematical expressions (e.g. after importing math module).
# Args:
# source: contain valid, executable and pure Python code. Should also import any required Python packages.
# For example: "import math\nresult = math.cos(2) * 10"
# Returns:
# dict | str: A dictionary containing variables declared, or an error message if an exception occurred.
# """
# try:
# namespace = {}
# sys.stderr.write(f"Executing Python program:\n{source}\n")
# exec(source, namespace)
# results = {
# k: v
# for k, v in namespace.items()
# if not k.startswith('_') \
# and not isinstance(v, type) \
# and not isinstance(v, types.ModuleType) \
# and not callable(v) \
# and _is_serializable(v)
# }
# sys.stderr.write(f"Results: {json.dumps(results, indent=2)}\n")
# return results
# except Exception as e:
# msg = f"Error: {sys.exc_info()[1]}"
# sys.stderr.write(f"{msg}\n")
# return msg

View file

@ -83,6 +83,8 @@ def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False)
TEST_DATE = os.environ.get('TEST_DATE', '2024-07-26') TEST_DATE = os.environ.get('TEST_DATE', '2024-07-26')
def strftime_now(format): def strftime_now(format):
now = datetime.datetime.strptime(TEST_DATE, "%Y-%m-%d") now = datetime.datetime.strptime(TEST_DATE, "%Y-%m-%d")
# now = datetime.datetime.now() # now = datetime.datetime.now()