smart_prompt 0.5.0 → 0.5.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.
@@ -1,12 +1,13 @@
1
1
  module SmartPrompt
2
2
  class Engine
3
- attr_reader :config_file, :config, :adapters, :current_adapter, :llms, :templates
3
+ attr_reader :config_file, :config, :adapters, :current_adapter, :llms, :models, :templates
4
4
  attr_reader :stream_response, :history_manager
5
5
 
6
6
  def initialize(config_file)
7
7
  @config_file = config_file
8
8
  @adapters = {}
9
9
  @llms = {}
10
+ @models = {}
10
11
  @templates = {}
11
12
  @current_workers = {}
12
13
  @history_messages = []
@@ -65,10 +66,7 @@ module SmartPrompt
65
66
  SmartPrompt.logger = Logger.new(@config["logger_file"])
66
67
  end
67
68
  SmartPrompt.logger.info "Loading configuration from file: #{config_file}"
68
- if @config["better_prompt_db"]
69
- require "better_prompt"
70
- BetterPrompt.setup(db_path: @config["better_prompt_db"])
71
- end
69
+ @models = @config["models"] || {}
72
70
  @config["adapters"].each do |adapter_name, adapter_class|
73
71
  adapter_class = SmartPrompt.const_get(adapter_class)
74
72
  @adapters[adapter_name] = adapter_class
@@ -134,15 +132,12 @@ module SmartPrompt
134
132
  if result.class == String
135
133
  recive_message = {
136
134
  "role": "assistant",
137
- "content": result,
135
+ "content": sanitize_history_content(result),
138
136
  }
139
137
  elsif result.class == Array
140
138
  recive_message = nil
141
139
  else
142
- recive_message = {
143
- "role": result.dig("choices", 0, "message", "role"),
144
- "content": result.dig("choices", 0, "message", "content").to_s + result.dig("choices", 0, "message", "tool_calls").to_s,
145
- }
140
+ recive_message = assistant_history_message(result)
146
141
  end
147
142
  worker.conversation.add_message(recive_message) if recive_message
148
143
  SmartPrompt.logger.info "Worker result is: #{result}"
@@ -196,6 +191,21 @@ module SmartPrompt
196
191
 
197
192
  private
198
193
 
194
+ def assistant_history_message(result)
195
+ message = result.dig("choices", 0, "message") || {}
196
+ history_message = {
197
+ "role": message["role"] || "assistant",
198
+ "content": sanitize_history_content(message["content"].to_s),
199
+ }
200
+ tool_calls = message["tool_calls"]
201
+ history_message["tool_calls"] = tool_calls if tool_calls && !tool_calls.empty?
202
+ history_message
203
+ end
204
+
205
+ def sanitize_history_content(content)
206
+ content.to_s.gsub(/<\|channel\>thought\n.*?<channel\|>/m, "")
207
+ end
208
+
199
209
  # Recursively convert hash keys from strings to symbols
200
210
  def symbolize_keys(hash)
201
211
  return hash unless hash.is_a?(Hash)
@@ -31,7 +31,19 @@ module SmartPrompt
31
31
  end
32
32
  end
33
33
 
34
- def send_request(messages, model = nil, temperature = 0.7, tools = nil, proc = nil)
34
+ REQUEST_PARAMETER_KEYS = %w[
35
+ max_tokens
36
+ max_completion_tokens
37
+ top_p
38
+ top_k
39
+ response_format
40
+ tool_choice
41
+ parallel_tool_calls
42
+ seed
43
+ stop
44
+ ].freeze
45
+
46
+ def send_request(messages, model = nil, temperature = 0.7, tools = nil, proc = nil, request_options = {})
35
47
  SmartPrompt.logger.info "OpenAIAdapter: Sending request to OpenAI"
36
48
  temperature = 0.7 if temperature == nil
37
49
  if model
@@ -46,6 +58,8 @@ module SmartPrompt
46
58
  messages: messages,
47
59
  temperature: @config["temperature"] || temperature,
48
60
  }
61
+ parameters.merge!(configured_request_parameters)
62
+ parameters.merge!(request_options || {})
49
63
  if proc
50
64
  parameters[:stream] = proc
51
65
  end
@@ -99,5 +113,15 @@ module SmartPrompt
99
113
  end
100
114
  return response.dig("data", 0, "embedding")
101
115
  end
