Class: Luo::Xinghuo
Constant Summary collapse
- PARAMS =
Dry::Schema.Params do required(:auditing).filled(:string) required(:messages).filled(:array) optional(:domain).maybe(:string) optional(:max_tokens).maybe(:integer) optional(:random_threshold).maybe(:float) optional(:uid).maybe(:string) optional(:stream).maybe(:bool) end
- EMBEDDING_PARAMS =
Dry::Schema.Params do required(:input).filled(:string) end
Class Method Summary collapse
Instance Method Summary collapse
- #chat(messages, random_threshold: nil, &block) ⇒ Object
- #create_embedding(text, model: 'text-embedding-ada-002') ⇒ Object
- #embedding(params) ⇒ Object
-
#request_chat(params, &block) ⇒ Object
header uid max length is 32 todo.
Methods included from Configurable
Class Method Details
.llm_func_adapter ⇒ Object
97 98 99 100 101 102 |
# File 'lib/luo/xinghuo.rb', line 97 def llm_func_adapter client = self.new Proc.new do |, temperature| client.chat(, random_threshold: temperature) end end |
.llm_func_adapter_stream ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/luo/xinghuo.rb', line 104 def llm_func_adapter_stream client = self.new Proc.new do |, temperature| client.chat(, random_threshold: temperature) do |chunk| yield chunk end end end |
Instance Method Details
#chat(messages, random_threshold: nil, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/luo/xinghuo.rb', line 54 def chat(, random_threshold: nil, &block) if .is_a?(Messages) = .to_a end params = PARAMS.call( auditing: config.auditing, domain: config.domain, messages: , max_tokens: config.max_tokens, random_threshold: random_threshold || config.random_threshold, uid: config.uid.call, stream: block_given? ) return params.errors unless params.success? body = {} if block_given? content = "" response = request_chat(params) do |req| req..on_data = Proc.new do |chunk, *| if chunk =~ /data: (.+?)\n(?!data: \[DONE\])/ json = JSON.parse($1) content += json.dig('choices', 0, 'delta', 'content') body.merge!(json) end block.call(chunk) end end body['choices'][0]['delta']['content'] = content body['choices'][0]['message'] = body['choices'][0].delete('delta') else response = request_chat(params) end if response.success? body = response.body if body.empty? body.dig('choices', 0, 'message', 'content') else raise "request_chat failed: #{response.body}" end end |
#create_embedding(text, model: 'text-embedding-ada-002') ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/luo/xinghuo.rb', line 43 def (text, model: 'text-embedding-ada-002') params = EMBEDDING_PARAMS.call(input: text) return params.errors unless params.success? response = (params) if response.success? response.body.dig("data") else raise "create_embeddings failed: #{response.body}" end end |
#embedding(params) ⇒ Object
39 40 41 |
# File 'lib/luo/xinghuo.rb', line 39 def (params) client.post('/v1/embedding', params.to_h) end |
#request_chat(params, &block) ⇒ Object
header uid max length is 32 todo
35 36 37 |
# File 'lib/luo/xinghuo.rb', line 35 def request_chat(params, &block) client.post('/v1/spark/completions', params.to_h, &block) end |