feat: set environment variable LLM_FUNCTION_NAME (#27)

This commit is contained in:
sigoden
2024-06-06 16:33:24 +08:00
committed by GitHub
parent 26b4338b81
commit 39447fb39e
3 changed files with 72 additions and 62 deletions
+18 -15
View File
@@ -6,30 +6,31 @@ import sys
import importlib.util
def parse_argv():
func_file = sys.argv[0]
func_name = sys.argv[0]
func_data = None
if func_file.endswith("bin.py"):
func_file = sys.argv[1] if len(sys.argv) > 1 else None
if func_name.endswith("bin.py"):
func_name = 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_name = os.path.basename(func_name)
func_data = sys.argv[1] if len(sys.argv) > 1 else None
if not func_file.endswith(".py"):
func_file += ".py"
if func_name.endswith(".py"):
func_name = func_name[:-3]
return func_file, func_data
return func_name, func_data
def load_func(func_file):
func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file}")
def load_func(func_name):
func_file_name = f"{func_name}.py"
func_path = os.path.join(os.environ["LLM_FUNCTIONS_DIR"], f"tools/{func_file_name}")
if os.path.exists(func_path):
spec = importlib.util.spec_from_file_location(func_file, func_path)
spec = importlib.util.spec_from_file_location(f"{func_file_name}", func_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
else:
print(f"Invalid function: {func_file}")
print(f"Invalid function: {func_file_name}")
sys.exit(1)
def load_env(file_path):
@@ -45,14 +46,16 @@ def load_env(file_path):
except FileNotFoundError:
pass
os.environ["LLM_FUNCTIONS_DIR"] = os.path.join(os.path.dirname(__file__), "..")
os.environ["LLM_FUNCTIONS_DIR"] = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
load_env(os.path.join(os.environ["LLM_FUNCTIONS_DIR"], ".env"))
func_file, func_data = parse_argv()
func_name, func_data = parse_argv()
os.environ["LLM_FUNCTION_NAME"] = func_name
if os.getenv("LLM_FUNCTION_ACTION") == "declarate":
module = load_func(func_file)
module = load_func(func_name)
print(json.dumps(module.declarate(), indent=2))
else:
if not func_data:
@@ -66,5 +69,5 @@ else:
print("Invalid json data")
sys.exit(1)
module = load_func(func_file)
module = load_func(func_name)
module.execute(args)