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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -10
- data/README.cn.md +307 -64
- data/README.md +311 -64
- data/Rakefile +10 -1
- data/config/anthropic_config.yml +151 -0
- data/config/image_generation_config.yml +22 -0
- data/config/multimodal_config.yml +85 -0
- data/config/sensenova_config.yml +63 -0
- data/config/zhipu_config.yml +73 -0
- data/examples/anthropic_basic_chat.rb +143 -0
- data/examples/anthropic_example.rb +232 -0
- data/examples/anthropic_multimodal.rb +212 -0
- data/examples/anthropic_streaming.rb +312 -0
- data/examples/anthropic_tool_calling.rb +393 -0
- data/examples/automatic_cleanup_example.rb +109 -0
- data/examples/history_management_examples.rb +522 -0
- data/examples/image_generation_example.rb +130 -0
- data/examples/monitoring_example.rb +121 -0
- data/examples/multimodal_example.rb +63 -0
- data/examples/relevance_based_strategy_example.rb +87 -0
- data/examples/sensenova_example.rb +129 -0
- data/examples/stt_example.rb +287 -0
- data/examples/tts_example.rb +244 -0
- data/examples/video_generation_example.rb +189 -0
- data/examples/zhipu_example.rb +151 -0
- data/lib/smart_prompt/anthropic_adapter.rb +363 -281
- data/lib/smart_prompt/compression_engine.rb +201 -0
- data/lib/smart_prompt/context_strategy.rb +22 -0
- data/lib/smart_prompt/conversation.rb +81 -191
- data/lib/smart_prompt/engine.rb +36 -19
- data/lib/smart_prompt/history_manager.rb +596 -0
- data/lib/smart_prompt/hybrid_strategy.rb +222 -0
- data/lib/smart_prompt/image_generation_adapter.rb +297 -0
- data/lib/smart_prompt/lru_cache.rb +133 -0
- data/lib/smart_prompt/message.rb +57 -0
- data/lib/smart_prompt/multimodal_adapter.rb +277 -0
- data/lib/smart_prompt/openai_adapter.rb +1 -25
- data/lib/smart_prompt/persistence_layer.rb +197 -0
- data/lib/smart_prompt/relevance_based_strategy.rb +221 -0
- data/lib/smart_prompt/sensenova_adapter.rb +410 -0
- data/lib/smart_prompt/session.rb +140 -0
- data/lib/smart_prompt/sliding_window_strategy.rb +100 -0
- data/lib/smart_prompt/stt_adapter.rb +381 -0
- data/lib/smart_prompt/summary_based_strategy.rb +152 -0
- data/lib/smart_prompt/token_counter.rb +74 -0
- data/lib/smart_prompt/tts_adapter.rb +403 -0
- data/lib/smart_prompt/version.rb +1 -1
- data/lib/smart_prompt/video_generation_adapter.rb +330 -0
- data/lib/smart_prompt/worker.rb +25 -3
- data/lib/smart_prompt/zhipu_adapter.rb +616 -0
- data/lib/smart_prompt.rb +22 -2
- data/workers/history_management_examples.rb +407 -0
- data/workers/image_generation_workers.rb +119 -0
- data/workers/multimodal_workers.rb +110 -0
- data/workers/sensenova_workers.rb +62 -0
- data/workers/stt_workers.rb +195 -0
- data/workers/tts_workers.rb +388 -0
- data/workers/video_generation_workers.rb +264 -0
- data/workers/zhipu_workers.rb +113 -0
- metadata +84 -8
data/lib/smart_prompt/engine.rb
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
module SmartPrompt
|
|
2
2
|
class Engine
|
|
3
|
-
attr_reader :config_file, :config, :adapters, :current_adapter, :llms, :
|
|
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
|
-
|
|
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":
|
|
137
|
+
"content": result,
|
|
127
138
|
}
|
|
128
139
|
elsif result.class == Array
|
|
129
140
|
recive_message = nil
|
|
130
141
|
else
|
|
131
|
-
recive_message =
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
190
|
-
|
|
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
|