Module: Raix::PromptDeclarations

Extended by:
ActiveSupport::Concern, ChatCompletion
Defined in:
lib/raix/prompt_declarations.rb

Overview

The PromptDeclarations module provides a way to chain prompts and handle user responses in a serialized manner (in the order they were defined), with support for functions if the FunctionDispatch module is also included.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary

Attributes included from ChatCompletion

#frequency_penalty, #logit_bias, #logprobs, #loop, #min_p, #presence_penalty, #provider, #repetition_penalty, #response_format, #seed, #stop, #stream, #tool_choice, #tools, #top_a, #top_k, #top_logprobs, #top_p

Instance Method Summary collapse

Methods included from ChatCompletion

transcript

Instance Method Details

#chat_completion(params: {}, raw: false) ⇒ Object

Executes the chat completion process based on the class-level declared prompts. The response to each prompt is added to the transcript automatically and returned.

Prompts require at least a ‘text` lambda parameter.

Uses system prompt in following order of priority:

 - system lambda specified in the prompt declaration
 - system_prompt instance method if defined
 - system_prompt class-level declaration if defined

TODO: shortcut syntax passes just a string prompt if no other options are needed.

Parameters:

  • params (Hash) (defaults to: {})

    Parameters for the chat completion override those defined in the current prompt.

Options Hash (params:):

  • :raw (Boolean) — default: false

    Whether to return the raw response or dig the text content.

Raises:

  • (RuntimeError)

    If no prompts are defined.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/raix/prompt_declarations.rb', line 70

def chat_completion(params: {}, raw: false)
  raise "No prompts defined" unless self.class.prompts.present?

  current_prompts = self.class.prompts.clone

  while (@current_prompt = current_prompts.shift)
    __system_prompt = instance_exec(&@current_prompt.system) if @current_prompt.system.present? # rubocop:disable Lint/UnderscorePrefixedVariableName
    __system_prompt ||= system_prompt if respond_to?(:system_prompt)
    __system_prompt ||= self.class.system_prompt.presence
    transcript << { system: __system_prompt } if __system_prompt
    transcript << { user: instance_exec(&@current_prompt.text) } # text is required

    params = @current_prompt.params.merge(params)

    super(params:, raw:).then do |response|
      transcript << { assistant: response }
      @last_response = send(@current_prompt.name, response)
    end
  end

  @last_response
end

#max_tokensInteger

Returns the max_tokens parameter of the current prompt or the default max_tokens.

Returns:

  • (Integer)

    The max_tokens parameter of the current prompt or the default max_tokens.



110
111
112
# File 'lib/raix/prompt_declarations.rb', line 110

def max_tokens
  @current_prompt.params[:max_tokens] || super
end

#modelObject

Returns the model parameter of the current prompt or the default model.

Returns:

  • (Object)

    The model parameter of the current prompt or the default model.



96
97
98
# File 'lib/raix/prompt_declarations.rb', line 96

def model
  @current_prompt.params[:model] || super
end

#temperatureFloat

Returns the temperature parameter of the current prompt or the default temperature.

Returns:

  • (Float)

    The temperature parameter of the current prompt or the default temperature.



103
104
105
# File 'lib/raix/prompt_declarations.rb', line 103

def temperature
  @current_prompt.params[:temperature] || super
end