Class: L::OpenAI
- Inherits:
-
Object
- Object
- L::OpenAI
- Defined in:
- lib/lammy/openai.rb
Overview
Use the OpenAI API’s Ruby library
Constant Summary collapse
- MODELS =
%w[ gpt-4o-mini gpt-4o gpt-4-turbo gpt-4 gpt-3.5-turbo gpt-4o-mini-2024-07-18 gpt-4o-2024-08-06 gpt-4o-2024-05-13 gpt-4-turbo-preview gpt-4-turbo-2024-04-09 ].freeze
- EMBEDDINGS =
%w[ text-embedding-3-small text-embedding-3-large text-embedding-ada-002 ].freeze
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
-
#chat(user_message, system_message = nil) ⇒ Object
Generate a response with support for structured output.
-
#embeddings(chunks) ⇒ Object
OpenAI’s text embeddings measure the relatedness of text strings.
-
#initialize(settings) ⇒ OpenAI
constructor
A new instance of OpenAI.
Constructor Details
#initialize(settings) ⇒ OpenAI
Returns a new instance of OpenAI.
20 21 22 |
# File 'lib/lammy/openai.rb', line 20 def initialize(settings) @settings = settings end |
Instance Attribute Details
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
18 19 20 |
# File 'lib/lammy/openai.rb', line 18 def settings @settings end |
Instance Method Details
#chat(user_message, system_message = nil) ⇒ Object
Generate a response with support for structured output
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lammy/openai.rb', line 25 def chat(, = nil) schema = schema(settings) = (, ) response = client.chat( parameters: { model: settings[:model], response_format: schema, messages: }.compact ).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.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lammy/openai.rb', line 44 def (chunks) responses = chunks.map do |chunk| response = client.( parameters: { model: settings[:model], dimensions: settings[:dimensions], input: chunk } ) response.dig('data', 0, 'embedding') end responses.one? ? responses.first : responses end |