aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/bin.py
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-06 11:40:09 +0800
committerGitHub <noreply@github.com>2024-06-06 11:40:09 +0800
commita144077aec90d13587d67b3a2065d5728d4f6319 (patch)
treedd5a81e34c160771817e940c49bedb20e02615b4 /scripts/bin.py
parent6f82b1f53af60fccb35aa7a13c13e6d9ebe7796f (diff)
downloadllm-functions-docker-a144077aec90d13587d67b3a2065d5728d4f6319.tar.gz
refactor: move run/tool.* to scripts/bin.* (#22)
Diffstat (limited to 'scripts/bin.py')
-rwxr-xr-xscripts/bin.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/scripts/bin.py b/scripts/bin.py
new file mode 100755
index 0000000..6c56ea0
--- /dev/null
+++ b/scripts/bin.py
@@ -0,0 +1,70 @@
+#!/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("bin.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):
+ func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{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)
+
+def load_env(file_path):
+ try:
+ with open(file_path, 'r') as f:
+ for line in f:
+ line = line.strip()
+ if line.startswith('#') or line == '':
+ continue
+
+ key, *value = line.split('=')
+ os.environ[key.strip()] = '='.join(value).strip()
+ except FileNotFoundError:
+ pass
+
+os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..")
+
+load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env"))
+
+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