116
+
117
+ private
118
+
119
+ def configured_request_parameters
120
+ REQUEST_PARAMETER_KEYS.each_with_object({}) do |key, parameters|
121
+ next unless @config.key?(key)
122
+
123
+ parameters[key.to_sym] = @config[key]
124
+ end
125
+ end
102
126
  end
103
127
  end
@@ -3,6 +3,10 @@ require "json"
3
3
  require "net/http"
4
4
  require "uri"
5
5
  require "fileutils"
6
+ require_relative "concerns/image_persistence"
7
+ require_relative "concerns/openai_chat_shaping"
8
+ require_relative "concerns/multimodal_messages"
9
+ require_relative "concerns/http_client"
6
10
 
7
11
  module SmartPrompt
8
12
  # Adapter for SenseNova (商汤 日日新) — the SenseCore large-model platform.
@@ -38,7 +42,6 @@ module SmartPrompt
38
42
  2048x2048 2752x1536 1536x2752 3072x1376 1344x3136 2560x720 3072x864
39
43
  ].freeze
40
44
  DEFAULT_IMAGE_SIZE = "2048x2048".freeze
41
- SUPPORTED_IMAGE_FORMATS = %w[jpg jpeg png gif bmp webp].freeze
42
45
 
43
46
  # SenseNova sampling parameters forwarded from config to the chat request when present.
44
47
  CHAT_OPTIONAL_KEYS = %w[
@@ -46,6 +49,31 @@ module SmartPrompt
46
49
  reasoning_effort max_completion_tokens max_tokens
47
50
  ].freeze
48
51
 
52
+ include ImagePersistence
53
+ include OpenAIChatShaping
54
+ include MultimodalMessages
55
+ include HTTPClient
56
+
57
+ # ---- hooks for shared concerns -------------------------------------------
58
+ def provider_label
59
+ "SenseNova"
60
+ end
61
+
62
+ def default_image_prefix
63
+ "sensenova_image"
64
+ end
65
+
66
+ # SenseNova exposes the reasoning trace under `reasoning` (not reasoning_content)
67
+ # and also returns system_fingerprint — override the OpenAIChatShaping hooks so the
68
+ # shared shaper still produces the uniform reasoning_content / fingerprint output.
69
+ def reasoning_field_name
70
+ "reasoning"
71
+ end
72
+
73
+ def extra_top_level_fields(raw)
74
+ { "system_fingerprint" => raw["system_fingerprint"] }
75
+ end
76
+
49
77
  def initialize(config)
50
78
  super
51
79
  SmartPrompt.logger.info "Start create the SmartPrompt SenseNovaAdapter."
@@ -166,16 +194,7 @@ module SmartPrompt
166
194
  images
167
195
  end
168
196
 
169
- # Save one or many generated images to disk (Array from #generate_image or a single hash).
170
- def save_image(image_data, output_dir = "./output", filename_prefix = "sensenova_image")
171
- FileUtils.mkdir_p(output_dir)
172
- images = image_data.is_a?(Array) ? image_data : [image_data]
173
- saved = images.each_with_index.map do |img, index|
174
- save_single_image(img, output_dir, "#{filename_prefix}_#{index + 1}")
175
- end
176
- SmartPrompt.logger.info "Saved #{saved.size} SenseNova image(s) to #{output_dir}"
177
- saved
178
- end
197
+ # (save_image / save_single_image provided by ImagePersistence concern.)
179
198
 
180
199
  private
181
200
 
@@ -192,171 +211,10 @@ module SmartPrompt
192
211
  body
193
212
  end
194
213
 
195
- # Pass messages through, normalizing any multimodal content. Local image paths inside
196
- # image_url.url are converted to data: URLs; http(s)/data URLs and plain text pass through.
197
- def process_multimodal_messages(messages)
198
- messages.map do |msg|
199
- role = msg[:role] || msg["role"]
200
- content = msg[:content] || msg["content"]
201
- content = content.map { |item| normalize_content_item(item) } if content.is_a?(Array)
202
- { "role" => role, "content" => content }
203
- end
204
- end
205
-
206
- def normalize_content_item(item)
207
- return { "type" => "text", "text" => item.to_s } unless item.is_a?(Hash)
208
-
209
- type = item[:type] || item["type"]
210
- if type == "image_url"
211
- iu = item[:image_url] || item["image_url"]
212
- url = iu.is_a?(Hash) ? (iu[:url] || iu["url"]) : iu
213
- { "type" => "image_url", "image_url" => { "url" => normalize_image_url(url) } }
214
- else
215
- stringify_hash(item)
216
- end
217
- end
218
-
219
- def normalize_image_url(url)
220
- return url if url.nil?
221
- return url if url.start_with?("http://", "https://", "data:")
214
+ # (process_multimodal_messages / normalize_* / stringify_hash provided by MultimodalMessages concern.)
215
+ # (build_completion_response / build_stream_chunk provided by OpenAIChatShaping concern.)
222
216
 
223
- raise Error, "Image file not found: #{url}" unless File.exist?(url)
224
- ext = File.extname(url).downcase.delete(".")
225
- raise Error, "Unsupported image format: #{ext}" unless SUPPORTED_IMAGE_FORMATS.include?(ext)
226
- mime = ext == "jpg" ? "jpeg" : ext
227
- "data:image/#{mime};base64,#{Base64.strict_encode64(File.binread(url))}"
228
- end
229
-
230
- # ---- response shaping -----------------------------------------------------
231
-
232
- # Convert a non-streaming SenseNova response into the OpenAI completion shape the
233
- # rest of SmartPrompt expects, surfacing the reasoning model's `reasoning` field.
234
- def build_completion_response(raw)
235
- msg = raw.dig("choices", 0, "message") || {}
236
- message = { "role" => msg["role"] || "assistant" }
237
- message["content"] = msg["content"]
238
- message["reasoning_content"] = msg["reasoning"] if msg["reasoning"]
239
- message["tool_calls"] = msg["tool_calls"] if msg["tool_calls"]
240
-
241
- response = {
242
- "id" => raw["id"],
243
- "object" => raw["object"] || "chat.completion",
244
- "created" => raw["created"],
245
- "model" => raw["model"],
246
- "choices" => [{
247
- "index" => 0,
248
- "message" => message,
249
- "finish_reason" => raw.dig("choices", 0, "finish_reason"),
250
- }],
251
- }
252
- response["usage"] = raw["usage"] if raw["usage"]
253
- response["system_fingerprint"] = raw["system_fingerprint"] if raw["system_fingerprint"]
254
- response
255
- end
256
-
257
- # Convert one SSE event from SenseNova's stream into an OpenAI-style streaming chunk.
258
- # The key remap is delta.reasoning -> delta.reasoning_content, which is what
259
- # Engine#@stream_proc reads for reasoning models.
260
- def build_stream_chunk(data)
261
- chunk = {
262
- "id" => data["id"],
263
- "object" => data["object"],
264
- "created" => data["created"],
265
- "model" => data["model"],
266
- }
267
- chunk["usage"] = data["usage"] if data["usage"]
268
- chunk["system_fingerprint"] = data["system_fingerprint"] if data["system_fingerprint"]
269
-
270
- choices = data["choices"] || []
271
- if choices.any?
272
- delta = choices[0]["delta"] || {}
273
- new_delta = {}
274
- new_delta["role"] = delta["role"] if delta["role"]
275
- new_delta["content"] = delta["content"] if delta["content"]
276
- new_delta["reasoning_content"] = delta["reasoning"] if delta["reasoning"]
277
- new_delta["tool_calls"] = delta["tool_calls"] if delta["tool_calls"]
278
- chunk["choices"] = [{
279
- "index" => choices[0]["index"] || 0,
280
- "delta" => new_delta,
281
- "finish_reason" => choices[0]["finish_reason"],
282
- }]
283
- else
284
- # Usage-only final event (choices is an empty array).
285
- chunk["choices"] = []
286
- end
287
- chunk
288
- end
289
-
290
- # ---- HTTP -----------------------------------------------------------------
291
-
292
- def http_post_json(url, body)
293
- uri = URI.parse(url)
294
- http = Net::HTTP.new(uri.host, uri.port)
295
- http.use_ssl = (uri.scheme == "https")
296
- http.open_timeout = 30
297
- http.read_timeout = 240
298
-
299
- request = Net::HTTP::Post.new(uri.request_uri)
300
- request["Content-Type"] = "application/json"
301
- request["Authorization"] = "Bearer #{@api_key}"
302
- request.body = body.to_json
303
-
304
- SmartPrompt.logger.debug "SenseNova POST #{uri} body=#{body.to_json}"
305
- response = http.request(request)
306
-
307
- if response.is_a?(Net::HTTPSuccess)
308
- JSON.parse(response.body)
309
- else
310
- SmartPrompt.logger.error "SenseNova API error: #{response.code} - #{response.body}"
311
- raise LLMAPIError, "SenseNova API error: #{response.code} - #{response.body}"
312
- end
313
- end
314
-
315
- # POST with stream:true and yield each parsed SSE `data:` payload to the block.
316
- def stream_chat(url, body)
317
- uri = URI.parse(url)
318
- http = Net::HTTP.new(uri.host, uri.port)
319
- http.use_ssl = (uri.scheme == "https")
320
- http.open_timeout = 30
321
- http.read_timeout = 300
322
-
323
- request = Net::HTTP::Post.new(uri.request_uri)
324
- request["Content-Type"] = "application/json"
325
- request["Authorization"] = "Bearer #{@api_key}"
326
- request["Accept"] = "text/event-stream"
327
- request.body = body.to_json
328
-
329
- buffer = ""
330
- done = false
331
-
332
- http.request(request) do |response|
333
- unless response.is_a?(Net::HTTPSuccess)
334
- raise LLMAPIError, "SenseNova stream error: #{response.code} - #{response.body}"
335
- end
336
-
337
- response.read_body do |segment|
338
- break if done
339
- buffer << segment
340
- while (idx = buffer.index("\n"))
341
- line = buffer.slice!(0, idx + 1).strip
342
- next if line.empty? || !line.start_with?("data:")
343
-
344
- payload = line.sub(/\Adata:\s*/, "")
345
- if payload == "[DONE]"
346
- done = true
347
- break
348
- end
349
-
350
- begin
351
- data = JSON.parse(payload)
352
- rescue JSON::ParserError
353
- next
354
- end
355
- yield data
356
- end
357
- end
358
- end
359
- end
217
+ # (http_post_json / stream_chat provided by HTTPClient concern.)
360
218
 
361
219
  # Resolve the image size: default to 2048x2048 when none given, and warn (but still
362
220
  # send) when the caller asks for a size sensenova-u1-fast does not accept.
@@ -370,41 +228,6 @@ module SmartPrompt
370
228
  size
371
229
  end
372
230
 
373
- def save_single_image(image_data, output_dir, filename)
374
- if image_data[:b64_json]
375
- file_path = File.join(output_dir, "#{filename}.png")
376
- File.binwrite(file_path, Base64.decode64(image_data[:b64_json]))
377
- elsif image_data[:url]
378
- uri = URI.parse(image_data[:url])
379
- response = Net::HTTP.get_response(uri)
380
- raise Error, "Failed to download image from URL: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
381
-
382
- ext = case response["content-type"]
383
- when "image/jpeg", "image/jpg" then "jpg"
384
- when "image/png" then "png"
385
- when "image/gif" then "gif"
386
- when "image/webp" then "webp"
387
- else "png"
388
- end
389
- file_path = File.join(output_dir, "#{filename}.#{ext}")
390
- File.binwrite(file_path, response.body)
391
- else
392
- raise Error, "No image data available to save"
393
- end
394
- file_path
395
- end
396
-
397
- def stringify_hash(hash)
398
- case hash
399
- when Hash
400
- hash.each_with_object({}) do |(k, v), memo|
401
- memo[k.to_s] = stringify_hash(v)
402
- end
403
- when Array
404
- hash.map { |v| stringify_hash(v) }
405
- else
406
- hash
407
- end
408
- end
231
+ # (stringify_hash provided by MultimodalMessages concern.)
409
232
  end
410
233
  end
@@ -1,3 +1,3 @@
1
1
  module SmartPrompt
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -11,8 +11,11 @@ module SmartPrompt
11
11
  end
12
12
 
13
13
  def execute(params = {})
14
- # Generate default session ID if using history and no session_id provided
15
- session_id = params[:session_id] || "default"
14
+ # Generate default session ID if using history and no session_id provided.
15
+ # (Do NOT default to a literal "default" here — that would make every
16
+ # history-using worker share one session and leave the worker-name branch
17
+ # below as dead code, breaking per-worker session isolation.)
18
+ session_id = params[:session_id]
16
19
  if params[:with_history] && !session_id && @engine.history_manager
17
20
  session_id = "worker_#{@name}_#{Time.now.to_i}"
18
21
  SmartPrompt.logger.info "Generated default session ID: #{session_id}"