Sha256: c4648a9f5f87e86590472f3145aef353bff1da9b7d2fbb70d56b26e0427ce5f4

Contents?: true

Size: 745 Bytes

Versions: 3

Compression:

Stored size: 745 Bytes

Contents

class ActiveCortex::Generator::Text < ActiveCortex::Generator
  def self.accepts?(record:, field_name:)
    record.class.attribute_types[field_name.to_s].type == :string
  end

  def save_generation
    record.send("#{field_name}=", generation)
  end

  def generation
    openai_content || raise(ActiveCortex::Error, openai_error_message)
  end

  private

  def openai_content
    openai_response["choices"][0]["message"]["content"]
  rescue
    nil
  end

  def openai_error_message
    "Error from OpenAI. " + { response: openai_response }.to_json
  end

  def openai_response
    @openai_response ||= openai_client.chat(parameters: {
      model: model,
      messages: [
        { role: "user", content: prompt }
      ],
    })
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_cortex-0.3.0 lib/active_cortex/generator/text.rb
active_cortex-0.2.1 lib/active_cortex/generator/text.rb
active_cortex-0.2.0 lib/active_cortex/generator/text.rb