smart_prompt 0.4.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 729a2b8524be928407172c7e12f14dc6bebc97f9fdf4e1f3ff35f9e7791a5f11
4
- data.tar.gz: a8fa95d01d5c5372bb669d67f5dca337db77c7e8444ddb166ac579318cf38e8d
3
+ metadata.gz: 396c6097973289a34143e86b65f428d55919d0e755916992a5c714e289ebf5a2
4
+ data.tar.gz: 5d2c2d81b486e1fb05b53f116047ab1f90be77c9536fd6440a96b573bdad00c3
5
5
  SHA512:
6
- metadata.gz: ab384181eba6b17a53505110cc3500c35d5774bbe08d4ea0d7cc367281dcd22c1f45ed0eff367e22aeac382d6f8d29efb20e328f6e07e0b8c64f5e2c42368029
7
- data.tar.gz: 62cbb97e5794f77fb467fdef9b5e6403450e5ed7332ca6f0044c967c16a193c70a334a2bd95897af79605ae6d51be8b2317e3011b70cc923dd859cd8b951fd57
6
+ metadata.gz: c6880395149678a195ea6efc46a81623d61c14c56534936041a625616a9e6b716597c078ccee0ac0d47c3b2a8e67d272742ee36940fa64321426799db0b26e4d
7
+ data.tar.gz: 63f0b8ae0f6f62363443731cae6fed3eafe746c463d259c9b88eee8563d6ef0cdcd84e684d6daeedd7329c3dcab84748ab62358fa2935b9d0278ec347df45ffb
@@ -239,16 +239,60 @@ module SmartPrompt
239
239
  end
240
240
 
241
241
  def extract_content(response)
242
- response.fetch("content", []).map do |block|
242
+ text_parts = []
243
+ tool_calls = []
244
+
245
+ response.fetch("content", []).each do |block|
243
246
  case block["type"]
244
247
  when "text"
245
- block["text"].to_s
248
+ text_parts << block["text"].to_s
246
249
  when "tool_use"
247
- block.to_s
250
+ tool_calls << openai_tool_call(block)
248
251
  else
249
- block.to_s
252
+ text_parts << block.to_s
250
253
  end
251
- end.join
254
+ end
255
+
256
+ content = text_parts.join
257
+ return content if tool_calls.empty?
258
+
259
+ openai_response(response, content, tool_calls)
260
+ end
261
+
262
+ def openai_response(response, content, tool_calls)
263
+ {
264
+ "id" => response["id"],
265
+ "object" => "chat.completion",
266
+ "created" => Time.now.to_i,
267
+ "model" => response["model"],
268
+ "choices" => [{
269
+ "index" => 0,
270
+ "message" => {
271
+ "role" => "assistant",
272
+ "content" => content,
273
+ "tool_calls" => tool_calls,
274
+ },
275
+ "finish_reason" => openai_finish_reason(response["stop_reason"]),
276
+ }],
277
+ "usage" => response["usage"],
278
+ }
279
+ end
280
+
281
+ def openai_tool_call(block)
282
+ {
283
+ "id" => block["id"],
284
+ "type" => "function",
285
+ "function" => {
286
+ "name" => block["name"],
287
+ "arguments" => JSON.generate(block["input"] || {}),
288
+ },
289
+ }
290
+ end
291
+
292
+ def openai_finish_reason(stop_reason)
293
+ return "tool_calls" if stop_reason == "tool_use"
294
+
295
+ stop_reason
252
296
  end
253
297
  end
254
298
  end
@@ -1,3 +1,3 @@
1
1
  module SmartPrompt
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  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.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhuang biaowei