Module: Raix::Predicate
Overview
A module for handling yes/no questions using AI chat completion. When included in a class, it provides methods to define handlers for yes and no responses. All handlers are optional. Any response that does not begin with “yes, ” or “no, ” will be considered a maybe.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary
Attributes included from ChatCompletion
#available_tools, #cache_at, #frequency_penalty, #logit_bias, #logprobs, #loop, #max_completion_tokens, #max_tokens, #max_tool_calls, #min_p, #model, #prediction, #presence_penalty, #provider, #repetition_penalty, #response_format, #seed, #stop, #stop_tool_calls_and_respond, #stream, #temperature, #tool_choice, #tools, #top_a, #top_k, #top_logprobs, #top_p
Instance Method Summary collapse
Methods included from ChatCompletion
#chat_completion, #configuration, #dispatch_tool_function, #transcript
Instance Method Details
#ask(question, openai: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/raix/predicate.rb', line 32 def ask(question, openai: false) raise "Please define a yes and/or no block" if self.class.yes_block.nil? && self.class.no_block.nil? transcript << { system: "Always answer 'Yes, ', 'No, ', or 'Maybe, ' followed by a concise explanation!" } transcript << { user: question } chat_completion(openai:).tap do |response| if response.downcase.start_with?("yes,") instance_exec(response, &self.class.yes_block) if self.class.yes_block elsif response.downcase.start_with?("no,") instance_exec(response, &self.class.no_block) if self.class.no_block elsif self.class.maybe_block instance_exec(response, &self.class.maybe_block) else puts "[Raix::Predicate] Unhandled response: #{response}" end end end |