smart_prompt 0.5.1 → 0.5.3

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.
@@ -0,0 +1,167 @@
1
+ # 硅基流动 (SiliconFlow / SiliconCloud) workers for SmartPrompt
2
+ #
3
+ # One worker per model category, reusing the standard DSL (`use`, `model`, `sys_msg`,
4
+ # `prompt`, `send_msg`) and the media helpers. Chat/vision/embed/image/image-edit go
5
+ # through Conversation-delegated methods; rerank/video/tts/asr reach the adapter
6
+ # directly via engine.llms[...] (the methods Conversation does not delegate).
7
+ #
8
+ # `send_msg` transparently becomes streaming when the engine invokes the worker via
9
+ # call_worker_by_stream — so :siliconflow_chat serves both sync and stream callers.
10
+
11
+ # 1. 文本对话 (sync + stream)
12
+ SmartPrompt.define_worker :siliconflow_chat do
13
+ use "sf_chat"
14
+ model params[:model] if params[:model]
15
+ sys_msg(params[:system] || "你是一个有帮助的中文助手,回答简洁准确。", params)
16
+ prompt(params[:prompt] || "你好,请用一句话介绍硅基流动 SiliconFlow。")
17
+ send_msg
18
+ end
19
+
20
+ # 2. 多模态对话 (vision / video / audio). Accepts image_url/video_url/audio_url,
21
+ # a single media url, or arrays (image_urls).
22
+ SmartPrompt.define_worker :siliconflow_vision do
23
+ use "sf_vision"
24
+ model params[:model] if params[:model]
25
+ sys_msg("你是一个专业的多模态分析助手,能够准确描述和分析图像/视频/音频内容。", params)
26
+
27
+ content = [{ type: "text", text: params[:question] || "请描述这张图片中的内容。" }]
28
+ ([params[:image_url]] + (params[:image_urls] || [])).compact.uniq.each do |url|
29
+ content << { type: "image_url", image_url: { url: url } }
30
+ end
31
+ content << { type: "video_url", video_url: { url: params[:video_url] } } if params[:video_url]
32
+ content << { type: "audio_url", audio_url: { url: params[:audio_url] } } if params[:audio_url]
33
+ add_message({ role: "user", content: content })
34
+
35
+ send_msg
36
+ end
37
+
38
+ # 3. 向量模型 (embeddings). Returns a normalized numeric vector of the user text.
39
+ SmartPrompt.define_worker :siliconflow_embed do
40
+ use "sf_embed"
41
+ model params[:model] if params[:model]
42
+ prompt(params[:text] || "硅基流动 SiliconFlow 大模型")
43
+ embeddings(params[:length] || 1024)
44
+ end
45
+
46
+ # 4. 重排 (rerank). Reorders params[:documents] by relevance to params[:query].
47
+ # Conversation does not delegate rerank, so we reach the adapter directly.
48
+ SmartPrompt.define_worker :siliconflow_rerank do
49
+ use "sf_rerank"
50
+ model params[:model] if params[:model]
51
+ adapter = engine.llms["sf_rerank"]
52
+
53
+ adapter.rerank(
54
+ params[:query],
55
+ params[:documents] || [],
56
+ model: params[:model],
57
+ top_n: params[:top_n],
58
+ return_documents: params[:return_documents],
59
+ )
60
+ end
61
+
62
+ # 5. 文生图 (text-to-image). Returns the generated image(s); optionally saves to disk.
63
+ SmartPrompt.define_worker :siliconflow_image do
64
+ use "sf_image"
65
+ model params[:model] if params[:model]
66
+
67
+ images = generate_image(params[:prompt], {
68
+ model: params[:model],
69
+ negative_prompt: params[:negative_prompt],
70
+ image_size: params[:image_size] || params[:size],
71
+ batch_size: params[:batch_size] || params[:n],
72
+ seed: params[:seed],
73
+ num_inference_steps: params[:num_inference_steps],
74
+ guidance_scale: params[:guidance_scale],
75
+ })
76
+
77
+ if params[:save_to_file]
78
+ saved = save_image(images, params[:output_dir] || "./generated_images", params[:filename_prefix] || "siliconflow")
79
+ { images: images, saved_files: saved }
80
+ else
81
+ images
82
+ end
83
+ end
84
+
85
+ # 6. 图像编辑 / 图生图 (Qwen-Image-Edit). Accepts image (and image2/image3 for
86
+ # multi-image fusion) as local path, data URL, or http URL.
87
+ SmartPrompt.define_worker :siliconflow_image_edit do
88
+ use "sf_image"
89
+ model params[:model] || "Qwen/Qwen-Image-Edit-2509"
90
+
91
+ images = edit_image(params[:prompt], {
92
+ model: params[:model] || "Qwen/Qwen-Image-Edit-2509",
93
+ image: params[:image] || params[:image_file],
94
+ image2: params[:image2],
95
+ image3: params[:image3],
96
+ negative_prompt: params[:negative_prompt],
97
+ seed: params[:seed],
98
+ guidance_scale: params[:guidance_scale],
99
+ })
100
+
101
+ if params[:save_to_file]
102
+ saved = save_image(images, params[:output_dir] || "./edited_images", params[:filename_prefix] || "siliconflow_edit")
103
+ { images: images, saved_files: saved }
104
+ else
105
+ images
106
+ end
107
+ end
108
+
109
+ # 7. 文生视频 / 图生视频 (async: submit -> poll -> download).
110
+ SmartPrompt.define_worker :siliconflow_video do
111
+ use "sf_video"
112
+ model params[:model] if params[:model]
113
+ adapter = engine.llms["sf_video"]
114
+
115
+ submitted = adapter.generate_video(params[:prompt], params)
116
+ result = { submitted: submitted }
117
+
118
+ if params[:wait_for_completion]
119
+ completed = adapter.wait_for_video_completion(
120
+ submitted[:request_id],
121
+ check_interval: params[:check_interval] || 10,
122
+ timeout: params[:timeout] || 600
123
+ )
124
+ if completed[:video_url] && params[:download_to_file]
125
+ output_dir = params[:output_dir] || "./generated_videos"
126
+ prefix = params[:filename_prefix] || "siliconflow_video"
127
+ output_path = File.join(output_dir, "#{prefix}_#{submitted[:request_id]}.mp4")
128
+ downloaded = adapter.download_video(completed[:video_url], output_path)
129
+ result = { submitted: submitted, video: completed, downloaded_file: downloaded }
130
+ else
131
+ result = { submitted: submitted, video: completed }
132
+ end
133
+ end
134
+ result
135
+ end
136
+
137
+ # 8. 语音合成 (TTS — CosyVoice2 / MOSS-TTSD). Saves the synthesized audio to disk.
138
+ SmartPrompt.define_worker :siliconflow_tts do
139
+ use "sf_tts"
140
+ model params[:model] if params[:model]
141
+ adapter = engine.llms["sf_tts"]
142
+
143
+ output_path = params[:output_path] || "./generated_audio/siliconflow_tts.mp3"
144
+ info = adapter.synthesize_to_file(
145
+ params[:text],
146
+ output_path,
147
+ voice: params[:voice],
148
+ model: params[:model],
149
+ response_format: params[:response_format] || "mp3",
150
+ speed: params[:speed],
151
+ language: params[:language],
152
+ )
153
+ info
154
+ end
155
+
156
+ # 9. 语音识别 (ASR — SenseVoiceSmall). Transcribes a local audio file.
157
+ SmartPrompt.define_worker :siliconflow_asr do
158
+ use "sf_asr"
159
+ model params[:model] if params[:model]
160
+ adapter = engine.llms["sf_asr"]
161
+
162
+ adapter.transcribe_audio(
163
+ params[:audio_file],
164
+ model: params[:model],
165
+ language: params[:language],
166
+ )
167
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_prompt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhuang biaowei
@@ -152,6 +152,7 @@ files:
152
152
  - config/image_generation_config.yml
