Class: Lammy::OpenAI

Inherits:
Object
  • Object
show all
Defined in:
lib/lammy/openai.rb

Overview

Use the OpenAI API’s Ruby library

Constant Summary collapse

MODELS =
[
  /^gpt-4o(?:-\d{4}-\d{2}-\d{2})?$/, /^chatgpt-4o-latest$/,
  /^gpt-4o-mini(?:-\d{4}-\d{2}-\d{2})?$/,
  /^o1(?:-preview(?:-\d{4}-\d{2}-\d{2})?)?$/,
  /^o1-mini(?:-\d{4}-\d{2}-\d{2})?$/,
  /^gpt-3\.5-turbo$/,
  /^gpt-4(?:-turbo(?:-\d{4}-\d{2}-\d{2})?|-32k|-\d{4}-preview|-vision-preview)?$/,
  /^gpt-3\.5-turbo-(?:\d{4}|\d{2}k-\d{4}|-instruct)$/,
  /^(?:davinci|babbage)-002$/
].freeze
EMBEDDINGS =
%w[
  text-embedding-3-small text-embedding-3-large text-embedding-ada-002
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ OpenAI

Returns a new instance of OpenAI.



27
28
29
# File 'lib/lammy/openai.rb', line 27

def initialize(settings)
  @settings = settings
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



25
26
27
# File 'lib/lammy/openai.rb', line 25

def settings
  @settings
end

Instance Method Details

#chat(user_message, system_message = nil, stream = nil) ⇒ Object

Generate a response with support for structured output



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lammy/openai.rb', line 32

def chat(user_message, system_message = nil, stream = nil)
  schema = schema(settings)
  messages = messages(user_message, system_message)

  request = client.chat(
    parameters: {
      model: settings[:model] || Lammy.configuration.model,
      response_format: schema,
      messages: messages,
      stream: stream ? ->(chunk) { stream.call(stream_content(chunk)) } : nil
    }.compact
  )

  return stream if stream

  response = request.dig('choices', 0, 'message', 'content')
  content = schema ? ::Hashie::Mash.new(JSON.parse(response)) : response
  array?(schema) ? content.items : content
end

#embeddings(chunks) ⇒ Object

OpenAI’s text embeddings measure the relatedness of text strings. An embedding is a vector of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lammy/openai.rb', line 55

def embeddings(chunks)
  responses = chunks.map do |chunk|
    response = client.embeddings(
      parameters: { model: settings[:model], dimensions: settings[:dimensions], input: chunk }
    )

    response.dig('data', 0, 'embedding')
  end

  responses.one? ? responses.first : responses
end