luo 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4078773adb559f48cefc4e2bf43e64c0476bd4eb6f4e9ccfa215558c93c2d346
4
- data.tar.gz: 4732dba7c93e21066cc828bc8042617ad29c93a6b50c799c518b8d96332cd640
3
+ metadata.gz: 8d99e88006e5445420b8659fd680122ca1b047e15d9e97e7b62b6fd2144930b9
4
+ data.tar.gz: e7b6e686e73e1daa3bc3d345e8a1523267220e768bdd474d345dc263f2e9ab6b
5
5
  SHA512:
6
- metadata.gz: 521b30ee3642e76b70ff2ddd6d906b0394e7ad3ecee9ff4e69eb4155482c385947ab3051aafd1c38083c9e7e8aca90ca28b1edf8144d662ef76c8e9659be70a3
7
- data.tar.gz: 325450266621354fcda412c1b40c4df88079c2eb985690e43f1b6fae4d7f87a5bf6de7a04ea077c5426a61e5e76c9d93df3908aa560189135228218382e9619e
6
+ metadata.gz: 20eafa8f4fafc7483e3acf28f532729eb6a993dad888d93f5c28d7bd7608643909d7fe771db4772c0e896658d9c3a6587760c9e85c115e81e7d29ea4813afc7a
7
+ data.tar.gz: 0b6cf883f769c6cbad3a178faa16ae6cd02dc7f240335e2b17b30f096b1d4f2b126a07cde18ecf8b5dacf4d8ead7d7a0add0f308abbeb4dd406f818c9778fe68
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- luo (0.2.5)
4
+ luo (0.2.6)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
6
  dry-configurable (~> 1.0, >= 1.0.1)
7
7
  dry-schema (~> 1.13, >= 1.13.1)
data/lib/luo/open_ai.rb CHANGED
@@ -46,7 +46,12 @@ module Luo
46
46
  end
47
47
  params = EMBEDDING_PARAMS.call(input: text, model: model)
48
48
  return params.errors unless params.success?
49
- embeddings(params).body.dig("data").map { |v| v["embedding"] }
49
+ response = embeddings(params)
50
+ if response.success?
51
+ response.body.dig("data").map { |v| v["embedding"] }
52
+ else
53
+ raise "create_embeddings failed: #{response.body}"
54
+ end
50
55
  end
51
56
 
52
57
  def chat(messages, temperature: nil)
@@ -59,7 +64,12 @@ module Luo
59
64
  messages: messages
60
65
  )
61
66
  return params.errors unless params.success?
62
- chat_completions(params).body.dig("choices", 0, "message", "content")
67
+ response = chat_completions(params)
68
+ if response.success?
69
+ response.body.dig("choices", 0, "message", "content")
70
+ else
71
+ raise "request_chat failed: #{response.body}"
72
+ end
63
73
  end
64
74
 
65
75
  class << self
data/lib/luo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Luo
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  end
data/lib/luo/xinghuo.rb CHANGED
@@ -23,15 +23,16 @@ module Luo
23
23
  optional(:max_tokens).maybe(:integer)
24
24
  optional(:random_threshold).maybe(:float)
25
25
  optional(:uid).maybe(:string)
26
+ optional(:stream).maybe(:bool)
26
27
  end
27
28
 
28
29
  # header uid max length is 32 todo
29
30
 
30
- def request_chat(params)
31
- client.post('/v1/spark/completions', params.to_h)
31
+ def request_chat(params, &block)
32
+ client.post('/v1/spark/completions', params.to_h, &block)
32
33
  end
33
34
 
34
- def chat(messages, random_threshold: nil)
35
+ def chat(messages, random_threshold: nil, &block)
35
36
  if messages.is_a?(Messages)
36
37
  messages = messages.to_a
37
38
  end
@@ -41,10 +42,36 @@ module Luo
41
42
  messages: messages,
42
43
  max_tokens: config.max_tokens,
