refactor: rename bot to agent (#44)

This commit is contained in:
sigoden
2024-06-22 06:52:45 +08:00
committed by GitHub
parent a799428b39
commit adfb7c2b49
12 changed files with 192 additions and 192 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
/tmp /tmp
functions.txt functions.txt
tools.txt tools.txt
bots.txt agents.txt
functions.json functions.json
/bin /bin
/cache /cache
+96 -96
View File
@@ -32,17 +32,17 @@ run@tool() {
"$BIN_DIR/$argc_cmd$ext" "$argc_json" "$BIN_DIR/$argc_cmd$ext" "$argc_json"
} }
# @cmd Run the bot # @cmd Run the agent
# @alias bot:run # @alias agent:run
# @arg cmd![`_choice_bot`] The bot command # @arg cmd![`_choice_agent`] The agent command
# @arg action![`_choice_bot_action`] The bot action # @arg action![`_choice_agent_action`] The agent action
# @arg json The json data # @arg json The json data
run@bot() { run@agent() {
if _is_win; then if _is_win; then
ext=".cmd" ext=".cmd"
fi fi
if [[ -z "$argc_json" ]]; then if [[ -z "$argc_json" ]]; then
functions_path="bots/$argc_cmd/functions.json" functions_path="agents/$argc_cmd/functions.json"
if [[ -f "$functions_path" ]]; then if [[ -f "$functions_path" ]]; then
declaration="$(jq --arg name "$argc_action" '.[] | select(.name == $name)' "$functions_path")" declaration="$(jq --arg name "$argc_action" '.[] | select(.name == $name)' "$functions_path")"
if [[ -n "$declaration" ]]; then if [[ -n "$declaration" ]]; then
@@ -63,10 +63,10 @@ build() {
else else
echo 'Skipped building tools sine tools.txt is missing' echo 'Skipped building tools sine tools.txt is missing'
fi fi
if [[ -f bots.txt ]]; then if [[ -f agents.txt ]]; then
argc build@bot argc build@agent
else else
echo 'Skipped building bots sine bots.txt is missing' echo 'Skipped building agents sine agents.txt is missing'
fi fi
} }
@@ -180,33 +180,33 @@ generate-declarations@tool() {
"$cmd" "scripts/build-declarations.$lang" "tools/$1" "$cmd" "scripts/build-declarations.$lang" "tools/$1"
} }
# @cmd Build bots # @cmd Build agents
# @alias bot:build # @alias agent:build
# @option --names-file=bots.txt Path to a file containing bot filenames, one per line. # @option --names-file=agents.txt Path to a file containing agent filenames, one per line.
# Example: # Example:
# hackernews # hackernews
# spotify # spotify
# @arg bots*[`_choice_bot`] The bot filenames # @arg agents*[`_choice_agent`] The agent filenames
build@bot() { build@agent() {
if [[ "${#argc_bots[@]}" -gt 0 ]]; then if [[ "${#argc_agents[@]}" -gt 0 ]]; then
mkdir -p "$TMP_DIR" mkdir -p "$TMP_DIR"
argc_names_file="$TMP_DIR/bots.txt" argc_names_file="$TMP_DIR/agents.txt"
printf "%s\n" "${argc_bots[@]}" > "$argc_names_file" printf "%s\n" "${argc_agents[@]}" > "$argc_names_file"
else else
argc clean@bot argc clean@agent
fi fi
argc build-declarations@bot --names-file "${argc_names_file}" argc build-declarations@agent --names-file "${argc_names_file}"
argc build-bin@bot --names-file "${argc_names_file}" argc build-bin@agent --names-file "${argc_names_file}"
} }
# @cmd Build bots to bin # @cmd Build agents to bin
# @alias bot:build-bin # @alias agent:build-bin
# @option --names-file=bots.txt Path to a file containing bot dirs, one per line. # @option --names-file=agents.txt Path to a file containing agent dirs, one per line.
# @arg bots*[`_choice_bot`] The bot names # @arg agents*[`_choice_agent`] The agent names
build-bin@bot() { build-bin@agent() {
mkdir -p "$BIN_DIR" mkdir -p "$BIN_DIR"
if [[ "${#argc_bots[@]}" -gt 0 ]]; then if [[ "${#argc_agents[@]}" -gt 0 ]]; then
names=("${argc_bots[@]}" ) names=("${argc_agents[@]}" )
elif [[ -f "$argc_names_file" ]]; then elif [[ -f "$argc_names_file" ]]; then
names=($(cat "$argc_names_file")) names=($(cat "$argc_names_file"))
if [[ "${#names[@]}" -gt 0 ]]; then if [[ "${#names[@]}" -gt 0 ]]; then
@@ -214,88 +214,88 @@ build-bin@bot() {
fi fi
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input bots, not found '$argc_names_file', please create it add some tools." _die "error: not input agents, not found '$argc_names_file', please create it add some tools."
fi fi
not_found_bots=() not_found_agents=()
for name in "${names[@]}"; do for name in "${names[@]}"; do
bot_dir="bots/$name" agent_dir="agents/$name"
found=false found=false
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"
bot_tools_file="$bot_dir/tools.$lang" agent_tools_file="$agent_dir/tools.$lang"
if [[ -f "$bot_tools_file" ]]; then if [[ -f "$agent_tools_file" ]]; then
found=true found=true
if _is_win; then if _is_win; then
bin_file="$BIN_DIR/$name.cmd" bin_file="$BIN_DIR/$name.cmd"
_build_win_shim bot $lang > "$bin_file" _build_win_shim agent $lang > "$bin_file"
else else
bin_file="$BIN_DIR/$name" bin_file="$BIN_DIR/$name"
ln -s -f "$PWD/scripts/run-bot.$lang" "$bin_file" ln -s -f "$PWD/scripts/run-agent.$lang" "$bin_file"
fi fi
echo "Build bot $name" echo "Build agent $name"
fi fi
done done
if [[ "$found" = "false" ]]; then if [[ "$found" = "false" ]]; then
not_found_bots+=("$name") not_found_agents+=("$name")
fi fi
done done
if [[ -n "$not_found_bots" ]]; then if [[ -n "$not_found_agents" ]]; then
_die "error: not found bots: ${not_found_bots[*]}" _die "error: not found agents: ${not_found_agents[*]}"
fi fi
} }
# @cmd Build bots function declarations file # @cmd Build agents function declarations file
# @alias bot:build-declarations # @alias agent:build-declarations
# @option --names-file=bots.txt Path to a file containing bot dirs, one per line. # @option --names-file=agents.txt Path to a file containing agent dirs, one per line.
# @arg bots*[`_choice_bot`] The tool filenames # @arg agents*[`_choice_agent`] The tool filenames
build-declarations@bot() { build-declarations@agent() {
if [[ "${#argc_bots[@]}" -gt 0 ]]; then if [[ "${#argc_agents[@]}" -gt 0 ]]; then
names=("${argc_bots[@]}" ) names=("${argc_agents[@]}" )
elif [[ -f "$argc_names_file" ]]; then elif [[ -f "$argc_names_file" ]]; then
names=($(cat "$argc_names_file")) names=($(cat "$argc_names_file"))
fi fi
if [[ -z "$names" ]]; then if [[ -z "$names" ]]; then
_die "error: not input bots, not found '$argc_names_file', please create it add some tools." _die "error: not input agents, not found '$argc_names_file', please create it add some tools."
fi fi
not_found_bots=() not_found_agents=()
build_failed_bots=() build_failed_agents=()
for name in "${names[@]}"; do for name in "${names[@]}"; do
bot_dir="bots/$name" agent_dir="agents/$name"
build_ok=false build_ok=false
found=false found=false
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"
bot_tools_file="$bot_dir/tools.$lang" agent_tools_file="$agent_dir/tools.$lang"
if [[ -f "$bot_tools_file" ]]; then if [[ -f "$agent_tools_file" ]]; then
found=true found=true
json_data="$(generate-declarations@bot "$name")" || { json_data="$(generate-declarations@agent "$name")" || {
build_failed_bots+=("$name") build_failed_agents+=("$name")
} }
declarations_file="$bot_dir/functions.json" declarations_file="$agent_dir/functions.json"
echo "Build $declarations_file" echo "Build $declarations_file"
echo "$json_data" > "$declarations_file" echo "$json_data" > "$declarations_file"
fi fi
done done
if [[ "$found" == "false" ]]; then if [[ "$found" == "false" ]]; then
not_found_bots+=("$name") not_found_agents+=("$name")
fi fi
done done
if [[ -n "$not_found_bots" ]]; then if [[ -n "$not_found_agents" ]]; then
_die "error: not found bots: ${not_found_bots[*]}" _die "error: not found agents: ${not_found_agents[*]}"
fi fi
if [[ -n "$build_failed_bots" ]]; then if [[ -n "$build_failed_agents" ]]; then
_die "error: invalid bots: ${build_failed_bots[*]}" _die "error: invalid agents: ${build_failed_agents[*]}"
fi fi
} }
# @cmd Generate function declarations for the bot # @cmd Generate function declarations for the agent
# @alias bot:generate-declarations # @alias agent:generate-declarations
# @flag --oneline Summary JSON in one line # @flag --oneline Summary JSON in one line
# @arg bot![`_choice_bot`] The bot name # @arg agent![`_choice_agent`] The agent name
generate-declarations@bot() { generate-declarations@agent() {
tools_path="$(_get_bot_tools_path "$1")" tools_path="$(_get_agent_tools_path "$1")"
if [[ -z "$tools_path" ]]; then if [[ -z "$tools_path" ]]; then
_die "error: no found entry file at bots/$1/tools.<lang>" _die "error: no found entry file at agents/$1/tools.<lang>"
fi fi
lang="${tools_path##*.}" lang="${tools_path##*.}"
cmd="$(_lang_to_cmd "$lang")" cmd="$(_lang_to_cmd "$lang")"
@@ -315,18 +315,18 @@ list@tool() {
_choice_tool _choice_tool
} }
# @cmd List bots which can be put into bots.txt # @cmd List agents which can be put into agents.txt
# @alias bot:list # @alias agent:list
# Examples: # Examples:
# argc list-bots > bots.txt # argc list-agents > agents.txt
list@bot() { list@agent() {
_choice_bot _choice_agent
} }
# @cmd Test the project # @cmd Test the project
test() { test() {
test@tool test@tool
test@bot test@agent
} }
# @cmd Test tools # @cmd Test tools
@@ -395,20 +395,20 @@ test-demo-tools() {
done done
} }
# @cmd Test bots # @cmd Test agents
# @alias bot:test # @alias agent:test
test@bot() { test@agent() {
tmp_dir="cache/tmp" tmp_dir="cache/tmp"
mkdir -p "$tmp_dir" mkdir -p "$tmp_dir"
names_file="$tmp_dir/bots.txt" names_file="$tmp_dir/agents.txt"
argc list@bot > "$names_file" argc list@agent > "$names_file"
argc build@bot --names-file "$names_file" argc build@agent --names-file "$names_file"
test-todo-bots test-todo-agents
} }
# @cmd Test todo-* bots # @cmd Test todo-* agents
# @alias bot:test-todo # @alias agent:test-todo
test-todo-bots() { test-todo-agents() {
if _is_win; then if _is_win; then
ext=".cmd" ext=".cmd"
fi fi
@@ -423,11 +423,11 @@ test-todo-bots() {
cmd="${item#*:}" cmd="${item#*:}"
if command -v "$cmd" &> /dev/null; then if command -v "$cmd" &> /dev/null; then
lang="${item%:*}" lang="${item%:*}"
bot_name="todo-$lang" agent_name="todo-$lang"
rm -rf "cache/$bot_name/todos.json" rm -rf "cache/$agent_name/todos.json"
for test_case in "${test_cases[@]}"; do for test_case in "${test_cases[@]}"; do
IFS='#' read -r action data <<<"${test_case}" IFS='#' read -r action data <<<"${test_case}"
cmd_path="$BIN_DIR/$bot_name$ext" cmd_path="$BIN_DIR/$agent_name$ext"
echo "Test $cmd_path: " echo "Test $cmd_path: "
"$cmd_path" "$action" "$data" "$cmd_path" "$action" "$data"
done done
@@ -443,11 +443,11 @@ clean@tool() {
rm -rf functions.json rm -rf functions.json
} }
# @cmd Clean bots # @cmd Clean agents
# @alias bot:clean # @alias agent:clean
clean@bot() { clean@agent() {
_choice_bot | xargs -I{} rm -rf "$BIN_DIR/{}" _choice_agent | xargs -I{} rm -rf "$BIN_DIR/{}"
_choice_bot | xargs -I{} rm -rf bots/{}/functions.json _choice_agent | xargs -I{} rm -rf agents/{}/functions.json
} }
# @cmd Install this repo to aichat functions_dir # @cmd Install this repo to aichat functions_dir
@@ -500,12 +500,12 @@ _lang_to_cmd() {
done done
} }
_get_bot_tools_path() { _get_agent_tools_path() {
name="$1" name="$1"
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"
entry_file="bots/$name/tools.$lang" entry_file="agents/$name/tools.$lang"
if [[ -f "bots/$name/tools.$lang" ]]; then if [[ -f "agents/$name/tools.$lang" ]]; then
echo "$entry_file" echo "$entry_file"
fi fi
done done
@@ -581,17 +581,17 @@ _choice_tool() {
done done
} }
_choice_bot() { _choice_agent() {
ls -1 bots ls -1 agents
} }
_choice_bot_action() { _choice_agent_action() {
if [[ "$ARGC_COMPGEN" -eq 1 ]]; then if [[ "$ARGC_COMPGEN" -eq 1 ]]; then
expr="s/: /\t/" expr="s/: /\t/"
else else
expr="s/:.*//" expr="s/:.*//"
fi fi
argc generate-declarations@bot "$1" --oneline | sed "$expr" argc generate-declarations@agent "$1" --oneline | sed "$expr"
} }
_choice_cmd() { _choice_cmd() {
+16 -16
View File
@@ -17,7 +17,7 @@ Make sure you have the following tools installed:
git clone https://github.com/sigoden/llm-functions git clone https://github.com/sigoden/llm-functions
``` ```
**2. Build tools and bots:** **2. Build tools and agents:**
- Create a `./tools.txt` file with each tool filename on a new line. - Create a `./tools.txt` file with each tool filename on a new line.
@@ -26,14 +26,14 @@ get_current_weather.sh
may_execute_py_code.py may_execute_py_code.py
``` ```
- Create a `./bots.txt` file with each bot name on a new line. - Create a `./agents.txt` file with each agent name on a new line.
``` ```
todo-sh todo-sh
hackernews hackernews
``` ```
- Run `argc build` to build functions declarations files (`functions.json`) and binaries (`./bin`) for tools and bots. - Run `argc build` to build functions declarations files (`functions.json`) and binaries (`./bin`) for tools and agents.
**3. Configure your AIChat:** **3. Configure your AIChat:**
@@ -63,7 +63,7 @@ Now you can interact with your LLM using natural language prompts that trigger y
![execute-type-showcase](https://github.com/sigoden/llm-functions/assets/4012553/1dbc345f-daf9-4d65-a49f-3df8c7df1727) ![execute-type-showcase](https://github.com/sigoden/llm-functions/assets/4012553/1dbc345f-daf9-4d65-a49f-3df8c7df1727)
![bot-showcase](https://github.com/sigoden/llm-functions/assets/4012553/b4411eeb-d79c-4245-8ec2-dd424ba25621) ![agent-showcase](https://github.com/sigoden/llm-functions/assets/4012553/b4411eeb-d79c-4245-8ec2-dd424ba25621)
## Writing Your Own Tools ## Writing Your Own Tools
@@ -122,32 +122,32 @@ def main(code: str):
``` ```
## Writing Bots ## Writing Agents
Bot = Prompt + Tools (Function Callings) + Knowndge (RAG). It's also known as OpenAI's GPTs. Agent = Prompt + Tools (Function Callings) + Knowndge (RAG). It's also known as OpenAI's GPTs.
The bot has the following folder structure: The agent has the following folder structure:
``` ```
└── bots └── agents
└── mybot └── myagent
├── embeddings/ # Contains RAG files for knownledge ├── embeddings/ # Contains RAG files for knownledge
├── functions.json # Function declarations file (Auto-generated) ├── functions.json # Function declarations file (Auto-generated)
├── index.yaml # Bot definition file ├── index.yaml # Agent definition file
└── tools.{sh,js,py} # Bot tools script └── tools.{sh,js,py} # Agent tools script
``` ```
The bot definition file (`index.yaml`) defines crucial aspects of your bot: The agent definition file (`index.yaml`) defines crucial aspects of your agent:
```yaml ```yaml
name: TestBot name: TestAgent
description: This is test bot description: This is test agent
version: v0.1.0 version: v0.1.0
instructions: You are a test bot to ... instructions: You are a test agent to ...
conversation_starters: conversation_starters:
- What can you do? - What can you do?
``` ```
Refer to `./bots/todo-{sh,js,py}` for examples of how to implement a bot. Refer to `./agents/todo-{sh,js,py}` for examples of how to implement a agent.
## License ## License
@@ -60,7 +60,7 @@ exports.clear_todos = function clearTodos() {
} }
function _getTodosFile() { function _getTodosFile() {
const cacheDir = process.env.LLM_BOT_CACHE_DIR || '/tmp'; const cacheDir = process.env.LLM_AGENT_CACHE_DIR || '/tmp';
if (!fs.existsSync(cacheDir)) { if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true }); fs.mkdirSync(cacheDir, { recursive: true });
} }
@@ -56,7 +56,7 @@ def clear_todos():
def _get_todos_file() -> str: def _get_todos_file() -> str:
cache_dir=os.environ.get("LLM_BOT_CACHE_DIR", "/tmp") cache_dir=os.environ.get("LLM_AGENT_CACHE_DIR", "/tmp")
if not os.path.exists(cache_dir): if not os.path.exists(cache_dir):
os.makedirs(cache_dir, exist_ok=True) os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, "todos.json") return os.path.join(cache_dir, "todos.json")
@@ -1,5 +1,5 @@
name: TodoBot name: Todo
description: Your name is TodoBot and you are a helpful chatbot that manages a todo list. description: A helpful ai agent that manages a todo list.
instructions: | instructions: |
You will be provided with a list of todos. You will be provided with a list of todos.
Users can interact with you using the following commands: Users can interact with you using the following commands:
@@ -60,7 +60,7 @@ _argc_before() {
} }
_get_todos_file() { _get_todos_file() {
echo "${LLM_BOT_CACHE_DIR:-/tmp}/todos.json" echo "${LLM_AGENT_CACHE_DIR:-/tmp}/todos.json"
} }
# See more details at https://github.com/sigoden/argc # See more details at https://github.com/sigoden/argc
+29 -29
View File
@@ -5,36 +5,36 @@ const fs = require("fs");
const os = require("os"); const os = require("os");
async function main() { async function main() {
const [botName, botFunc, rawData] = parseArgv("run-bot.js"); const [agentName, agentFunc, rawData] = parseArgv("run-agent.js");
const botData = parseRawData(rawData); const agentData = parseRawData(rawData);
const rootDir = path.resolve(__dirname, ".."); const rootDir = path.resolve(__dirname, "..");
setupEnv(rootDir, botName); setupEnv(rootDir, agentName);
const botToolsPath = path.resolve(rootDir, `bots/${botName}/tools.js`); const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`);
await run(botToolsPath, botFunc, botData); await run(agentToolsPath, agentFunc, agentData);
} }
function parseArgv(thisFileName) { function parseArgv(thisFileName) {
let botName = process.argv[1]; let agentName = process.argv[1];
let botFunc = ""; let agentFunc = "";
let botData = null; let agentData = null;
if (botName.endsWith(thisFileName)) { if (agentName.endsWith(thisFileName)) {
botName = process.argv[2]; agentName = process.argv[2];
botFunc = process.argv[3]; agentFunc = process.argv[3];
botData = process.argv[4]; agentData = process.argv[4];
} else { } else {
botName = path.basename(botName); agentName = path.basename(agentName);
botFunc = process.argv[2]; agentFunc = process.argv[2];
botData = process.argv[3]; agentData = process.argv[3];
} }
if (botName.endsWith(".js")) { if (agentName.endsWith(".js")) {
botName = botName.slice(0, -3); agentName = agentName.slice(0, -3);
} }
return [botName, botFunc, botData]; return [agentName, agentFunc, agentData];
} }
function parseRawData(data) { function parseRawData(data) {
@@ -48,12 +48,12 @@ function parseRawData(data) {
} }
} }
function setupEnv(rootDir, botName) { function setupEnv(rootDir, agentName) {
process.env["LLM_ROOT_DIR"] = rootDir; process.env["LLM_ROOT_DIR"] = rootDir;
loadEnv(path.resolve(rootDir, ".env")); loadEnv(path.resolve(rootDir, ".env"));
process.env["LLM_BOT_NAME"] = botName; process.env["LLM_AGENT_NAME"] = agentName;
process.env["LLM_BOT_ROOT_DIR"] = path.resolve(rootDir, "bots", botName); process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(rootDir, "agents", agentName);
process.env["LLM_BOT_CACHE_DIR"] = path.resolve(rootDir, "cache", botName); process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(rootDir, "cache", agentName);
} }
function loadEnv(filePath) { function loadEnv(filePath) {
@@ -70,20 +70,20 @@ function loadEnv(filePath) {
} catch {} } catch {}
} }
async function run(botPath, botFunc, botData) { async function run(agentPath, agentFunc, agentData) {
let mod; let mod;
if (os.platform() === "win32") { if (os.platform() === "win32") {
botPath = `file://${botPath}`; agentPath = `file://${agentPath}`;
} }
try { try {
mod = await import(botPath); mod = await import(agentPath);
} catch { } catch {
throw new Error(`Unable to load bot tools at '${botPath}'`); throw new Error(`Unable to load agent tools at '${agentPath}'`);
} }
if (!mod || !mod[botFunc]) { if (!mod || !mod[agentFunc]) {
throw new Error(`Not module function '${botFunc}' at '${botPath}'`); throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
} }
const value = await mod[botFunc](botData); const value = await mod[agentFunc](agentData);
dumpValue(value); dumpValue(value);
} }
+28 -28
View File
@@ -7,14 +7,14 @@ import importlib.util
def main(): def main():
(bot_name, bot_func, raw_data) = parse_argv("run-bot.py") (agent_name, agent_func, raw_data) = parse_argv("run-agent.py")
bot_data = parse_raw_data(raw_data) agent_data = parse_raw_data(raw_data)
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
setup_env(root_dir, bot_name) setup_env(root_dir, agent_name)
bot_tools_path = os.path.join(root_dir, f"bots/{bot_name}/tools.py") agent_tools_path = os.path.join(root_dir, f"agents/{agent_name}/tools.py")
run(bot_tools_path, bot_func, bot_data) run(agent_tools_path, agent_func, agent_data)
def parse_raw_data(data): def parse_raw_data(data):
@@ -30,31 +30,31 @@ def parse_raw_data(data):
def parse_argv(this_file_name): def parse_argv(this_file_name):
argv = sys.argv[:] + [None] * max(0, 4 - len(sys.argv)) argv = sys.argv[:] + [None] * max(0, 4 - len(sys.argv))
bot_name = argv[0] agent_name = argv[0]
bot_func = "" agent_func = ""
bot_data = None agent_data = None
if bot_name.endswith(this_file_name): if agent_name.endswith(this_file_name):
bot_name = sys.argv[1] agent_name = sys.argv[1]
bot_func = sys.argv[2] agent_func = sys.argv[2]
bot_data = sys.argv[3] agent_data = sys.argv[3]
else: else:
bot_name = os.path.basename(bot_name) agent_name = os.path.basename(agent_name)
bot_func = sys.argv[1] agent_func = sys.argv[1]
bot_data = sys.argv[2] agent_data = sys.argv[2]
if bot_name.endswith(".py"): if agent_name.endswith(".py"):
bot_name = bot_name[:-3] agent_name = agent_name[:-3]
return bot_name, bot_func, bot_data return agent_name, agent_func, agent_data
def setup_env(root_dir, bot_name): def setup_env(root_dir, agent_name):
os.environ["LLM_ROOT_DIR"] = root_dir os.environ["LLM_ROOT_DIR"] = root_dir
load_env(os.path.join(root_dir, ".env")) load_env(os.path.join(root_dir, ".env"))
os.environ["LLM_BOT_NAME"] = bot_name os.environ["LLM_AGENT_NAME"] = agent_name
os.environ["LLM_BOT_ROOT_DIR"] = os.path.join(root_dir, "bots", bot_name) os.environ["LLM_AGENT_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name)
os.environ["LLM_BOT_CACHE_DIR"] = os.path.join(root_dir, "cache", bot_name) os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name)
def load_env(file_path): def load_env(file_path):
@@ -71,20 +71,20 @@ def load_env(file_path):
pass pass
def run(bot_path, bot_func, bot_data): def run(agent_path, agent_func, agent_data):
try: try:
spec = importlib.util.spec_from_file_location( spec = importlib.util.spec_from_file_location(
os.path.basename(bot_path), bot_path os.path.basename(agent_path), agent_path
) )
mod = importlib.util.module_from_spec(spec) mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod) spec.loader.exec_module(mod)
except: except:
raise Exception(f"Unable to load bot tools at '{bot_path}'") raise Exception(f"Unable to load agent tools at '{agent_path}'")
if not hasattr(mod, bot_func): if not hasattr(mod, agent_func):
raise Exception(f"Not module function '{bot_func}' at '{bot_path}'") raise Exception(f"Not module function '{agent_func}' at '{agent_path}'")
value = getattr(mod, bot_func)(**bot_data) value = getattr(mod, agent_func)(**agent_data)
dump_value(value) dump_value(value)
+17 -17
View File
@@ -2,26 +2,26 @@
set -e set -e
main() { main() {
this_file_name=run-bot.sh this_file_name=run-agent.sh
parse_argv "$@" parse_argv "$@"
root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)"
setup_env setup_env
bot_tools_path="$root_dir/bots/$bot_name/tools.sh" agent_tools_path="$root_dir/agents/$agent_name/tools.sh"
run run
} }
parse_argv() { parse_argv() {
if [[ "$0" == *"$this_file_name" ]]; then if [[ "$0" == *"$this_file_name" ]]; then
bot_name="$1" agent_name="$1"
bot_func="$2" agent_func="$2"
bot_data="$3" agent_data="$3"
else else
bot_name="$(basename "$0")" agent_name="$(basename "$0")"
bot_func="$1" agent_func="$1"
bot_data="$2" agent_data="$2"
fi fi
if [[ "$bot_name" == *.sh ]]; then if [[ "$agent_name" == *.sh ]]; then
bot_name="${bot_name:0:$((${#bot_name}-3))}" agent_name="${agent_name:0:$((${#agent_name}-3))}"
fi fi
} }
@@ -30,24 +30,24 @@ setup_env() {
if [[ -f "$LLM_ROOT_DIR/.env" ]]; then if [[ -f "$LLM_ROOT_DIR/.env" ]]; then
source "$LLM_ROOT_DIR/.env" source "$LLM_ROOT_DIR/.env"
fi fi
export LLM_BOT_NAME="$bot_name" export LLM_AGENT_NAME="$agent_name"
export LLM_BOT_ROOT_DIR="$LLM_ROOT_DIR/bots/$bot_name" export LLM_AGENT_ROOT_DIR="$LLM_ROOT_DIR/agents/$agent_name"
export LLM_BOT_CACHE_DIR="$LLM_ROOT_DIR/cache/$bot_name" export LLM_AGENT_CACHE_DIR="$LLM_ROOT_DIR/cache/$agent_name"
} }
run() { run() {
if [[ -z "$bot_data" ]]; then if [[ -z "$agent_data" ]]; then
die "No JSON data" die "No JSON data"
fi fi
_jq=jq _jq=jq
if [[ "$OS" == "Windows_NT" ]]; then if [[ "$OS" == "Windows_NT" ]]; then
_jq="jq -b" _jq="jq -b"
bot_tools_path="$(cygpath -w "$bot_tools_path")" agent_tools_path="$(cygpath -w "$agent_tools_path")"
fi fi
data="$( data="$(
echo "$bot_data" | \ echo "$agent_data" | \
$_jq -r ' $_jq -r '
to_entries | .[] | to_entries | .[] |
(.key | split("_") | join("-")) as $key | (.key | split("_") | join("-")) as $key |
@@ -68,7 +68,7 @@ run() {
args+=("$(echo "$line" | $_jq -r '.')") args+=("$(echo "$line" | $_jq -r '.')")
fi fi
done <<< "$data" done <<< "$data"
"$bot_tools_path" "$bot_func" "${args[@]}" "$agent_tools_path" "$agent_func" "${args[@]}"
} }
die() { die() {