smart_prompt 0.2.1 → 0.2.2
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/lib/smart_prompt/conversation.rb +24 -20
- data/lib/smart_prompt/llm_adapter.rb +1 -0
- data/lib/smart_prompt/openai_adapter.rb +13 -18
- data/lib/smart_prompt/version.rb +1 -1
- data/lib/smart_prompt/worker.rb +2 -2
- 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: 671745a347fa5443d42ce018a0c00870f5c1c13771060d1facea3ecf6cff41d6
|
4
|
+
data.tar.gz: 5a2f1177a1ed5de38daee6422586841c8358c869cd3e9fc71d4b67078a5438e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fed26cf6880bf7b9bb2e71c2e4013c66a25b9b542bc88e5727025eef57731d5024dc3a01d548ed3d76f404646835666540dc7ea056477f8abd9cc2b28bddeeb
|
7
|
+
data.tar.gz: 1ac2f3ed8fc1b664a623888193d82fe114ef8a8a8f43b6dd8600875132d617af624ccca6d0c6f10814759ae881e51c2351ba6f4bba3b09f89b426381da470fb7
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "yaml"
|
2
|
+
require "retriable"
|
3
3
|
require "numo/narray"
|
4
4
|
|
5
5
|
module SmartPrompt
|
@@ -7,7 +7,7 @@ module SmartPrompt
|
|
7
7
|
include APIHandler
|
8
8
|
attr_reader :messages, :last_response, :config_file
|
9
9
|
|
10
|
-
def initialize(engine)
|
10
|
+
def initialize(engine, tools = nil)
|
11
11
|
SmartPrompt.logger.info "Create Conversation"
|
12
12
|
@messages = []
|
13
13
|
@engine = engine
|
@@ -16,6 +16,7 @@ module SmartPrompt
|
|
16
16
|
@templates = engine.templates
|
17
17
|
@current_adapter = engine.current_adapter
|
18
18
|
@last_response = nil
|
19
|
+
@tools = tools
|
19
20
|
end
|
20
21
|
|
21
22
|
def use(llm_name)
|
@@ -38,46 +39,49 @@ module SmartPrompt
|
|
38
39
|
SmartPrompt.logger.info "Use template #{template_name}"
|
39
40
|
raise "Template #{template_name} not found" unless @templates.key?(template_name)
|
40
41
|
content = @templates[template_name].render(params)
|
41
|
-
@messages << { role:
|
42
|
+
@messages << { role: "user", content: content }
|
42
43
|
self
|
43
44
|
else
|
44
|
-
@messages << { role:
|
45
|
+
@messages << { role: "user", content: template_name }
|
45
46
|
self
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
50
|
def sys_msg(message)
|
50
51
|
@sys_msg = message
|
51
|
-
@messages << { role:
|
52
|
+
@messages << { role: "system", content: message }
|
52
53
|
self
|
53
54
|
end
|
54
55
|
|
55
56
|
def send_msg_once
|
56
57
|
raise "No LLM selected" if @current_llm.nil?
|
57
58
|
@last_response = @current_llm.send_request(@messages, @model_name, @temperature)
|
58
|
-
@messages=[]
|
59
|
-
@messages << { role:
|
59
|
+
@messages = []
|
60
|
+
@messages << { role: "system", content: @sys_msg }
|
60
61
|
@last_response
|
61
62
|
end
|
62
63
|
|
63
64
|
def send_msg
|
64
65
|
Retriable.retriable(RETRY_OPTIONS) do
|
65
66
|
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
|
66
|
-
@last_response = @current_llm.send_request(@messages, @model_name, @temperature)
|
67
|
-
@
|
68
|
-
|
67
|
+
@last_response = @current_llm.send_request(@messages, @model_name, @temperature, @tools, nil)
|
68
|
+
if @last_response == ""
|
69
|
+
@last_response = @current_llm.last_response
|
70
|
+
end
|
71
|
+
@messages = []
|
72
|
+
@messages << { role: "system", content: @sys_msg }
|
69
73
|
@last_response
|
70
74
|
end
|
71
|
-
rescue => e
|
75
|
+
rescue => e
|
72
76
|
return "Failed to call LLM after #{MAX_RETRIES} attempts: #{e.message}"
|
73
77
|
end
|
74
78
|
|
75
79
|
def send_msg_by_stream(&proc)
|
76
80
|
Retriable.retriable(RETRY_OPTIONS) do
|
77
81
|
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
|
78
|
-
@current_llm.send_request(@messages, @model_name, @temperature, proc)
|
79
|
-
@messages=[]
|
80
|
-
@messages << { role:
|
82
|
+
@current_llm.send_request(@messages, @model_name, @temperature, @tools, proc)
|
83
|
+
@messages = []
|
84
|
+
@messages << { role: "system", content: @sys_msg }
|
81
85
|
end
|
82
86
|
rescue => e
|
83
87
|
return "Failed to call LLM after #{MAX_RETRIES} attempts: #{e.message}"
|
@@ -85,7 +89,7 @@ module SmartPrompt
|
|
85
89
|
|
86
90
|
def normalize(x, length)
|
87
91
|
if x.length > length
|
88
|
-
x = Numo::NArray.cast(x[0..length-1])
|
92
|
+
x = Numo::NArray.cast(x[0..length - 1])
|
89
93
|
norm = Math.sqrt((x * x).sum)
|
90
94
|
return (x / norm).to_a
|
91
95
|
else
|
@@ -98,15 +102,15 @@ module SmartPrompt
|
|
98
102
|
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
|
99
103
|
text = ""
|
100
104
|
@messages.each do |msg|
|
101
|
-
if msg[:role]=="user"
|
105
|
+
if msg[:role] == "user"
|
102
106
|
text = msg[:content]
|
103
107
|
end
|
104
108
|
end
|
105
109
|
@last_response = @current_llm.embeddings(text, @model_name)
|
106
|
-
@messages=[]
|
107
|
-
@messages << { role:
|
110
|
+
@messages = []
|
111
|
+
@messages << { role: "system", content: @sys_msg }
|
108
112
|
normalize(@last_response, length)
|
109
113
|
end
|
110
114
|
end
|
111
115
|
end
|
112
|
-
end
|
116
|
+
end
|
@@ -31,7 +31,7 @@ module SmartPrompt
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
def send_request(messages, model=nil, temperature=0.7, proc)
|
34
|
+
def send_request(messages, model=nil, temperature=0.7, tools=nil, proc=nil)
|
35
35
|
SmartPrompt.logger.info "OpenAIAdapter: Sending request to OpenAI"
|
36
36
|
if model
|
37
37
|
model_name = model
|
@@ -40,24 +40,18 @@ module SmartPrompt
|
|
40
40
|
end
|
41
41
|
SmartPrompt.logger.info "OpenAIAdapter: Using model #{model_name}"
|
42
42
|
begin
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
parameters: {
|
54
|
-
model: model_name,
|
55
|
-
messages: messages,
|
56
|
-
temperature: @config['temperature'] || temperature,
|
57
|
-
stream: proc
|
58
|
-
}
|
59
|
-
)
|
43
|
+
parameters = {
|
44
|
+
model: model_name,
|
45
|
+
messages: messages,
|
46
|
+
temperature: @config['temperature'] || temperature
|
47
|
+
}
|
48
|
+
if proc
|
49
|
+
parameters[:stream]=proc
|
50
|
+
end
|
51
|
+
if tools
|
52
|
+
parameters[:tools]=tools
|
60
53
|
end
|
54
|
+
response = @client.chat(parameters: parameters)
|
61
55
|
rescue OpenAI::Error => e
|
62
56
|
SmartPrompt.logger.error "OpenAI API error: #{e.message}"
|
63
57
|
raise LLMAPIError, "OpenAI API error: #{e.message}"
|
@@ -75,6 +69,7 @@ module SmartPrompt
|
|
75
69
|
end
|
76
70
|
SmartPrompt.logger.info "OpenAIAdapter: Received response from OpenAI"
|
77
71
|
if proc == nil
|
72
|
+
@last_response = response
|
78
73
|
return response.dig("choices", 0, "message", "content")
|
79
74
|
end
|
80
75
|
end
|
data/lib/smart_prompt/version.rb
CHANGED
data/lib/smart_prompt/worker.rb
CHANGED
@@ -11,13 +11,13 @@ module SmartPrompt
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def execute(params = {})
|
14
|
-
conversation = Conversation.new(@engine)
|
14
|
+
conversation = Conversation.new(@engine, params[:tools])
|
15
15
|
context = WorkerContext.new(conversation, params, @engine)
|
16
16
|
context.instance_eval(&@code)
|
17
17
|
end
|
18
18
|
|
19
19
|
def execute_by_stream(params = {}, &proc)
|
20
|
-
conversation = Conversation.new(@engine)
|
20
|
+
conversation = Conversation.new(@engine, params[:tools])
|
21
21
|
context = WorkerContext.new(conversation, params, @engine, proc)
|
22
22
|
context.instance_eval(&@code)
|
23
23
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_prompt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhuang biaowei
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-28 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: yaml
|