smart_prompt 0.4.4 → 0.5.0

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -10
  3. data/README.cn.md +307 -64
  4. data/README.md +311 -64
  5. data/Rakefile +10 -1
  6. data/config/anthropic_config.yml +151 -0
  7. data/config/image_generation_config.yml +22 -0
  8. data/config/multimodal_config.yml +85 -0
  9. data/config/sensenova_config.yml +63 -0
  10. data/config/zhipu_config.yml +73 -0
  11. data/examples/anthropic_basic_chat.rb +143 -0
  12. data/examples/anthropic_example.rb +232 -0
  13. data/examples/anthropic_multimodal.rb +212 -0
  14. data/examples/anthropic_streaming.rb +312 -0
  15. data/examples/anthropic_tool_calling.rb +393 -0
  16. data/examples/automatic_cleanup_example.rb +109 -0
  17. data/examples/history_management_examples.rb +522 -0
  18. data/examples/image_generation_example.rb +130 -0
  19. data/examples/monitoring_example.rb +121 -0
  20. data/examples/multimodal_example.rb +63 -0
  21. data/examples/relevance_based_strategy_example.rb +87 -0
  22. data/examples/sensenova_example.rb +129 -0
  23. data/examples/stt_example.rb +287 -0
  24. data/examples/tts_example.rb +244 -0
  25. data/examples/video_generation_example.rb +189 -0
  26. data/examples/zhipu_example.rb +151 -0
  27. data/lib/smart_prompt/anthropic_adapter.rb +363 -281
  28. data/lib/smart_prompt/compression_engine.rb +201 -0
  29. data/lib/smart_prompt/context_strategy.rb +22 -0
  30. data/lib/smart_prompt/conversation.rb +81 -191
  31. data/lib/smart_prompt/engine.rb +36 -19
  32. data/lib/smart_prompt/history_manager.rb +596 -0
  33. data/lib/smart_prompt/hybrid_strategy.rb +222 -0
  34. data/lib/smart_prompt/image_generation_adapter.rb +297 -0
  35. data/lib/smart_prompt/lru_cache.rb +133 -0
  36. data/lib/smart_prompt/message.rb +57 -0
  37. data/lib/smart_prompt/multimodal_adapter.rb +277 -0
  38. data/lib/smart_prompt/openai_adapter.rb +1 -25
  39. data/lib/smart_prompt/persistence_layer.rb +197 -0
  40. data/lib/smart_prompt/relevance_based_strategy.rb +221 -0
  41. data/lib/smart_prompt/sensenova_adapter.rb +410 -0
  42. data/lib/smart_prompt/session.rb +140 -0
  43. data/lib/smart_prompt/sliding_window_strategy.rb +100 -0
  44. data/lib/smart_prompt/stt_adapter.rb +381 -0
  45. data/lib/smart_prompt/summary_based_strategy.rb +152 -0
  46. data/lib/smart_prompt/token_counter.rb +74 -0
  47. data/lib/smart_prompt/tts_adapter.rb +403 -0
  48. data/lib/smart_prompt/version.rb +1 -1
  49. data/lib/smart_prompt/video_generation_adapter.rb +330 -0
  50. data/lib/smart_prompt/worker.rb +25 -3
  51. data/lib/smart_prompt/zhipu_adapter.rb +616 -0
  52. data/lib/smart_prompt.rb +22 -2
  53. data/workers/history_management_examples.rb +407 -0
  54. data/workers/image_generation_workers.rb +119 -0
  55. data/workers/multimodal_workers.rb +110 -0
  56. data/workers/sensenova_workers.rb +62 -0
  57. data/workers/stt_workers.rb +195 -0
  58. data/workers/tts_workers.rb +388 -0
  59. data/workers/video_generation_workers.rb +264 -0
  60. data/workers/zhipu_workers.rb +113 -0
  61. metadata +84 -8
@@ -1,16 +1,16 @@
1
1
  module SmartPrompt
2
2
  class Engine
3
- attr_reader :config_file, :config, :adapters, :current_adapter, :llms, :models, :templates
4
- attr_reader :stream_response
3
+ attr_reader :config_file, :config, :adapters, :current_adapter, :llms, :templates
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 = {}
11
10
  @templates = {}
12
11
  @current_workers = {}
13
12
  @history_messages = []
13
+ @history_manager = nil
14
14
  load_config(config_file)
