diff options
| author | sigoden <sigoden@gmail.com> | 2024-05-21 08:26:47 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-21 08:26:47 +0800 |
| commit | ef43b22a8e6d4eb7388a552ba6d52d662e38fd0c (patch) | |
| tree | cf03ce9e37b5e3068aa38aa2213b558d210bc1a0 /cmd/cmd.rb | |
| parent | e6642b5898ef3c46b2d37ab8dfce18f5db0a5618 (diff) | |
| download | llm-functions-docker-ef43b22a8e6d4eb7388a552ba6d52d662e38fd0c.tar.gz | |
feat: rewrite to accept json data from cli args other than env var (#7)
* update readme
Diffstat (limited to 'cmd/cmd.rb')
| -rwxr-xr-x | cmd/cmd.rb | 48 |
1 files changed, 33 insertions, 15 deletions
@@ -3,34 +3,52 @@ require 'json' require 'pathname' -def load_module - if __FILE__.end_with?("cmd.rb") - func_name = ARGV[0] +def parse_argv + func_file = __FILE__ + func_data = nil + + if func_file.end_with?("cmd.rb") + func_file = ARGV[0] + func_data = ARGV[1] else - func_name = Pathname.new(__FILE__).basename.to_s + func_file = File.basename(func_file) + func_data = ARGV[0] end - func_name += '.rb' unless func_name.end_with?('.rb') - func_path = File.expand_path("../rb/#{func_name}", __dir__) + func_file += '.rb' unless func_file.end_with?(".rb") + + [func_file, func_data] +end + +def load_func(func_file) + func_path = File.expand_path("../rb/#{func_file}", __dir__) begin - return require_relative func_path + require func_path rescue LoadError - puts "Invalid ruby function: #{func_name}" + puts "Invalid function: #{func_file}" exit 1 end end -if ENV["LLM_FUNCTION_DECLARATE"] - declarate = load_module.method(:declarate) - puts JSON.pretty_generate(declarate.call) +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 - data = JSON.parse(ENV["LLM_FUNCTION_DATA"]) + args = JSON.parse(func_data) rescue JSON::ParserError - puts "Invalid LLM_FUNCTION_DATA" + puts "Invalid json data" exit 1 end - execute = load_module.method(:execute) - execute.call(data) + + load_func(func_file) + execute(args) end
\ No newline at end of file |
