diff options
| author | sigoden <sigoden@gmail.com> | 2024-06-05 18:37:52 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-05 18:37:52 +0800 |
| commit | 005d23030dab2b7708996e779dc78dd5f8ebdb8d (patch) | |
| tree | 0d05583d462a4795a4236a5006befc096047e969 /run | |
| parent | 7e3f47093f2dcb36cf94a35403027ec72bf7b084 (diff) | |
| download | llm-functions-docker-005d23030dab2b7708996e779dc78dd5f8ebdb8d.tar.gz | |
feat: adjust project structure (#16)
Diffstat (limited to 'run')
| -rwxr-xr-x | run/tool.js | 55 | ||||
| -rwxr-xr-x | run/tool.py | 54 | ||||
| -rwxr-xr-x | run/tool.rb | 54 | ||||
| -rwxr-xr-x | run/tool.sh | 91 |
4 files changed, 254 insertions, 0 deletions
diff --git a/run/tool.js b/run/tool.js new file mode 100755 index 0000000..de97423 --- /dev/null +++ b/run/tool.js @@ -0,0 +1,55 @@ +#!/usr/bin/env node + +const path = require("path"); + +function parseArgv() { + let func_file = process.argv[1]; + let func_data = null; + + if (func_file.endsWith("tool.js")) { + func_file = process.argv[2] + func_data = process.argv[3] + } else { + func_file = path.basename(func_file) + func_data = process.argv[2]; + } + + if (!func_file.endsWith(".js")) { + func_file += '.js' + } + + return [func_file, func_data] +} + +function loadFunc(func_file) { + const func_path = path.resolve(__dirname, `../tools/js/${func_file}`) + try { + return require(func_path); + } catch { + console.log(`Invalid function: ${func_file}`) + process.exit(1) + } +} + +const [func_file, func_data] = parseArgv(); + +if (process.env["LLM_FUNCTION_ACTION"] == "declarate") { + const { declarate } = loadFunc(func_file); + console.log(JSON.stringify(declarate(), null, 2)) +} else { + if (!func_data) { + console.log("No json data"); + process.exit(1) + } + + let args; + try { + args = JSON.parse(func_data) + } catch { + console.log("Invalid json data") + process.exit(1) + } + + const { execute } = loadFunc(func_file); + execute(args) +}
\ No newline at end of file diff --git a/run/tool.py b/run/tool.py new file mode 100755 index 0000000..0555363 --- /dev/null +++ b/run/tool.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import os +import json +import sys +import importlib.util + +def parse_argv(): + func_file = sys.argv[0] + func_data = None + + if func_file.endswith("tool.py"): + func_file = sys.argv[1] if len(sys.argv) > 1 else None + func_data = sys.argv[2] if len(sys.argv) > 2 else None + else: + func_file = os.path.basename(func_file) + func_data = sys.argv[1] if len(sys.argv) > 1 else None + + if not func_file.endswith(".py"): + func_file += ".py" + + return func_file, func_data + +def load_func(func_file): + base_dir = os.path.dirname(os.path.abspath(__file__)) + func_path = os.path.join(base_dir, f"../tools/py/{func_file}") + if os.path.exists(func_path): + spec = importlib.util.spec_from_file_location(func_file, func_path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + else: + print(f"Invalid function: {func_file}") + sys.exit(1) + +func_file, func_data = parse_argv() + +if os.getenv("LLM_FUNCTION_ACTION") == "declarate": + module = load_func(func_file) + print(json.dumps(module.declarate(), indent=2)) +else: + if not func_data: + print("No json data") + sys.exit(1) + + args = None + try: + args = json.loads(func_data) + except (json.JSONDecodeError, TypeError): + print("Invalid json data") + sys.exit(1) + + module = load_func(func_file) + module.execute(args)
\ No newline at end of file diff --git a/run/tool.rb b/run/tool.rb new file mode 100755 index 0000000..4a692ef --- /dev/null +++ b/run/tool.rb @@ -0,0 +1,54 @@ +#!/usr/bin/env ruby + +require 'json' +require 'pathname' + +def parse_argv + func_file = __FILE__ + func_data = nil + + if func_file.end_with?("tool.rb") + func_file = ARGV[0] + func_data = ARGV[1] + else + func_file = File.basename(func_file) + func_data = ARGV[0] + end + + func_file += '.rb' unless func_file.end_with?(".rb") + + [func_file, func_data] +end + +def load_func(func_file) + func_path = File.expand_path("../tools/rb/#{func_file}", __dir__) + + begin + require func_path + rescue LoadError + puts "Invalid function: #{func_file}" + exit 1 + end +end + +func_file, func_data = parse_argv + +if ENV["LLM_FUNCTION_ACTION"] == "declarate" + load_func(func_file) + puts JSON.pretty_generate(declarate) +else + if func_data.nil? + puts "No json data" + exit 1 + end + + begin + args = JSON.parse(func_data) + rescue JSON::ParserError + puts "Invalid json data" + exit 1 + end + + load_func(func_file) + execute(args) +end
\ No newline at end of file diff --git a/run/tool.sh b/run/tool.sh new file mode 100755 index 0000000..da9e412 --- /dev/null +++ b/run/tool.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +set -e + +if [[ "$0" == *tool.sh ]]; then + FUNC_FILE="$1" + FUNC_DATA="$2" +else + FUNC_FILE="$(basename "$0")" + FUNC_DATA="$1" +fi +if [[ "$FUNC_FILE" != *'.sh' ]]; then + FUNC_FILE="$FUNC_FILE.sh" +fi + +PROJECT_DIR="$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd)" +FUNC_FILE="$PROJECT_DIR/tools/sh/$FUNC_FILE" + +if [[ "$OS" == "Windows_NT" ]]; then + FUNC_FILE="$(cygpath -w "$FUNC_FILE")" +fi + +if [[ "$LLM_FUNCTION_ACTION" == "declarate" ]]; then + argc --argc-export "$FUNC_FILE" | \ + jq -r ' + def parse_description(flag_option): + if flag_option.describe == "" then + {} + else + { "description": flag_option.describe } + end; + + def parse_enum(flag_option): + if flag_option.choice.type == "Values" then + { "enum": flag_option.choice.data } + else + {} + end; + + def parse_property(flag_option): + [ + { condition: (flag_option.flag == true), result: { type: "boolean" } }, + { condition: (flag_option.multiple_occurs == true), result: { type: "array", items: { type: "string" } } }, + { condition: (flag_option.notations[0] == "INT"), result: { type: "integer" } }, + { condition: (flag_option.notations[0] == "NUM"), result: { type: "number" } }, + { condition: true, result: { type: "string" } } ] + | map(select(.condition) | .result) | first + | (. + parse_description(flag_option)) + | (. + parse_enum(flag_option)) + ; + + + def parse_parameter(flag_options): + { + type: "object", + properties: (reduce flag_options[] as $item ({}; . + { ($item.id | sub("-"; "_"; "g")): parse_property($item) })), + required: [flag_options[] | select(.required == true) | .id], + }; + + { + name: (.name | sub("-"; "_"; "g")), + description: .describe, + parameters: parse_parameter([.flag_options[] | select(.id != "help" and .id != "version")]) + }' +else + if [[ -z "$FUNC_DATA" ]]; then + echo "No json data" + exit 1 + fi + + data="$( + echo "$FUNC_DATA" | \ + jq -r ' + to_entries | .[] | + (.key | split("_") | join("-")) as $key | + if .value | type == "array" then + .value | .[] | "--\($key)\n\(.)" + elif .value | type == "boolean" then + if .value then "--\($key)" else "" end + else + "--\($key)\n\(.value)" + end' | \ + tr -d '\r' + )" || { + echo "Invalid json data" + exit 1 + } + while IFS= read -r line; do + ARGS+=("$line") + done <<< "$data" + "$FUNC_FILE" "${ARGS[@]}" +fi
\ No newline at end of file |