15
15
  SmartPrompt.logger.info "Started create the SmartPrompt engine."
16
16
  @stream_proc = Proc.new do |chunk, _bytesize|
@@ -65,7 +65,10 @@ module SmartPrompt
65
65
  SmartPrompt.logger = Logger.new(@config["logger_file"])
66
66
  end
67
67
  SmartPrompt.logger.info "Loading configuration from file: #{config_file}"
68
- @models = @config["models"] || {}
68
+ if @config["better_prompt_db"]
69
+ require "better_prompt"
70
+ BetterPrompt.setup(db_path: @config["better_prompt_db"])
71
+ end
69
72
  @config["adapters"].each do |adapter_name, adapter_class|
70
73
  adapter_class = SmartPrompt.const_get(adapter_class)
71
74
  @adapters[adapter_name] = adapter_class
@@ -79,6 +82,14 @@ module SmartPrompt
79
82
  template_name = file.gsub(@config["template_path"] + "/", "").gsub("\.erb", "")
80
83
  @templates[template_name] = PromptTemplate.new(file)
81
84
  end
85
+
86
+ # Initialize HistoryManager if configured
87
+ if @config["history"]
88
+ history_config = symbolize_keys(@config["history"])
89
+ @history_manager = HistoryManager.new(history_config)
90
+ SmartPrompt.logger.info "HistoryManager initialized with configuration"
91
+ end
92
+
82
93
  load_workers
83
94
  rescue Psych::SyntaxError => ex
84
95
  SmartPrompt.logger.error "YAML syntax error in config file: #{ex.message}"
@@ -123,12 +134,15 @@ module SmartPrompt
123
134
  if result.class == String
124
135
  recive_message = {
125
136
  "role": "assistant",
126
- "content": sanitize_history_content(result),
137
+ "content": result,
127
138
  }
128
139
  elsif result.class == Array
129
140
  recive_message = nil
130
141
  else
131
- recive_message = assistant_history_message(result)
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
+ }
132
146
  end
133
147
  worker.conversation.add_message(recive_message) if recive_message
134
148
  SmartPrompt.logger.info "Worker result is: #{result}"
@@ -146,7 +160,8 @@ module SmartPrompt
146
160
  begin
147
161
  @origin_proc = proc
148
162
  @stream_response = {}
149
- worker.execute_by_stream(params, &@stream_proc)
163
+ ret = worker.execute_by_stream(params, &@stream_proc)
164
+ @stream_response = ret if @stream_response.empty?
150
165
  SmartPrompt.logger.info "Worker #{worker_name} executed(stream) successfully"
151
166
  SmartPrompt.logger.info "Worker #{worker_name} stream response is: #{@stream_response}"
152
167
  rescue => e
@@ -166,28 +181,30 @@ module SmartPrompt
166
181
  end
167
182
 
168
183
  def history_messages
184
+ if @history_manager
185
+ SmartPrompt.logger.warn "[DEPRECATED] Engine#history_messages is deprecated. Use history_manager.get_context(session_id) instead."
186
+ end
169
187
  @history_messages
170
188
  end
171
189
 
172
190
  def clear_history_messages
191
+ if @history_manager
192
+ SmartPrompt.logger.warn "[DEPRECATED] Engine#clear_history_messages is deprecated. Use history_manager.clear_session(session_id) instead."
193
+ end
173
194
  @history_messages = []
174
195
  end
175
196
 
176
197
  private
177
198
 
178
- def assistant_history_message(result)
179
- message = result.dig("choices", 0, "message") || {}
180
- history_message = {
181
- "role": message["role"] || "assistant",
182
- "content": sanitize_history_content(message["content"].to_s),
183
- }
184
- tool_calls = message["tool_calls"]
185
- history_message["tool_calls"] = tool_calls if tool_calls && !tool_calls.empty?
186
- history_message
187
- end
199
+ # Recursively convert hash keys from strings to symbols
200
+ def symbolize_keys(hash)
201
+ return hash unless hash.is_a?(Hash)
188
202
 
189
- def sanitize_history_content(content)
190
- content.to_s.gsub(/<\|channel\>thought\n.*?<channel\|>/m, "")
203
+ hash.each_with_object({}) do |(key, value), result|
204
+ new_key = key.is_a?(String) ? key.to_sym : key
205
+ new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
206
+ result[new_key] = new_value
207
+ end
191
208
  end
192
209
  end
193
210
  end