Class: SmartPrompt::Conversation
- Inherits:
-
Object
- Object
- SmartPrompt::Conversation
show all
- Includes:
- APIHandler
- Defined in:
- lib/smart_prompt/conversation.rb
Constant Summary
collapse
- MODEL_REQUEST_OPTION_KEYS =
%w[
max_tokens
max_completion_tokens
top_p
top_k
response_format
tool_choice
parallel_tool_calls
seed
stop
].freeze
Constants included
from APIHandler
APIHandler::MAX_RETRIES, APIHandler::RETRY_OPTIONS
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#add_message(msg, with_history = false) ⇒ Object
-
#audio(source, **metadata) ⇒ Object
-
#edit_image(prompt, params = {}) ⇒ Object
-
#embeddings(length) ⇒ Object
-
#generate_image(prompt, params = {}) ⇒ Object
-
#history_messages ⇒ Object
-
#image(source, token_budget: nil, **metadata) ⇒ Object
-
#initialize(engine, tools = nil, session_id = nil) ⇒ Conversation
constructor
A new instance of Conversation.
-
#model(model_name) ⇒ Object
-
#multimodal_prompt(parts, with_history: false) ⇒ Object
-
#normalize(x, length) ⇒ Object
-
#prompt(template_name, params = {}, with_history: false) ⇒ Object
-
#request_options(options = {}) ⇒ Object
-
#save_image(image_data, output_dir = "./output", filename_prefix = "generated_image") ⇒ Object
-
#send_msg(params = {}) ⇒ Object
-
#send_msg_by_stream(params = {}, &proc) ⇒ Object
-
#send_msg_once ⇒ Object
-
#sys_msg(message, params = {}) ⇒ Object
-
#temperature(temperature) ⇒ Object
-
#thinking(enabled = true) ⇒ Object
-
#use(llm_name) ⇒ Object
-
#use_model(model_name) ⇒ Object
-
#video(source, fps: nil, max_seconds: nil, **metadata) ⇒ Object
Constructor Details
#initialize(engine, tools = nil, session_id = nil) ⇒ Conversation
Returns a new instance of Conversation.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/smart_prompt/conversation.rb', line 25
def initialize(engine, tools = nil, session_id = nil)
SmartPrompt.logger.info "Create Conversation"
@messages = []
@engine = engine
@adapters = engine.adapters
@llms = engine.llms
@models = engine.models
@current_llm_name = nil
@templates = engine.templates
@temperature = 0.7
@current_adapter = engine.current_adapter
@last_response = nil
@tools = tools
@request_options = {}
@pending_content_parts = []
@thinking_enabled = nil
@session_id = session_id
@use_history_manager = false
end
|
Instance Attribute Details
#config_file ⇒ Object
Returns the value of attribute config_file.
21
22
23
|
# File 'lib/smart_prompt/conversation.rb', line 21
def config_file
@config_file
end
|
#last_call_id ⇒ Object
Returns the value of attribute last_call_id.
22
23
24
|
# File 'lib/smart_prompt/conversation.rb', line 22
def last_call_id
@last_call_id
end
|
#last_response ⇒ Object
Returns the value of attribute last_response.
21
22
23
|
# File 'lib/smart_prompt/conversation.rb', line 21
def last_response
@last_response
end
|
#messages ⇒ Object
Returns the value of attribute messages.
21
22
23
|
# File 'lib/smart_prompt/conversation.rb', line 21
def messages
@messages
end
|
#session_id ⇒ Object
Returns the value of attribute session_id.
23
24
25
|
# File 'lib/smart_prompt/conversation.rb', line 23
def session_id
@session_id
end
|
Instance Method Details
#add_message(msg, with_history = false) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/smart_prompt/conversation.rb', line 103
def add_message(msg, with_history = false)
if with_history
if @engine.history_manager
@use_history_manager = true
@session_id ||= generate_default_session_id
@engine.history_manager.add_message(@session_id, msg)
else
@engine.history_messages << msg
end
end
@messages << msg
end
|
#audio(source, **metadata) ⇒ Object
149
150
151
152
|
# File 'lib/smart_prompt/conversation.rb', line 149
def audio(source, **metadata)
@pending_content_parts << media_part("audio", source, **metadata)
self
end
|
#edit_image(prompt, params = {}) ⇒ Object
357
358
359
|
# File 'lib/smart_prompt/conversation.rb', line 357
def edit_image(prompt, params = {})
@current_llm.edit_image(prompt, params)
end
|
#embeddings(length) ⇒ Object
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/smart_prompt/conversation.rb', line 220
def embeddings(length)
Retriable.retriable(RETRY_OPTIONS) do
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
text = ""
@messages.each do |msg|
if msg[:role] == "user"
text = msg[:content]
end
end
@last_response = @current_llm.embeddings(text, @model_name)
@messages = []
@messages << { role: "system", content: @sys_msg }
normalize(@last_response, length)
end
end
|
#generate_image(prompt, params = {}) ⇒ Object
353
354
355
|
# File 'lib/smart_prompt/conversation.rb', line 353
def generate_image(prompt, params = {})
@current_llm.generate_image(prompt, params)
end
|
#history_messages ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/smart_prompt/conversation.rb', line 91
def history_messages
if @use_history_manager && @engine.history_manager
session_messages = @engine.history_manager.get_context(@session_id)
session_messages.map(&:to_h)
else
@engine.history_messages
end
end
|
#image(source, token_budget: nil, **metadata) ⇒ Object
144
145
146
147
|
# File 'lib/smart_prompt/conversation.rb', line 144
def image(source, token_budget: nil, **metadata)
@pending_content_parts << media_part("image", source, token_budget: token_budget, **metadata)
self
end
|
#model(model_name) ⇒ Object
69
70
71
|
# File 'lib/smart_prompt/conversation.rb', line 69
def model(model_name)
@model_name = model_name
end
|
#multimodal_prompt(parts, with_history: false) ⇒ Object
139
140
141
142
|
# File 'lib/smart_prompt/conversation.rb', line 139
def multimodal_prompt(parts, with_history: false)
add_message({ role: "user", content: normalize_content_parts(parts) }, with_history)
self
end
|
#normalize(x, length) ⇒ Object
210
211
212
213
214
215
216
217
218
|
# File 'lib/smart_prompt/conversation.rb', line 210
def normalize(x, length)
if x.length > length
x = Numo::NArray.cast(x[0..length - 1])
norm = Math.sqrt((x * x).sum)
return (x / norm).to_a
else
return x.concat([0] * (x.length - length))
end
end
|
#prompt(template_name, params = {}, with_history: false) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/smart_prompt/conversation.rb', line 119
def prompt(template_name, params = {}, with_history: false)
if template_name.class == Symbol
template_name = template_name.to_s
SmartPrompt.logger.info "Use template #{template_name}"
raise "Template #{template_name} not found" unless @templates.key?(template_name)
content = @templates[template_name].render(params)
add_user_content(content, with_history)
self
else
add_user_content(template_name, with_history)
self
end
end
|
#request_options(options = {}) ⇒ Object
77
78
79
80
|
# File 'lib/smart_prompt/conversation.rb', line 77
def request_options(options = {})
@request_options.merge!(options || {})
self
end
|
#save_image(image_data, output_dir = "./output", filename_prefix = "generated_image") ⇒ Object
361
362
363
|
# File 'lib/smart_prompt/conversation.rb', line 361
def save_image(image_data, output_dir = "./output", filename_prefix = "generated_image")
@current_llm.save_image(image_data, output_dir, filename_prefix)
end
|
#send_msg(params = {}) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/smart_prompt/conversation.rb', line 176
def send_msg(params = {})
Retriable.retriable(RETRY_OPTIONS) do
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
if params[:with_history]
@last_response = send_llm_request(history_messages, nil)
else
@last_response = send_llm_request(@messages, nil)
end
if @last_response == ""
@last_response = @current_llm.last_response
end
@messages = []
@messages << { role: "system", content: @sys_msg }
@last_response
end
rescue => e
return "Failed to call LLM after #{MAX_RETRIES} attempts: #{e.message}"
end
|
#send_msg_by_stream(params = {}, &proc) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/smart_prompt/conversation.rb', line 195
def send_msg_by_stream(params = {}, &proc)
Retriable.retriable(RETRY_OPTIONS) do
raise ConfigurationError, "No LLM selected" if @current_llm.nil?
if params[:with_history]
send_llm_request(history_messages, proc)
else
send_llm_request(@messages, proc)
end
@messages = []
@messages << { role: "system", content: @sys_msg }
end
rescue => e
return "Failed to call LLM after #{MAX_RETRIES} attempts: #{e.message}"
end
|
#send_msg_once ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/smart_prompt/conversation.rb', line 159
def send_msg_once
raise "No LLM selected" if @current_llm.nil?
@last_response = send_llm_request(@messages, nil)
@messages = []
@messages << { role: "system", content: @sys_msg }
@last_response
end
|
#sys_msg(message, params = {}) ⇒ Object
133
134
135
136
137
|
# File 'lib/smart_prompt/conversation.rb', line 133
def sys_msg(message, params = {})
@sys_msg = thinking_system_message(message)
add_message({ role: "system", content: @sys_msg }, params[:with_history])
self
end
|
#temperature(temperature) ⇒ Object
73
74
75
|
# File 'lib/smart_prompt/conversation.rb', line 73
def temperature(temperature)
@temperature = temperature
end
|
#thinking(enabled = true) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/smart_prompt/conversation.rb', line 82
def thinking(enabled = true)
@thinking_enabled = enabled
if @sys_msg
@sys_msg = thinking_system_message(@sys_msg)
refresh_system_message(@sys_msg)
end
self
end
|
#use(llm_name) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/smart_prompt/conversation.rb', line 45
def use(llm_name)
llm_name = llm_name.to_s
raise ConfigurationError, "LLM #{llm_name} not configured" unless @llms.key?(llm_name)
@current_llm = @llms[llm_name]
@current_llm_name = llm_name
self
end
|
#use_model(model_name) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/smart_prompt/conversation.rb', line 53
def use_model(model_name)
model_name = model_name.to_s
model_config = @models[model_name] || @models[model_name.to_sym]
raise ConfigurationError, "Model #{model_name} not configured" unless model_config
llm_name = model_config["use"] || model_config[:use]
configured_model_name = model_config["model"] || model_config[:model]
raise ConfigurationError, "Model #{model_name} must define use" if llm_name.nil? || llm_name.empty?
raise ConfigurationError, "Model #{model_name} must define model" if configured_model_name.nil? || configured_model_name.empty?
use(llm_name)
model(configured_model_name)
merge_model_request_options(model_config)
self
end
|
#video(source, fps: nil, max_seconds: nil, **metadata) ⇒ Object
154
155
156
157
|
# File 'lib/smart_prompt/conversation.rb', line 154
def video(source, fps: nil, max_seconds: nil, **metadata)
@pending_content_parts << media_part("video", source, fps: fps, max_seconds: max_seconds, **metadata)
self
end
|