Class: SmartPrompt::Engine
- Inherits:
-
Object
- Object
- SmartPrompt::Engine
- Defined in:
- lib/smart_prompt/engine.rb
Instance Attribute Summary collapse
-
#adapters ⇒ Object
readonly
Returns the value of attribute adapters.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#current_adapter ⇒ Object
readonly
Returns the value of attribute current_adapter.
-
#llms ⇒ Object
readonly
Returns the value of attribute llms.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
Instance Method Summary collapse
- #call_worker(worker_name, params = {}) ⇒ Object
- #call_worker_by_stream(worker_name, params = {}, &proc) ⇒ Object
- #check_worker(worker_name) ⇒ Object
- #clear_history_messages ⇒ Object
- #create_dir(filename) ⇒ Object
- #get_worker(worker_name) ⇒ Object
- #history_messages ⇒ Object
-
#initialize(config_file) ⇒ Engine
constructor
A new instance of Engine.
- #load_config(config_file) ⇒ Object
- #load_workers ⇒ Object
Constructor Details
#initialize(config_file) ⇒ Engine
Returns a new instance of Engine.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/smart_prompt/engine.rb', line 5 def initialize(config_file) @config_file = config_file @adapters = {} @llms = {} @templates = {} @current_workers = {} @history_messages = [] load_config(config_file) SmartPrompt.logger.info "Started create the SmartPrompt engine." end |
Instance Attribute Details
#adapters ⇒ Object (readonly)
Returns the value of attribute adapters.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def adapters @adapters end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def config @config end |
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def config_file @config_file end |
#current_adapter ⇒ Object (readonly)
Returns the value of attribute current_adapter.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def current_adapter @current_adapter end |
#llms ⇒ Object (readonly)
Returns the value of attribute llms.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def llms @llms end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
3 4 5 |
# File 'lib/smart_prompt/engine.rb', line 3 def templates @templates end |
Instance Method Details
#call_worker(worker_name, params = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/smart_prompt/engine.rb', line 74 def call_worker(worker_name, params = {}) SmartPrompt.logger.info "Calling worker: #{worker_name} with params: #{params}" worker = get_worker(worker_name) begin result = worker.execute(params) SmartPrompt.logger.info "Worker #{worker_name} executed successfully" if result.class == String = { "role": "assistant", "content": result, } else = { "role": result.dig("choices", 0, "message", "role"), "content": result.dig("choices", 0, "message", "content").to_s + result.dig("choices", 0, "message", "tool_calls").to_s, } end worker.conversation.() result rescue => e SmartPrompt.logger.error "Error executing worker #{worker_name}: #{e.}" SmartPrompt.logger.debug e.backtrace.join("\n") raise end end |
#call_worker_by_stream(worker_name, params = {}, &proc) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/smart_prompt/engine.rb', line 100 def call_worker_by_stream(worker_name, params = {}, &proc) SmartPrompt.logger.info "Calling worker: #{worker_name} with params: #{params}" worker = get_worker(worker_name) begin worker.execute_by_stream(params, &proc) SmartPrompt.logger.info "Worker #{worker_name} executed(stream) successfully" rescue => e SmartPrompt.logger.error "Error executing worker #{worker_name}: #{e.}" SmartPrompt.logger.debug e.backtrace.join("\n") raise end end |
#check_worker(worker_name) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/smart_prompt/engine.rb', line 65 def check_worker(worker_name) if SmartPrompt::Worker.workers[worker_name] return true else SmartPrompt.logger.warn "Invalid worker: #{worker_name}" return false end end |
#clear_history_messages ⇒ Object
126 127 128 |
# File 'lib/smart_prompt/engine.rb', line 126 def @history_messages = [] end |
#create_dir(filename) ⇒ Object
16 17 18 19 20 |
# File 'lib/smart_prompt/engine.rb', line 16 def create_dir(filename) path = File::path(filename).to_s parent_dir = File::dirname(path) Dir.mkdir(parent_dir, 0755) unless File.directory?(parent_dir) end |
#get_worker(worker_name) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/smart_prompt/engine.rb', line 113 def get_worker(worker_name) SmartPrompt.logger.info "Creating worker instance for: #{worker_name}" unless worker = @current_workers[worker_name] worker = Worker.new(worker_name, self) @current_workers[worker_name] = worker end return worker end |
#history_messages ⇒ Object
122 123 124 |
# File 'lib/smart_prompt/engine.rb', line 122 def @history_messages end |
#load_config(config_file) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/smart_prompt/engine.rb', line 22 def load_config(config_file) begin @config_file = config_file @config = YAML.load_file(config_file) if @config["logger_file"] create_dir(@config["logger_file"]) SmartPrompt.logger = Logger.new(@config["logger_file"]) end SmartPrompt.logger.info "Loading configuration from file: #{config_file}" @config["adapters"].each do |adapter_name, adapter_class| adapter_class = SmartPrompt.const_get(adapter_class) @adapters[adapter_name] = adapter_class end @config["llms"].each do |llm_name, llm_config| adapter_class = @adapters[llm_config["adapter"]] @llms[llm_name] = adapter_class.new(llm_config) end @current_llm = @config["default_llm"] if @config["default_llm"] Dir.glob(File.join(@config["template_path"], "*.erb")).each do |file| template_name = file.gsub(@config["template_path"] + "/", "").gsub("\.erb", "") @templates[template_name] = PromptTemplate.new(file) end load_workers rescue Psych::SyntaxError => ex SmartPrompt.logger.error "YAML syntax error in config file: #{ex.}" raise ConfigurationError, "Invalid YAML syntax in config file: #{ex.}" rescue Errno::ENOENT => ex SmartPrompt.logger.error "Config file not found: #{ex.}" raise ConfigurationError, "Config file not found: #{ex.}" rescue StandardError => ex SmartPrompt.logger.error "Error loading configuration: #{ex.}" raise ConfigurationError, "Error loading configuration: #{ex.}" ensure SmartPrompt.logger.info "Configuration loaded successfully" end end |
#load_workers ⇒ Object
59 60 61 62 63 |
# File 'lib/smart_prompt/engine.rb', line 59 def load_workers Dir.glob(File.join(@config["worker_path"], "*.rb")).each do |file| require(file) end end |