aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-07-10 18:53:32 +0800
committerGitHub <noreply@github.com>2024-07-10 18:53:32 +0800
commit732eae532c8e8632db95ab80e0dde5071e744386 (patch)
tree94e9f6d15b64cff5a26d6bbf44f16c53deb8c324 /scripts
parent01e07c0cc0be0b1600d688616d12ad0afa9edc71 (diff)
downloadllm-functions-docker-732eae532c8e8632db95ab80e0dde5071e744386.tar.gz
feat: adjust the way of returning data to LLM (#69)
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/run-agent.js26
-rwxr-xr-xscripts/run-agent.py15
-rwxr-xr-xscripts/run-agent.sh50
-rwxr-xr-xscripts/run-tool.js14
-rwxr-xr-xscripts/run-tool.py15
-rwxr-xr-xscripts/run-tool.sh44
6 files changed, 117 insertions, 47 deletions
diff --git a/scripts/run-agent.js b/scripts/run-agent.js
index 69dd119..36fc66b 100755
--- a/scripts/run-agent.js
+++ b/scripts/run-agent.js
@@ -49,11 +49,19 @@ function parseRawData(data) {
}
function setupEnv(rootDir, agentName) {
- process.env["LLM_ROOT_DIR"] = rootDir;
loadEnv(path.resolve(rootDir, ".env"));
+ process.env["LLM_ROOT_DIR"] = rootDir;
process.env["LLM_AGENT_NAME"] = agentName;
- process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(rootDir, "agents", agentName);
- process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(rootDir, "cache", agentName);
+ process.env["LLM_AGENT_ROOT_DIR"] = path.resolve(
+ rootDir,
+ "agents",
+ agentName,
+ );
+ process.env["LLM_AGENT_CACHE_DIR"] = path.resolve(
+ rootDir,
+ "cache",
+ agentName,
+ );
}
function loadEnv(filePath) {
@@ -84,22 +92,26 @@ async function run(agentPath, agentFunc, agentData) {
throw new Error(`Not module function '${agentFunc}' at '${agentPath}'`);
}
const value = await mod[agentFunc](agentData);
- dumpValue(value);
+ returnToLLM(value);
}
-function dumpValue(value) {
+function returnToLLM(value) {
if (value === null || value === undefined) {
return;
}
+ let writer = process.stdout;
+ if (process.env["LLM_OUTPUT"]) {
+ writer = fs.createWriteStream(process.env["LLM_OUTPUT"]);
+ }
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
- console.log(value);
+ writer.write(value);
} else if (type === "object") {
const proto = Object.prototype.toString.call(value);
if (proto === "[object Object]" || proto === "[object Array]") {
const valueStr = JSON.stringify(value, null, 2);
require("assert").deepStrictEqual(value, JSON.parse(valueStr));
- console.log(valueStr);
+ writer.write(value);
}
}
}
diff --git a/scripts/run-agent.py b/scripts/run-agent.py
index 1239753..a3f3ec5 100755
--- a/scripts/run-agent.py
+++ b/scripts/run-agent.py
@@ -50,8 +50,8 @@ def parse_argv(this_file_name):
def setup_env(root_dir, agent_name):
- os.environ["LLM_ROOT_DIR"] = root_dir
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_ROOT_DIR"] = os.path.join(root_dir, "agents", agent_name)
os.environ["LLM_AGENT_CACHE_DIR"] = os.path.join(root_dir, "cache", agent_name)
@@ -85,20 +85,25 @@ def run(agent_path, agent_func, agent_data):
raise Exception(f"Not module function '{agent_func}' at '{agent_path}'")
value = getattr(mod, agent_func)(**agent_data)
- dump_value(value)
+ return_to_llm(value)
-def dump_value(value):
+def return_to_llm(value):
if value is None:
return
+ if "LLM_OUTPUT" in os.environ:
+ writer = open(os.environ["LLM_OUTPUT"], "w")
+ else:
+ writer = sys.stdout
+
value_type = type(value).__name__
if value_type in ("str", "int", "float", "bool"):
- print(value)
+ writer.write(value)
elif value_type == "dict" or value_type == "list":
value_str = json.dumps(value, indent=2)
assert value == json.loads(value_str)
- print(value_str)
+ writer.write(value_str)
if __name__ == "__main__":
diff --git a/scripts/run-agent.sh b/scripts/run-agent.sh
index 967efa5..c9ccb48 100755
--- a/scripts/run-agent.sh
+++ b/scripts/run-agent.sh
@@ -2,16 +2,16 @@
set -e
main() {
- this_file_name=run-agent.sh
- parse_argv "$@"
root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)"
+ self_name=run-agent.sh
+ parse_argv "$@"
setup_env
- agent_tools_path="$root_dir/agents/$agent_name/tools.sh"
- run
+ tools_path="$root_dir/agents/$agent_name/tools.sh"
+ run
}
parse_argv() {
- if [[ "$0" == *"$this_file_name" ]]; then
+ if [[ "$0" == *"$self_name" ]]; then
agent_name="$1"
agent_func="$2"
agent_data="$3"
@@ -26,29 +26,43 @@ parse_argv() {
}
setup_env() {
+ load_env "$root_dir/.env"
export LLM_ROOT_DIR="$root_dir"
- if [[ -f "$LLM_ROOT_DIR/.env" ]]; then
- set -o allexport && source "$LLM_ROOT_DIR/.env" && set +o allexport
- fi
export LLM_AGENT_NAME="$agent_name"
export LLM_AGENT_ROOT_DIR="$LLM_ROOT_DIR/agents/$agent_name"
export LLM_AGENT_CACHE_DIR="$LLM_ROOT_DIR/cache/$agent_name"
}
+load_env() {
+ local env_file="$1" env_vars
+ if [[ -f "$env_file" ]]; then
+ while IFS='=' read -r key value; do
+ if [[ "$key" == $'#'* ]] || [[ -z "$key" ]]; then
+ continue
+ fi
+ if [[ -z "${!key+x}" ]]; then
+ env_vars="$env_vars $key=$value"
+ fi
+ done < "$env_file"
+ if [[ -n "$env_vars" ]]; then
+ eval "export $env_vars"
+ fi
+ fi
+}
+
run() {
if [[ -z "$agent_data" ]]; then
die "No JSON data"
fi
- _jq=jq
if [[ "$OS" == "Windows_NT" ]]; then
- _jq="jq -b"
- agent_tools_path="$(cygpath -w "$agent_tools_path")"
+ set -o igncr
+ tools_path="$(cygpath -w "$tools_path")"
fi
data="$(
echo "$agent_data" | \
- $_jq -r '
+ jq -r '
to_entries | .[] |
(.key | split("_") | join("-")) as $key |
if .value | type == "array" then
@@ -65,10 +79,18 @@ run() {
if [[ "$line" == '--'* ]]; then
args+=("$line")
else
- args+=("$(echo "$line" | $_jq -r '.')")
+ args+=("$(echo "$line" | jq -r '.')")
fi
done <<< "$data"
- "$agent_tools_path" "$agent_func" "${args[@]}"
+ no_llm_output=0
+ if [[ -z "$LLM_OUTPUT" ]]; then
+ no_llm_output=1
+ export LLM_OUTPUT="$(mktemp)"
+ fi
+ "$tools_path" "$agent_func" "${args[@]}"
+ if [[ "$no_llm_output" -eq 1 ]]; then
+ cat "$LLM_OUTPUT"
+ fi
}
die() {
diff --git a/scripts/run-tool.js b/scripts/run-tool.js
index bc09b5e..9728f7d 100755
--- a/scripts/run-tool.js
+++ b/scripts/run-tool.js
@@ -46,8 +46,8 @@ function parseRawData(data) {
}
function setupEnv(rootDir, toolName) {
- process.env["LLM_ROOT_DIR"] = rootDir;
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);
}
@@ -80,22 +80,26 @@ async function run(toolPath, toolFunc, toolData) {
throw new Error(`Not module function '${toolFunc}' at '${toolPath}'`);
}
const value = await mod[toolFunc](toolData);
- dumpValue(value);
+ returnToLLM(value);
}
-function dumpValue(value) {
+function returnToLLM(value) {
if (value === null || value === undefined) {
return;
}
+ let writer = process.stdout;
+ if (process.env["LLM_OUTPUT"]) {
+ writer = fs.createWriteStream(process.env["LLM_OUTPUT"]);
+ }
const type = typeof value;
if (type === "string" || type === "number" || type === "boolean") {
- console.log(value);
+ writer.write(value);
} else if (type === "object") {
const proto = Object.prototype.toString.call(value);
if (proto === "[object Object]" || proto === "[object Array]") {
const valueStr = JSON.stringify(value, null, 2);
require("assert").deepStrictEqual(value, JSON.parse(valueStr));
- console.log(valueStr);
+ writer.write(value);
}
}
}
diff --git a/scripts/run-tool.py b/scripts/run-tool.py
index f5aef4f..b5cdc79 100755
--- a/scripts/run-tool.py
+++ b/scripts/run-tool.py
@@ -47,8 +47,8 @@ def parse_argv(this_file_name):
def setup_env(root_dir, tool_name):
- os.environ["LLM_ROOT_DIR"] = root_dir
load_env(os.path.join(root_dir, ".env"))
+ os.environ["LLM_ROOT_DIR"] = root_dir
os.environ["LLM_TOOL_NAME"] = tool_name
os.environ["LLM_TOOL_CACHE_DIR"] = os.path.join(root_dir, "cache", tool_name)
@@ -81,20 +81,25 @@ def run(tool_path, tool_func, tool_data):
raise Exception(f"Not module function '{tool_func}' at '{tool_path}'")
value = getattr(mod, tool_func)(**tool_data)
- dump_value(value)
+ return_to_llm(value)
-def dump_value(value):
+def return_to_llm(value):
if value is None:
return
+ if "LLM_OUTPUT" in os.environ:
+ writer = open(os.environ["LLM_OUTPUT"], "w")
+ else:
+ writer = sys.stdout
+
value_type = type(value).__name__
if value_type in ("str", "int", "float", "bool"):
- print(value)
+ writer.write(value)
elif value_type == "dict" or value_type == "list":
value_str = json.dumps(value, indent=2)
assert value == json.loads(value_str)
- print(value_str)
+ writer.write(value_str)
if __name__ == "__main__":
diff --git a/scripts/run-tool.sh b/scripts/run-tool.sh
index 4c89d53..d70b292 100755
--- a/scripts/run-tool.sh
+++ b/scripts/run-tool.sh
@@ -2,16 +2,16 @@
set -e
main() {
- this_file_name=run-tool.sh
- parse_argv "$@"
root_dir="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)"
+ self_name=run-tool.sh
+ parse_argv "$@"
setup_env
tool_path="$root_dir/tools/$tool_name.sh"
- run
+ run
}
parse_argv() {
- if [[ "$0" == *"$this_file_name" ]]; then
+ if [[ "$0" == *"$self_name" ]]; then
tool_name="$1"
tool_data="$2"
else
@@ -24,28 +24,42 @@ parse_argv() {
}
setup_env() {
+ load_env "$root_dir/.env"
export LLM_ROOT_DIR="$root_dir"
- if [[ -f "$LLM_ROOT_DIR/.env" ]]; then
- set -o allexport && source "$LLM_ROOT_DIR/.env" && set +o allexport
- fi
export LLM_TOOL_NAME="$tool_name"
export LLM_TOOL_CACHE_DIR="$LLM_ROOT_DIR/cache/$tool_name"
}
+load_env() {
+ local env_file="$1" env_vars
+ if [[ -f "$env_file" ]]; then
+ while IFS='=' read -r key value; do
+ if [[ "$key" == $'#'* ]] || [[ -z "$key" ]]; then
+ continue
+ fi
+ if [[ -z "${!key+x}" ]]; then
+ env_vars="$env_vars $key=$value"
+ fi
+ done < "$env_file"
+ if [[ -n "$env_vars" ]]; then
+ eval "export $env_vars"
+ fi
+ fi
+}
+
run() {
if [[ -z "$tool_data" ]]; then
die "No JSON data"
fi
- _jq=jq
if [[ "$OS" == "Windows_NT" ]]; then
- _jq="jq -b"
+ set -o igncr
tool_path="$(cygpath -w "$tool_path")"
fi
data="$(
echo "$tool_data" | \
- $_jq -r '
+ jq -r '
to_entries | .[] |
(.key | split("_") | join("-")) as $key |
if .value | type == "array" then
@@ -62,10 +76,18 @@ run() {
if [[ "$line" == '--'* ]]; then
args+=("$line")
else
- args+=("$(echo "$line" | $_jq -r '.')")
+ args+=("$(echo "$line" | jq -r '.')")
fi
done <<< "$data"
+ no_llm_output=0
+ if [[ -z "$LLM_OUTPUT" ]]; then
+ no_llm_output=1
+ export LLM_OUTPUT="$(mktemp)"
+ fi
"$tool_path" "${args[@]}"
+ if [[ "$no_llm_output" -eq 1 ]]; then
+ cat "$LLM_OUTPUT"
+ fi
}
die() {