refactor: demo tools/agents (#67)

This commit is contained in:
sigoden
2024-07-09 21:17:12 +08:00
committed by GitHub
parent 0dbdc9e710
commit 02e335c995
11 changed files with 116 additions and 32 deletions
+20 -31
View File
@@ -289,7 +289,7 @@ build-declarations@agent() {
fi fi
if [[ "$ok" == "true" ]]; then if [[ "$ok" == "true" ]]; then
if [[ -n "$agent_json_data" ]] && [[ -n "$tools_json_data" ]]; then if [[ -n "$agent_json_data" ]] && [[ -n "$tools_json_data" ]]; then
json_data="$(jq -s '.[0] + .[1]' <(echo "$agent_json_data") <(echo "$tools_json_data"))" json_data="$(echo "[$agent_json_data,$tools_json_data]" | jq 'flatten')"
elif [[ -n "$agent_json_data" ]]; then elif [[ -n "$agent_json_data" ]]; then
json_data="$agent_json_data" json_data="$agent_json_data"
elif [[ -n "$tools_json_data" ]]; then elif [[ -n "$tools_json_data" ]]; then
@@ -397,9 +397,10 @@ test-execute-code-tools() {
test-demo-tools() { test-demo-tools() {
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"
echo "---- Test demo_tool.$lang ---" tool="demo_$lang.$lang"
argc build-bin@tool "demo_tool.$lang" echo "---- Test $tool ---"
argc run@tool demo_tool '{ argc build-bin@tool "$tool"
argc run@tool $tool '{
"boolean": true, "boolean": true,
"string": "Hello", "string": "Hello",
"string_enum": "foo", "string_enum": "foo",
@@ -428,37 +429,24 @@ test@agent() {
names_file="$tmp_dir/agents.txt" names_file="$tmp_dir/agents.txt"
argc list@agent > "$names_file" argc list@agent > "$names_file"
argc build@agent --names-file "$names_file" argc build@agent --names-file "$names_file"
test-todo-agents test-demo-agents
} }
# @cmd Test todo-* agents # @cmd Test demo agents
# @alias agent:test-todo # @alias agent:test-demo
test-todo-agents() { test-demo-agents() {
if _is_win; then echo "Test demo agent:"
ext=".cmd" argc run@agent demo get_sysinfo '{}'
fi
test_cases=( \
'add_todo#{"desc":"Add a todo item"}' \
'add_todo#{"desc":"Add another todo item"}' \
'del_todo#{"id":1}' \
'list_todos#{}' \
'clear_todos#{}' \
)
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
cmd="${item#*:}" cmd="${item#*:}"
if command -v "$cmd" &> /dev/null; then lang="${item%:*}"
lang="${item%:*}" echo "Test agents/demo/tools.$lang:"
agent_name="todo-$lang" if [[ "$cmd" == "sh" ]]; then
rm -rf "cache/$agent_name/todos.json" "$(argc --argc-shell-path)" ./scripts/run-agent.sh demo get_sysinfo '{}'
for test_case in "${test_cases[@]}"; do elif command -v "$cmd" &> /dev/null; then
IFS='#' read -r action data <<<"${test_case}" $cmd ./scripts/run-agent.$lang demo get_sysinfo '{}'
cmd_path="$BIN_DIR/$agent_name$ext"
echo "Test $cmd_path: "
"$cmd_path" "$action" "$data"
done
fi fi
done done
} }
# @cmd Clean tools # @cmd Clean tools
@@ -475,7 +463,7 @@ clean@agent() {
_choice_agent | xargs -I{} rm -rf agents/{}/functions.json _choice_agent | xargs -I{} rm -rf agents/{}/functions.json
} }
# @cmd Symlink web_search tool # @cmd Link a tool as web_search tool
# #
# Example: # Example:
# argc link-web-search search_bing.sh # argc link-web-search search_bing.sh
@@ -484,7 +472,7 @@ link-web-search() {
_link_tool $1 web_search _link_tool $1 web_search
} }
# @cmd Symlink code_interpreter tool # @cmd Link a tool as code_interpreter tool
# #
# Example: # Example:
# argc link-code-interpreter execute_py_code.py # argc link-code-interpreter execute_py_code.py
@@ -550,6 +538,7 @@ _get_agent_tools_path() {
entry_file="agents/$name/tools.$lang" entry_file="agents/$name/tools.$lang"
if [[ -f "agents/$name/tools.$lang" ]]; then if [[ -f "agents/$name/tools.$lang" ]]; then
echo "$entry_file" echo "$entry_file"
break
fi fi
done done
} }
+1 -1
View File
@@ -134,7 +134,7 @@ The agent definition file (`index.yaml`) defines crucial aspects of your agent:
```yaml ```yaml
name: TestAgent name: TestAgent
description: This is test agent description: This is test agent
version: v0.1.0 version: 0.1.0
instructions: You are a test ai agent to ... instructions: You are a test ai agent to ...
conversation_starters: conversation_starters:
- What can you do? - What can you do?
+44
View File
@@ -0,0 +1,44 @@
# Demo
This is demo agent.
## tools.{sh,js,py}
You only need one of the `tools.sh`, `tools.js`, or `tools.py` files. All three are provided so that everyone can understand how to implement the tools in each language.
## tools.txt
The `tools.txt` is used to reuse the tools in the `tools/` directory.
## index.yaml
This document is essential as it defines the agent.
### variables
Variables are generally used to record a certain behavior or preference of a user.
```yaml
variables:
- name: foo
description: This is a foo
- name: bar
description: This is a bar with default value
default: val
```
Variables can be used in the `instructions`.
```yaml
instructions: |
The instructions can inline {{foo}} and {{bar}} variables.
```
### documents
Documents are used for RAG.
```yaml
documents:
- https://raw.githubusercontent.com/sigoden/llm-functions/main/README.md
```
+17
View File
@@ -0,0 +1,17 @@
name: Demo
description: This is demo agent.
version: 0.1.0
instructions: |
You are a AI agent designed to demonstrate agent capabilities.
Use prefer language {{lang}} to answer the question, regardless of the input language.
When the user asks for the system info, retrieve it by running the get_sysinfo tool.
When the user asks for the current time, retrieve it by using the get_current_time tool.
variables:
- name: lang
description: Your prefer language
conversation_starters:
- What is the prefer language
- Show me the system info
- Tell me the current time
documents:
- https://raw.githubusercontent.com/sigoden/llm-functions/main/README.md
+9
View File
@@ -0,0 +1,9 @@
const os = require("node:os");
/**
* Get the system info
*/
exports.get_sysinfo = function getSysinfo() {
return `OS: ${os.type()}
Arch: ${os.arch()}
User: ${process.env["USER"]}`
}
+12
View File
@@ -0,0 +1,12 @@
import os
import platform
def get_sysinfo():
"""
Get the system info
"""
return "\n".join([
f"OS: {platform.system()}",
f"Arch: {platform.machine()}",
f"User: {os.environ.get('USER')}"
])
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -e
# @cmd Get the system info
get_sysinfo() {
echo "OS: $(uname)"
echo "Arch: $(arch)"
echo "User: $USER"
}
# See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")"
+1
View File
@@ -0,0 +1 @@
get_current_time.sh