agent
: tool tweaks (remove ansi escapes from python output, update env keys + provider docs)
This commit is contained in:
parent
366efc8a18
commit
21a3c90a1c
6 changed files with 32 additions and 15 deletions
|
@ -39,6 +39,7 @@
|
||||||
- Run the tools in [examples/agent/tools](./examples/agent/tools) inside a docker container (check http://localhost:8088/docs once running):
|
- Run the tools in [examples/agent/tools](./examples/agent/tools) inside a docker container (check http://localhost:8088/docs once running):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
export BRAVE_SEARCH_API_KEY=... # https://api.search.brave.com/
|
||||||
# Shorthand: ./examples/agent/serve_tools_inside_docker.sh
|
# Shorthand: ./examples/agent/serve_tools_inside_docker.sh
|
||||||
docker run -p 8088:8088 -w /src -v $PWD/examples/agent:/src \
|
docker run -p 8088:8088 -w /src -v $PWD/examples/agent:/src \
|
||||||
--env BRAVE_SEARCH_API_KEY=$BRAVE_SEARCH_API_KEY \
|
--env BRAVE_SEARCH_API_KEY=$BRAVE_SEARCH_API_KEY \
|
||||||
|
@ -103,9 +104,10 @@
|
||||||
- To compare the above results w/ a cloud provider's tool usage behaviour, just set the `--provider` flag (accepts `openai`, `together`, `groq`) and/or use `--endpoint`, `--api-key`, and `--model`
|
- To compare the above results w/ a cloud provider's tool usage behaviour, just set the `--provider` flag (accepts `openai`, `together`, `groq`) and/or use `--endpoint`, `--api-key`, and `--model`
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export OPENAI_API_KEY=... # for --provider=openai
|
export LLAMA_API_KEY=... # for --provider=llama.cpp https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md
|
||||||
# export TOGETHER_API_KEY=... # for --provider=together
|
export OPENAI_API_KEY=... # for --provider=openai https://platform.openai.com/api-keys
|
||||||
# export GROQ_API_KEY=... # for --provider=groq
|
export TOGETHER_API_KEY=... # for --provider=together https://api.together.ai/settings/api-keys
|
||||||
|
export GROQ_API_KEY=... # for --provider=groq https://console.groq.com/keys
|
||||||
uv run examples/agent/run.py --tools http://localhost:8088 \
|
uv run examples/agent/run.py --tools http://localhost:8088 \
|
||||||
"Search for, fetch and summarize the homepage of llama.cpp" \
|
"Search for, fetch and summarize the homepage of llama.cpp" \
|
||||||
--provider=openai
|
--provider=openai
|
||||||
|
|
|
@ -146,22 +146,22 @@ def typer_async_workaround():
|
||||||
_PROVIDERS = {
|
_PROVIDERS = {
|
||||||
'llama.cpp': {
|
'llama.cpp': {
|
||||||
'endpoint': 'http://localhost:8080/v1/',
|
'endpoint': 'http://localhost:8080/v1/',
|
||||||
'api_key_env': 'LLAMACPP_API_KEY',
|
'api_key_env': 'LLAMA_API_KEY', # https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md
|
||||||
},
|
},
|
||||||
'openai': {
|
'openai': {
|
||||||
'endpoint': 'https://api.openai.com/v1/',
|
'endpoint': 'https://api.openai.com/v1/',
|
||||||
'default_model': 'gpt-4o',
|
'default_model': 'gpt-4o',
|
||||||
'api_key_env': 'OPENAI_API_KEY',
|
'api_key_env': 'OPENAI_API_KEY', # https://platform.openai.com/api-keys
|
||||||
},
|
},
|
||||||
'together': {
|
'together': {
|
||||||
'endpoint': 'https://api.together.xyz',
|
'endpoint': 'https://api.together.xyz',
|
||||||
'default_model': 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo',
|
'default_model': 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo',
|
||||||
'api_key_env': 'TOGETHER_API_KEY',
|
'api_key_env': 'TOGETHER_API_KEY', # https://api.together.ai/settings/api-keys
|
||||||
},
|
},
|
||||||
'groq': {
|
'groq': {
|
||||||
'endpoint': 'https://api.groq.com/openai',
|
'endpoint': 'https://api.groq.com/openai',
|
||||||
'default_model': 'llama-3.1-70b-versatile',
|
'default_model': 'llama-3.1-70b-versatile',
|
||||||
'api_key_env': 'GROQ_API_KEY',
|
'api_key_env': 'GROQ_API_KEY', # https://console.groq.com/keys
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +245,7 @@ async def main(
|
||||||
def describe(res, res_str):
|
def describe(res, res_str):
|
||||||
if isinstance(res, list):
|
if isinstance(res, list):
|
||||||
return f'{len(res)} items'
|
return f'{len(res)} items'
|
||||||
return f'{len(res_str)} chars'
|
return f'{len(res_str)} chars\n {res_str[:1000]}'
|
||||||
print(f' → {describe(tool_result, tool_result_str)}', file=sys.stderr)
|
print(f' → {describe(tool_result, tool_result_str)}', file=sys.stderr)
|
||||||
if verbose:
|
if verbose:
|
||||||
print(tool_result_str, file=sys.stderr)
|
print(tool_result_str, file=sys.stderr)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
# requires-python = ">=3.11"
|
# requires-python = ">=3.11"
|
||||||
# dependencies = [
|
# dependencies = [
|
||||||
# "aiohttp",
|
# "aiohttp",
|
||||||
|
# "beautifulsoup4",
|
||||||
# "fastapi",
|
# "fastapi",
|
||||||
# "html2text",
|
# "html2text",
|
||||||
# "ipython",
|
# "ipython",
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# Serves tools inside a docker container
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# examples/agent/serve_tools_inside_docker.sh [--verbose] [--include="tool1|tool2|..."] [--exclude="tool1|tool2|..."]
|
||||||
|
#
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
PORT=${PORT:-8088}
|
PORT=${PORT:-8088}
|
||||||
|
@ -8,4 +14,4 @@ docker run -p $PORT:$PORT \
|
||||||
-v $PWD/examples/agent:/src \
|
-v $PWD/examples/agent:/src \
|
||||||
--env BRAVE_SEARCH_API_KEY=$BRAVE_SEARCH_API_KEY \
|
--env BRAVE_SEARCH_API_KEY=$BRAVE_SEARCH_API_KEY \
|
||||||
--rm -it ghcr.io/astral-sh/uv:python3.12-alpine \
|
--rm -it ghcr.io/astral-sh/uv:python3.12-alpine \
|
||||||
uv run serve_tools.py --port $PORT
|
uv run serve_tools.py --port $PORT "$@"
|
||||||
|
|
|
@ -3,9 +3,9 @@ import html2text
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
async def fetch_page(url: str) -> str:
|
async def fetch_page(url: str):
|
||||||
'''
|
'''
|
||||||
Fetch a web page (convert it to markdown if possible).
|
Fetch a web page (convert it to markdown if possible), using aiohttp.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
from IPython.core.interactiveshell import InteractiveShell
|
from IPython.core.interactiveshell import InteractiveShell
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
import logging
|
import logging
|
||||||
|
@ -7,6 +8,11 @@ import sys
|
||||||
python_tools = {}
|
python_tools = {}
|
||||||
|
|
||||||
|
|
||||||
|
def _strip_ansi_codes(text):
|
||||||
|
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||||
|
return ansi_escape.sub('', text)
|
||||||
|
|
||||||
|
|
||||||
def python(code: str) -> str:
|
def python(code: str) -> str:
|
||||||
'''
|
'''
|
||||||
Execute Python code in a siloed environment using IPython and returns the output.
|
Execute Python code in a siloed environment using IPython and returns the output.
|
||||||
|
@ -18,7 +24,9 @@ def python(code: str) -> str:
|
||||||
str: The output of the executed code.
|
str: The output of the executed code.
|
||||||
'''
|
'''
|
||||||
logging.debug('[python] Executing %s', code)
|
logging.debug('[python] Executing %s', code)
|
||||||
shell = InteractiveShell()
|
shell = InteractiveShell(
|
||||||
|
colors='neutral',
|
||||||
|
)
|
||||||
shell.user_global_ns.update(python_tools)
|
shell.user_global_ns.update(python_tools)
|
||||||
|
|
||||||
old_stdout = sys.stdout
|
old_stdout = sys.stdout
|
||||||
|
@ -27,9 +35,9 @@ def python(code: str) -> str:
|
||||||
try:
|
try:
|
||||||
shell.run_cell(code)
|
shell.run_cell(code)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.debug('[python] Execution failed: %s\nCode: %s', e, code)
|
# logging.debug('[python] Execution failed: %s\nCode: %s', e, code)
|
||||||
return f'An error occurred: {e}'
|
return f'An error occurred:\n{_strip_ansi_codes(str(e))}'
|
||||||
finally:
|
finally:
|
||||||
sys.stdout = old_stdout
|
sys.stdout = old_stdout
|
||||||
|
|
||||||
return out.getvalue()
|
return _strip_ansi_codes(out.getvalue())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue