From a151ddcd5a9f896ff206dcbb2d0245963c4c571c Mon Sep 17 00:00:00 2001 From: ochafik Date: Fri, 4 Oct 2024 04:06:00 +0100 Subject: [PATCH] `agent`: handle function errors and dont' stringify str outputs --- examples/agent/run.py | 13 +++++++++---- examples/agent/serve_tools_inside_docker.sh | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/agent/run.py b/examples/agent/run.py index c89bf3b16..287262035 100644 --- a/examples/agent/run.py +++ b/examples/agent/run.py @@ -97,6 +97,8 @@ class OpenAPIMethod: url = f'{self.url}?{params}' async with aiohttp.ClientSession() as session: async with session.post(url, json=body) as response: + if response.status == 500: + raise Exception(await response.text()) response.raise_for_status() response_json = await response.json() @@ -240,12 +242,15 @@ async def main( pretty_call = f'{name}({", ".join(f"{k}={v.model_dump_json() if isinstance(v, BaseModel) else json.dumps(v)}" for k, v in args.items())})' print(f'⚙️ {pretty_call}', file=sys.stderr, end=None) sys.stdout.flush() - tool_result = await tool_map[name](**args) - tool_result_str = json.dumps(tool_result) - def describe(res, res_str): + try: + tool_result = await tool_map[name](**args) + except Exception as e: + tool_result = 'ERROR: ' + str(e) + tool_result_str = tool_result if isinstance(tool_result, str) else json.dumps(tool_result) + def describe(res, res_str, max_len = 1000): if isinstance(res, list): return f'{len(res)} items' - return f'{len(res_str)} chars\n {res_str[:1000]}' + return f'{len(res_str)} chars\n {res_str[:1000] if len(res_str) > max_len else res_str}...' print(f' → {describe(tool_result, tool_result_str)}', file=sys.stderr) if verbose: print(tool_result_str, file=sys.stderr) diff --git a/examples/agent/serve_tools_inside_docker.sh b/examples/agent/serve_tools_inside_docker.sh index 5146d3160..aad700f6c 100755 --- a/examples/agent/serve_tools_inside_docker.sh +++ b/examples/agent/serve_tools_inside_docker.sh @@ -7,7 +7,12 @@ # set -euo pipefail +if [[ -z "${BRAVE_SEARCH_API_KEY:-}" ]]; then + echo "Please set BRAVE_SEARCH_API_KEY environment variable in order to enable the brave_search tool" >&2 +fi + PORT=${PORT:-8088} +BRAVE_SEARCH_API_KEY=${BRAVE_SEARCH_API_KEY:-} docker run -p $PORT:$PORT \ -w /src \