aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/cmd.rb
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-05-19 22:43:49 +0800
committerGitHub <noreply@github.com>2024-05-19 22:43:49 +0800
commit03c4b6982293c5be31d7eda89b6dcb8c1a7a8059 (patch)
treea2d48d189dcbe242ab69b174c734db4e70d502a2 /cmd/cmd.rb
parentbe279dcd322da7f84bb6451f10456b19a979e9ad (diff)
downloadllm-functions-docker-03c4b6982293c5be31d7eda89b6dcb8c1a7a8059.tar.gz
feat: supports functions in bash/js/python/ruby (#6)
Diffstat (limited to 'cmd/cmd.rb')
-rwxr-xr-xcmd/cmd.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/cmd/cmd.rb b/cmd/cmd.rb
new file mode 100755
index 0000000..cb67f4d
--- /dev/null
+++ b/cmd/cmd.rb
@@ -0,0 +1,36 @@
+#!/usr/bin/env ruby
+
+require 'json'
+require 'pathname'
+
+def load_module
+ if __FILE__.end_with?("cmd.rb")
+ func_name = ARGV[0]
+ else
+ func_name = Pathname.new(__FILE__).basename.to_s
+ end
+
+ func_name += '.rb' unless func_name.end_with?('.rb')
+ func_path = File.expand_path("../rb/#{func_name}", __dir__)
+
+ begin
+ return require_relative func_path
+ rescue LoadError
+ puts "Invalid ruby function: #{func_name}"
+ exit 1
+ end
+end
+
+if ENV["LLM_FUNCTION_DECLARATE"]
+ declarate = load_module.method(:declarate)
+ puts JSON.pretty_generate(declarate.call)
+else
+ begin
+ data = JSON.parse(ENV["LLM_FUNCTION_DATA"])
+ rescue JSON::ParserError
+ puts "Invalid LLM_FUNCTION_DATA"
+ exit 1
+ end
+ execute = load_module.method(:execute)
+ execute.call(data)
+end \ No newline at end of file