diff options
| author | sigoden <sigoden@gmail.com> | 2024-10-18 18:28:03 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-18 18:28:03 +0800 |
| commit | 6eb391bdc76ed070299f1b70e96f021a03a6d97a (patch) | |
| tree | c28eba4cac9b9da1773ad35524fa02c2dd3bca25 | |
| parent | b5377bf28bb4d7ec114b7ab9ef1ecec3995cfe66 (diff) | |
| download | llm-functions-docker-6eb391bdc76ed070299f1b70e96f021a03a6d97a.tar.gz | |
feat: controls displaying the results from function call (#111)
| -rwxr-xr-x | scripts/run-agent.js | 65 | ||||
| -rwxr-xr-x | scripts/run-agent.py | 47 | ||||
| -rwxr-xr-x | scripts/run-agent.sh | 32 | ||||
| -rwxr-xr-x | scripts/run-tool.js | 51 | ||||
| -rwxr-xr-x | scripts/run-tool.py | 32 | ||||
| -rwxr-xr-x | scripts/run-tool.sh | 28 |
6 files changed, 235 insertions, 20 deletions
diff --git a/scripts/run-agent.js b/scripts/run-agent.js index 9aba908..3b176fe 100755 --- a/scripts/run-agent.js +++ b/scripts/run-agent.js @@ -1,7 +1,8 @@ #!/usr/bin/env node const path = require("path"); -const fs = require("fs"); +const { createWriteStream } = require("fs"); +const { readFile } = require("fs/promises"); const os = require("os"); async function main() { @@ -9,7 +10,7 @@ async function main() { const agentData = parseRawData(rawData); const rootDir = path.resolve(__dirname, ".."); - setupEnv(rootDir, agentName); + await setupEnv(rootDir, agentName, agentFunc); const agentToolsPath = path.resolve(rootDir, `agents/${agentName}/tools.js`); await run(agentToolsPath, agentFunc, agentData); @@ -48,10 +49,11 @@ function parseRawData(data) { } } -function setupEnv(rootDir, agentName) { - loadEnv(path.resolve(rootDir, ".env")); +async function setupEnv(rootDir, agentName, agentFunc) { + await loadEnv(path.resolve(rootDir, ".env")); process.env["LLM_ROOT_DIR"] = rootDir; process.env["LLM_AGENT_NAME"] = agentName; + process.env["LLM_AGENT_FUNC"] = agentFunc; process.env["LLM_AGENT_ROOT_DIR"] = path.resolve( rootDir, "agents", @@ -64,9 +66,9 @@ function setupEnv(rootDir, agentName) { ); } -function loadEnv(filePath) { +async function loadEnv(filePath) { try { - const data = fs.readFileSync(filePath, "utf-8"); + const data = await readFile(filePath, "utf-8"); const lines = data.split("\n"); lines.forEach((line) => { @@ -75,7 +77,7 @@ function loadEnv(filePath) { const [key, ...value] = line.split("="); process.env[key.trim()] = value.join("=").trim(); }); - } catch {} + } catch { } } async function run(agentPath, agentFunc, agentData) { @@ -93,6 +95,7 @@ async function run(agentPath, agentFunc, agentData) { } const value = await mod[agentFunc](agentData); returnToLLM(value); + await dumpResult(); } function returnToLLM(value) { @@ -101,7 +104,7 @@ function returnToLLM(value) { } let writer = process.stdout; if (process.env["LLM_OUTPUT"]) { - writer = fs.createWriteStream(process.env["LLM_OUTPUT"]); + writer = createWriteStream(process.env["LLM_OUTPUT"]); } const type = typeof value; if (type === "string" || type === "number" || type === "boolean") { @@ -116,4 +119,48 @@ function returnToLLM(value) { } } -main(); +async function dumpResult() { + if (!process.stdout.isTTY) { + return; + } + if (!process.env["LLM_OUTPUT"]) { + return; + } + let showResult = false; + const agentName = process.env["LLM_AGENT_NAME"].toUpperCase().replace(/-/g, '_'); + const agentEnvName = `LLM_AGENT_DUMP_RESULT_${agentName}`; + const agentEnvValue = process.env[agentEnvName]; + + const funcName = process.env["LLM_AGENT_FUNC"].toUpperCase().replace(/-/g, '_'); + const funcEnvName = `${agentEnvName}_${funcName}`; + const funcEnvValue = process.env[funcEnvName]; + if (agentEnvValue === '1' || agentEnvValue === 'true') { + if (funcEnvValue !== '0' && funcEnvValue !== 'false') { + showResult = true; + } + } else { + if (funcEnvValue === '1' || funcEnvValue === 'true') { + showResult = true; + } + } + if (!showResult) { + return; + } + + let data = ""; + try { + data = await readFile(process.env["LLM_OUTPUT"], "utf-8"); + } catch { + return; + } + process.stdout.write(`\x1b[2m----------------------\n${data}\n----------------------\x1b[0m\n`); +} + +(async () => { + try { + await main(); + } catch (err) { + console.error(err?.message || err); + process.exit(1); + } +})(); diff --git a/scripts/run-agent.py b/scripts/run-agent.py index a3f3ec5..582b792 100755 --- a/scripts/run-agent.py +++ b/scripts/run-agent.py @@ -11,7 +11,7 @@ def main(): agent_data = parse_raw_data(raw_data) root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) - setup_env(root_dir, agent_name) + setup_env(root_dir, agent_name, agent_func) agent_tools_path = os.path.join(root_dir, f"agents/{agent_name}/tools.py") run(agent_tools_path, agent_func, agent_data) @@ -49,10 +49,11 @@ def parse_argv(this_file_name): return agent_name, agent_func, agent_data -def setup_env(root_dir, agent_name): +def setup_env(root_dir, agent_name, agent_func): load_env(os.path.join(root_dir, ".env")) os.environ["LLM_ROOT_DIR"] = root_dir os.environ["LLM_AGENT_NAME"] = agent_name + os.environ["LLM_AGENT_FUNC"] = agent_func os.environ["LLM_AGENT_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name) os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name) @@ -86,6 +87,42 @@ def run(agent_path, agent_func, agent_data): value = getattr(mod, agent_func)(**agent_data) return_to_llm(value) + dump_result() + + +def dump_result(): + if not os.isatty(1): + return + + if not os.getenv("LLM_OUTPUT"): + return + + show_result = False + agent_name = os.environ["LLM_AGENT_NAME"].upper().replace("-", "_") + agent_env_name = f"LLM_AGENT_DUMP_RESULT_{agent_name}" + agent_env_value = os.getenv(agent_env_name) + + func_name = os.environ["LLM_AGENT_FUNC"].upper().replace("-", "_") + func_env_name = f"{agent_env_name}_{func_name}" + func_env_value = os.getenv(func_env_name) + + if agent_env_value in ("1", "true"): + if func_env_value not in ("0", "false"): + show_result = True + else: + if func_env_value in ("1", "true"): + show_result = True + + if not show_result: + return + + try: + with open(os.environ["LLM_OUTPUT"], "r", encoding="utf-8") as f: + data = f.read() + except: + return + + print(f"\x1b[2m----------------------\n{data}\n----------------------\x1b[0m") def return_to_llm(value): @@ -107,4 +144,8 @@ def return_to_llm(value): if __name__ == "__main__": - main() + try: + main() + except Exception as e: + print(e, file=sys.stderr) + sys.exit(1) diff --git a/scripts/run-agent.sh b/scripts/run-agent.sh index f37b43f..9ad0b49 100755 --- a/scripts/run-agent.sh +++ b/scripts/run-agent.sh @@ -29,6 +29,7 @@ setup_env() { load_env "$root_dir/.env" export LLM_ROOT_DIR="$root_dir" export LLM_AGENT_NAME="$agent_name" + export LLM_AGENT_FUNC="$agent_func" export LLM_AGENT_ROOT_DIR="$LLM_ROOT_DIR/agents/$agent_name" export LLM_AGENT_CACHE_DIR="$LLM_ROOT_DIR/cache/$agent_name" } @@ -91,7 +92,38 @@ EOF eval "'$tools_path' '$agent_func' $args" if [[ "$no_llm_output" -eq 1 ]]; then cat "$LLM_OUTPUT" + else + dump_result + fi +} + +dump_result() { + if [ ! -t 1 ]; then + return; + fi + + local agent_env_name agent_env_value func_env_name func_env_value show_result=0 + agent_env_name="LLM_AGENT_DUMP_RESULT_$(echo "$LLM_AGENT_NAME" | tr '[:lower:]' '[:upper:]' | tr '-' '_')" + agent_env_value="${!agent_env_name}" + func_env_name="${agent_env_name}_$(echo "$LLM_AGENT_FUNC" | tr '[:lower:]' '[:upper:]' | tr '-' '_')" + func_env_value="${!func_env_name}" + if [[ "$agent_env_value" == "1" || "$agent_env_value" == "true" ]]; then + if [[ "$func_env_value" != "0" && "$func_env_value" != "false" ]]; then + show_result=1 + fi + else + if [[ "$func_env_value" == "1" || "$func_env_value" == "true" ]]; then + show_result=1 + fi fi + if [[ "$show_result" -ne 1 ]]; then + return + fi + cat <<EOF +$(echo -e "\e[2m")---------------------- +$(cat "$LLM_OUTPUT") +----------------------$(echo -e "\e[0m") +EOF } die() { diff --git a/scripts/run-tool.js b/scripts/run-tool.js index 18770fd..9c86482 100755 --- a/scripts/run-tool.js +++ b/scripts/run-tool.js @@ -1,7 +1,8 @@ #!/usr/bin/env node const path = require("path"); -const fs = require("fs"); +const { createWriteStream } = require("fs"); +const { readFile } = require("fs/promises"); const os = require("os"); async function main() { @@ -9,7 +10,7 @@ async function main() { const toolData = parseRawData(rawData); const rootDir = path.resolve(__dirname, ".."); - setupEnv(rootDir, toolName); + await setupEnv(rootDir, toolName); const toolPath = path.resolve(rootDir, `tools/${toolName}.js`); await run(toolPath, "run", toolData); @@ -45,16 +46,16 @@ function parseRawData(data) { } } -function setupEnv(rootDir, toolName) { - loadEnv(path.resolve(rootDir, ".env")); +async function setupEnv(rootDir, toolName) { + await loadEnv(path.resolve(rootDir, ".env")); process.env["LLM_ROOT_DIR"] = rootDir; process.env["LLM_TOOL_NAME"] = toolName; process.env["LLM_TOOL_CACHE_DIR"] = path.resolve(rootDir, "cache", toolName); } -function loadEnv(filePath) { +async function loadEnv(filePath) { try { - const data = fs.readFileSync(filePath, "utf-8"); + const data = await readFile(filePath, "utf-8"); const lines = data.split("\n"); lines.forEach((line) => { @@ -63,7 +64,7 @@ function loadEnv(filePath) { const [key, ...value] = line.split("="); process.env[key.trim()] = value.join("=").trim(); }); - } catch {} + } catch { } } async function run(toolPath, toolFunc, toolData) { @@ -81,6 +82,7 @@ async function run(toolPath, toolFunc, toolData) { } const value = await mod[toolFunc](toolData); returnToLLM(value); + await dumpResult(); } function returnToLLM(value) { @@ -89,7 +91,7 @@ function returnToLLM(value) { } let writer = process.stdout; if (process.env["LLM_OUTPUT"]) { - writer = fs.createWriteStream(process.env["LLM_OUTPUT"]); + writer = createWriteStream(process.env["LLM_OUTPUT"]); } const type = typeof value; if (type === "string" || type === "number" || type === "boolean") { @@ -104,6 +106,39 @@ function returnToLLM(value) { } } +async function dumpResult() { + if (!process.stdout.isTTY) { + return; + } + if (!process.env["LLM_OUTPUT"]) { + return; + } + let showResult = false; + const toolName = process.env["LLM_TOOL_NAME"].toUpperCase().replace(/-/g, '_'); + const envName = `LLM_TOOL_DUMP_RESULT_${toolName}`; + const envValue = process.env[envName]; + if (process.env.LLM_TOOL_DUMP_RESULT === '1' || process.env.LLM_TOOL_DUMP_RESULT === 'true') { + if (envValue !== '0' && envValue !== 'false') { + showResult = true; + } + } else { + if (envValue === '1' || envValue === 'true') { + showResult = true; + } + } + if (!showResult) { + return; + } + + let data = ""; + try { + data = await readFile(process.env["LLM_OUTPUT"], "utf-8"); + } catch { + return; + } + process.stdout.write(`\x1b[2m----------------------\n${data}\n----------------------\x1b[0m\n`); +} + (async () => { try { await main(); diff --git a/scripts/run-tool.py b/scripts/run-tool.py index b5cdc79..4b6af78 100755 --- a/scripts/run-tool.py +++ b/scripts/run-tool.py @@ -82,6 +82,7 @@ def run(tool_path, tool_func, tool_data): value = getattr(mod, tool_func)(**tool_data) return_to_llm(value) + dump_result() def return_to_llm(value): @@ -102,6 +103,37 @@ def return_to_llm(value): writer.write(value_str) +def dump_result(): + if not os.isatty(1): + return + + if not os.getenv("LLM_OUTPUT"): + return + + show_result = False + tool_name = os.environ["LLM_TOOL_NAME"].upper().replace("-", "_") + env_name = f"LLM_TOOL_DUMP_RESULT_{tool_name}" + env_value = os.getenv(env_name) + + if os.getenv("LLM_TOOL_DUMP_RESULT") in ("1", "true"): + if env_value not in ("0", "false"): + show_result = True + else: + if env_value in ("1", "true"): + show_result = True + + if not show_result: + return + + try: + with open(os.environ["LLM_OUTPUT"], "r", encoding="utf-8") as f: + data = f.read() + except: + return + + print(f"\x1b[2m----------------------\n{data}\n----------------------\x1b[0m") + + if __name__ == "__main__": try: main() diff --git a/scripts/run-tool.sh b/scripts/run-tool.sh index bdc5547..4ede4a1 100755 --- a/scripts/run-tool.sh +++ b/scripts/run-tool.sh @@ -88,7 +88,35 @@ EOF eval "'$tool_path' $args" if [[ "$no_llm_output" -eq 1 ]]; then cat "$LLM_OUTPUT" + else + dump_result + fi +} + +dump_result() { + if [ ! -t 1 ]; then + return; + fi + local env_name env_value show_result=0 + env_name="LLM_TOOL_DUMP_RESULT_$(echo "$LLM_TOOL_NAME" | tr '[:lower:]' '[:upper:]' | tr '-' '_')" + env_value="${!env_name}" + if [[ "$LLM_TOOL_DUMP_RESULT" == "1" || "$LLM_TOOL_DUMP_RESULT" == "true" ]]; then + if [[ "$env_value" != "0" && "$env_value" != "false" ]]; then + show_result=1 + fi + else + if [[ "$env_value" == "1" || "$env_value" == "true" ]]; then + show_result=1 + fi fi + if [[ "$show_result" -ne 1 ]]; then + return + fi + cat <<EOF +$(echo -e "\e[2m")---------------------- +$(cat "$LLM_OUTPUT") +----------------------$(echo -e "\e[0m") +EOF } die() { |
