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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/luo/open_ai.rb +12 -2
- data/lib/luo/version.rb +1 -1
- data/lib/luo/xinghuo.rb +41 -5
- data/lib/luo/xinghuo_agent_runner.rb +12 -3
- data/templates/application.rb +3 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d99e88006e5445420b8659fd680122ca1b047e15d9e97e7b62b6fd2144930b9
|
4
|
+
data.tar.gz: e7b6e686e73e1daa3bc3d345e8a1523267220e768bdd474d345dc263f2e9ab6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20eafa8f4fafc7483e3acf28f532729eb6a993dad888d93f5c28d7bd7608643909d7fe771db4772c0e896658d9c3a6587760c9e85c115e81e7d29ea4813afc7a
|
7
|
+
data.tar.gz: 0b6cf883f769c6cbad3a178faa16ae6cd02dc7f240335e2b17b30f096b1d4f2b126a07cde18ecf8b5dacf4d8ead7d7a0add0f308abbeb4dd406f818c9778fe68
|
data/Gemfile.lock
CHANGED
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)
|
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)
|
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
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
|
-
|
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 =
|
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 =
|
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 =
|
49
|
+
context.final_result = request(messages)
|
41
50
|
end
|
42
51
|
end
|
43
52
|
|
data/templates/application.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2023-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|