Class: Luo::OpenAI
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
- #chat(messages, temperature: nil) ⇒ Object
- #chat_completions(params) ⇒ Object
- #create_embeddings(text, model: 'text-embedding-ada-002') ⇒ Object
- #embeddings(params) ⇒ Object
Methods included from Configurable
Class Method Details
.llm_func_adapter ⇒ Object
66 67 68 69 70 71 |
# File 'lib/luo/open_ai.rb', line 66 def llm_func_adapter client = self.new Proc.new do |, temperature| client.chat(, 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(, temperature: nil) if .is_a?(Messages) = .to_a end params = PARAMS.call( model: config.model, temperature: temperature || config.temperature, 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 (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? (params).body.dig("data").map { |v| v["embedding"] } end |
#embeddings(params) ⇒ Object
39 40 41 |
# File 'lib/luo/open_ai.rb', line 39 def (params) client.post('/v1/embeddings', params.to_h) end |