153
153
  - config/multimodal_config.yml
154
154
  - config/sensenova_config.yml
155
+ - config/siliconflow_config.yml
155
156
  - config/zhipu_config.yml
156
157
  - docs/ANTHROPIC_EXAMPLES.md
157
158
  - docs/CONVERSATION_INTEGRATION_SUMMARY.md
@@ -176,14 +177,31 @@ files:
176
177
  - examples/multimodal_example.rb
177
178
  - examples/relevance_based_strategy_example.rb
178
179
  - examples/sensenova_example.rb
180
+ - examples/siliconflow_example.rb
179
181
  - examples/stt_example.rb
180
182
  - examples/tts_example.rb
181
183
  - examples/video_generation_example.rb
182
184
  - examples/zhipu_example.rb
183
185
  - lib/smart_prompt.rb
186
+ - lib/smart_prompt/adapters/siliconflow/embed.rb
187
+ - lib/smart_prompt/adapters/siliconflow/image.rb
188
+ - lib/smart_prompt/adapters/siliconflow/rerank.rb
189
+ - lib/smart_prompt/adapters/siliconflow/text.rb
190
+ - lib/smart_prompt/adapters/siliconflow/video.rb
191
+ - lib/smart_prompt/adapters/siliconflow/voice.rb
192
+ - lib/smart_prompt/adapters/zhipu/embed.rb
193
+ - lib/smart_prompt/adapters/zhipu/image.rb
194
+ - lib/smart_prompt/adapters/zhipu/rerank.rb
195
+ - lib/smart_prompt/adapters/zhipu/text.rb
196
+ - lib/smart_prompt/adapters/zhipu/video.rb
197
+ - lib/smart_prompt/adapters/zhipu/voice.rb
184
198
  - lib/smart_prompt/anthropic_adapter.rb
185
199
  - lib/smart_prompt/api_handler.rb
186
200
  - lib/smart_prompt/compression_engine.rb
201
+ - lib/smart_prompt/concerns/http_client.rb
202
+ - lib/smart_prompt/concerns/image_persistence.rb
203
+ - lib/smart_prompt/concerns/multimodal_messages.rb
204
+ - lib/smart_prompt/concerns/openai_chat_shaping.rb
187
205
  - lib/smart_prompt/context_strategy.rb
188
206
  - lib/smart_prompt/conversation.rb
189
207
  - lib/smart_prompt/db_adapter.rb
@@ -202,6 +220,7 @@ files:
202
220
  - lib/smart_prompt/relevance_based_strategy.rb
203
221
  - lib/smart_prompt/sensenova_adapter.rb
204
222
  - lib/smart_prompt/session.rb
223
+ - lib/smart_prompt/siliconflow_adapter.rb
205
224
  - lib/smart_prompt/sliding_window_strategy.rb
206
225
  - lib/smart_prompt/stt_adapter.rb
207
226
  - lib/smart_prompt/summary_based_strategy.rb
@@ -216,6 +235,7 @@ files:
216
235
  - workers/image_generation_workers.rb
217
236
  - workers/multimodal_workers.rb
218
237
  - workers/sensenova_workers.rb
238
+ - workers/siliconflow_workers.rb
219
239
  - workers/stt_workers.rb
220
240
  - workers/tts_workers.rb
221
241
  - workers/video_generation_workers.rb