Class: Luo::Xinghuo

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

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

Methods included from Configurable

#config, included

Class Method Details

.llm_func_adapterObject



97
98
99
100
101
102
# File 'lib/luo/xinghuo.rb', line 97

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

.llm_func_adapter_streamObject



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 |messages, temperature|
    client.chat(messages, 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(messages, random_threshold: nil, &block)
  if messages.is_a?(Messages)
    messages = messages.to_a
  end
  params = PARAMS.call(
    auditing: config.auditing,
    domain: config.domain,
    messages: 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.options.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 create_embedding(text, model: 'text-embedding-ada-002')
  params = EMBEDDING_PARAMS.call(input: text)
  return params.errors unless params.success?
  response = embedding(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 embedding(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