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(
goal: Annotated[str, typer.Option()],
api_key: Optional[str] = None,
api_key: str = '<unset>',
tool_endpoint: Optional[list[str]] = None,
max_iterations: Optional[int] = 10,
verbose: bool = False,

View File

@ -5,12 +5,10 @@
# ]
# ///
import datetime
import json
from pydantic import BaseModel
import sys
import time
import types
from typing import Union, Optional, Dict
from typing import Optional
class Duration(BaseModel):
@ -46,6 +44,7 @@ class Duration(BaseModel):
(self.years or 0)*31536000,
])
class WaitForDuration(BaseModel):
duration: Duration
@ -53,21 +52,20 @@ class WaitForDuration(BaseModel):
sys.stderr.write(f"Waiting for {self.duration}...\n")
time.sleep(self.duration.get_total_seconds)
@staticmethod
def wait_for_duration(duration: Duration) -> None:
'Wait for a certain amount of time before continuing.'
# sys.stderr.write(f"Waiting for {duration}...\n")
time.sleep(duration.get_total_seconds)
@staticmethod
def wait_for_date(target_date: datetime.date) -> None:
f'''
Wait until a specific date is reached before continuing.
Today's date is {datetime.date.today()}
'''
# Get the current date
current_date = datetime.date.today()
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")
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:
"""
@ -102,55 +93,16 @@ def python(code: str) -> str:
from io import StringIO
import sys
# Create an isolated IPython shell instance
shell = InteractiveShell()
# Redirect stdout to capture output
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
sys.stdout = out = StringIO()
try:
# Execute the code
shell.run_cell(code)
except Exception as e:
# Restore stdout before returning
sys.stdout = old_stdout
return f"An error occurred: {e}"
finally:
# Always restore stdout
sys.stdout = old_stdout
# Retrieve the output
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
return out.getvalue()

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')
def strftime_now(format):
now = datetime.datetime.strptime(TEST_DATE, "%Y-%m-%d")
# now = datetime.datetime.now()