@modular-prompt/driver 0.9.0 → 0.9.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modular-prompt/driver",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -236,22 +236,43 @@ def handle_completion(prompt, options=None):
236
236
  generate_text(prompt, options)
237
237
 
238
238
 
239
- def handle_chat_vlm(messages, images, options=None, max_image_size=768):
239
+ def handle_chat_vlm(messages, images, options=None, max_image_size=768, tools=None, primer=None):
240
240
  """VLMモデル用のチャット処理
241
241
 
242
242
  messages: TypeScript側で画像プレースホルダー({type: "image"})が挿入済み
243
243
  images: 画像ファイルパスの配列(プレースホルダーと位置が対応)
244
+ tools: ツール定義(テンプレートが対応している場合のみ使用)
245
+ primer: アシスタント応答のプリフィックス
244
246
  """
245
247
  if options is None:
246
248
  options = {}
247
249
 
250
+ # primer処理
251
+ add_generation_prompt = True
252
+ if primer is not None:
253
+ messages.append({'role': 'assistant', 'content': primer})
254
+ add_generation_prompt = False
255
+
248
256
  # processorのapply_chat_templateを直接使用
249
257
  # systemメッセージのマージはTypeScript側でchat_restrictionsに基づき処理済み
250
- formatted_prompt = processor.apply_chat_template(
251
- messages,
252
- add_generation_prompt=True,
253
- tokenize=False,
254
- )
258
+ # tools対応を試みる(テンプレートが対応していなければtools無しで実行)
259
+ try:
260
+ formatted_prompt = processor.apply_chat_template(
261
+ messages,
262
+ tools=tools,
263
+ add_generation_prompt=add_generation_prompt,
264
+ tokenize=False,
265
+ )
266
+ except TypeError:
267
+ formatted_prompt = processor.apply_chat_template(
268
+ messages,
269
+ add_generation_prompt=add_generation_prompt,
270
+ tokenize=False,
271
+ )
272
+
273
+ if primer is not None:
274
+ formatted_prompt = primer.join(formatted_prompt.split(primer)[0:-1]) + primer
275
+ print(primer, end='', flush=True)
255
276
 
256
277
  # 画像ファイルを読み込み・リサイズ
257
278
  pil_images = load_and_resize_images(images, max_image_size)
@@ -271,7 +292,7 @@ def generate_text_vlm(prompt, images, options):
271
292
 
272
293
  for response in vlm_stream_generate(
273
294
  model, processor, prompt,
274
- image=images,
295
+ image=images if images else None,
275
296
  max_tokens=max_tokens,
276
297
  temperature=temperature,
277
298
  ):
@@ -358,9 +379,9 @@ def main():
358
379
  tools = req.get('tools')
359
380
  images = req.get('images', [])
360
381
 
361
- if model_kind == "vlm" and images:
382
+ if model_kind == "vlm":
362
383
  max_image_size = req.get('maxImageSize', 768)
363
- handle_chat_vlm(messages, images, options, max_image_size)
384
+ handle_chat_vlm(messages, images, options, max_image_size, tools, primer)
364
385
  else:
365
386
  handle_chat(messages, primer, options, tools)
366
387
 
@@ -245,8 +245,8 @@ def detect_tool_call_format(tokenizer):
245
245
  if template:
246
246
  # 複数のtool_call関連パターンを順に試行
247
247
  tool_call_patterns = [
248
- # <tool_call>...</tool_call>, <|tool_call|>...<|/tool_call|>
249
- (r'<\|?tool_call\|?>', r'</?\|?tool_call\|?>|<\|?/tool_call\|?>'),
248
+ # </tool_call>, <|/tool_call|> (終了タグ専用)
249
+ (r'<\|?tool_call\|?>', r'</tool_call>|<\|/tool_call\|>'),
250
250
  # <|tool_call_start|>...<|tool_call_end|>
251
251
  (r'<\|tool_call_start\|>', r'<\|tool_call_end\|>'),
252
252
  # <start_function_call>...<end_function_call>