43
44
  random_threshold: random_threshold || config.random_threshold,
44
- uid: config.uid.call
45
+ uid: config.uid.call,
46
+ stream: block_given?
45
47
  )
46
48
  return params.errors unless params.success?
47
- request_chat(params).body.dig('choices', 0, 'message', 'content')
49
+
50
+ body = {}
51
+ if block_given?
52
+ content = ""
53
+ response = request_chat(params) do |req|
54
+ req.options.on_data = Proc.new do |chunk, *|
55
+ if chunk =~ /data: (.+?)\n(?!data: \[DONE\])/
56
+ json = JSON.parse($1)
57
+ content += json.dig('choices', 0, 'delta', 'content')
58
+ body.merge!(json)
59
+ end
60
+ block.call(chunk)
61
+ end
62
+ end
63
+ body['choices'][0]['delta']['content'] = content
64
+ body['choices'][0]['message'] = body['choices'][0].delete('delta')
65
+ else
66
+ response = request_chat(params)
67
+ end
68
+
69
+ if response.success?
70
+ body = response.body if body.empty?
71
+ body.dig('choices', 0, 'message', 'content')
72
+ else
73
+ raise "request_chat failed: #{response.body}"
74
+ end
48
75
  end
49
76
 
50
77
  class << self
@@ -54,6 +81,15 @@ module Luo
54
81
  client.chat(messages, random_threshold: temperature)
55
82
  end
56
83
  end
84
+
85
+ def llm_func_adapter_stream
86
+ client = self.new
87
+ Proc.new do |messages, temperature|
88
+ client.chat(messages, random_threshold: temperature) do |chunk|
89
+ yield chunk
90
+ end
91
+ end
92
+ end
57
93
  end
58
94
 
59
95
  end
@@ -10,17 +10,26 @@ module Luo
10
10
  setting :response_error, default: Luo::Prompts.xinghuo_response_error
11
11
  end
12
12
  setting :client, default: Luo::Xinghuo.new
13
+ setting :stream_callback, default: nil
14
+
15
+ def request(messages)
16
+ if config.stream_callback&.respond_to? :call
17
+ client.chat(messages, &config.stream_callback)
18
+ else
19
+ client.chat(messages)
20
+ end
21
+ end
13
22
 
14
23
  on_request do
15
24
  context.messages = Messages.create(history: context.histories.search(context.user_input))
16
25
  .user(prompt: config.prompts.input, context: {agents: self.class.agents, last_user_input: context.user_input})
17
- response = client.chat(context.messages)
26
+ response = request(context.messages)
18
27
  if response.split("\n").select { |line| line.size >1 }.size > 1
19
28
  message = Messages.create(history: context.histories.search(context.user_input))
20
29
  .user(prompt: config.prompts.input, context: {agents: self.class.agents, last_user_input: context.user_input})
21
30
  .assistant(text: response)
22
31
  .user(prompt: config.prompts.response_error, context: {agents: self.class.agents, last_user_input: context.user_input})
23
- context.response = client.chat(message)
32
+ context.response = request(message)
24
33
  else
25
34
  context.response = response
26
35
  end
@@ -37,7 +46,7 @@ module Luo
37
46
  add_agent(agent)
38
47
  else
39
48
  messages = Messages.create(history: context.histories.search(context.user_input)).user(text: context.user_input)
40
- context.final_result = client.chat(messages)
49
+ context.final_result = request(messages)
41
50
  end
42
51
  end
43
52
 
@@ -5,6 +5,9 @@ Luo.app_setup do |loader|
5
5
  end
6
6
 
7
7
  class Runner < XinghuoAgentRunner
8
+
9
+ setting :stream_callback, default: ->(chunk) { puts chunk }
10
+
8
11
  register WeatherAgent
9
12
  register TimeAgent
10
13
  register XinghuoFinalAgent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - MJ
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-31 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk