Skip to content

Instantly share code, notes, and snippets.

@trobrock
Created November 29, 2023 18:08
Show Gist options
  • Save trobrock/6b3a973f49220fa1f71c7c9c193cb9b5 to your computer and use it in GitHub Desktop.
Save trobrock/6b3a973f49220fa1f71c7c9c193cb9b5 to your computer and use it in GitHub Desktop.
OpenAI in Rails
# frozen_string_literal: true
module Intelligence
extend ActiveSupport::Concern
MODEL = 'gpt-4-1106-preview'
private
def get_chat_completion(json: true)
return stub_data if use_stub_data?
parameters = {
model: MODEL,
messages: prompt_messages
}
parameters[:response_format] = { type: 'json_object' } if json
response = openai_client.chat(parameters:)
JSON.parse(response.dig('choices', 0, 'message', 'content')).deep_symbolize_keys
end
def openai_client
@openai_client ||= OpenAI::Client.new
end
def prompt_messages
[
{ role: 'system', content: system_prompt },
{ role: 'user', content: user_prompt }
]
end
def user_prompt
fail NotImplementedError, 'You must implement the user_prompt method'
end
def system_prompt
fail NotImplementedError, 'You must implement the system_prompt method'
end
def use_stub_data?
stub_data.present? && Rails.env.development?
end
def stub_data; end
end
# frozen_string_literal: true
class Prompt
def self.load(name, **params)
template = File.read("app/prompts/#{name}.txt.erb")
erb = ERB.new(template)
context = OpenStruct.new(params) # rubocop:disable Style/OpenStructUse
erb.result(context.instance_eval { binding })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment