Class: Luo::OpenAI

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/luo/open_ai.rb

Constant Summary collapse

PARAMS =
Dry::Schema.Params do
  required(:model).filled(:string)
  required(:temperature).filled(:float)
  required(:messages).filled(:array)
  optional(:top_p).maybe(:float)
  optional(:n).maybe(:integer)
  optional(:stream).maybe(:bool)
  optional(:stop).maybe(:array)
  optional(:max_tokens).maybe(:integer)
  optional(:presence_penalty).maybe(:float)
  optional(:frequency_penalty).maybe(:float)
  optional(:logit_bias).maybe(:hash)
  optional(:user).maybe(:string)
end
EMBEDDING_PARAMS =
Dry::Schema.Params do
  required(:input).filled(:array)
  required(:model).filled(:string)
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#config, included

Class Method Details

.llm_func_adapterObject



66
67
68
69
70
71
# File 'lib/luo/open_ai.rb', line 66

def llm_func_adapter
  client = self.new
  Proc.new do |messages, temperature|
    client.chat(messages, temperature: temperature)
  end
end

Instance Method Details

#chat(messages, temperature: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/luo/open_ai.rb', line 52

def chat(messages, temperature: nil)
  if messages.is_a?(Messages)
    messages = messages.to_a
  end
  params = PARAMS.call(
    model: config.model,
    temperature: temperature || config.temperature,
    messages: messages
  )
  return params.errors unless params.success?
  chat_completions(params).body.dig("choices", 0, "message", "content")
end

#chat_completions(params) ⇒ Object



35
36
37
# File 'lib/luo/open_ai.rb', line 35

def chat_completions(params)
  client.post('/v1/chat/completions', params.to_h)
end

#create_embeddings(text, model: 'text-embedding-ada-002') ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/luo/open_ai.rb', line 43

def create_embeddings(text, model: 'text-embedding-ada-002')
  if text.is_a?(String)
    text = [text]
  end
  params = EMBEDDING_PARAMS.call(input: text, model: model)
  return params.errors unless params.success?
  embeddings(params).body.dig("data").map { |v| v["embedding"] }
end

#embeddings(params) ⇒ Object



39
40
41
# File 'lib/luo/open_ai.rb', line 39

def embeddings(params)
  client.post('/v1/embeddings', params.to_h)
end