jack 6 days ago
parent
commit
8144aab2b2
1 changed files with 27 additions and 1 deletions
  1. 27 1
      app/core/runner/thread_runner.py

+ 27 - 1
app/core/runner/thread_runner.py

@@ -188,6 +188,32 @@ class ThreadRunner:
         logging.info("messages: run %s", run)
         logging.info(messages)
         logging.info(tools)
+        # 判断本次是否允许调用工具
+        use_tools = len(run_steps) < self.max_step
+        tool_choice = "auto" if use_tools else "none"
+
+        # 当不允许调用工具时,不传递 tools 参数,避免与 tool_choice='none' 冲突
+        tools_param = [tool.openai_function for tool in tools] if use_tools else None
+
+        # 仅在流式且需要 usage 统计时才传递 stream_options
+        stream_options = run.stream_options if self.stream else None
+
+        response_stream = llm.run(
+            messages=messages,
+            model=run.model,
+            tools=tools_param,                # 不使用工具时为 None
+            tool_choice=tool_choice,
+            stream=self.stream,
+            stream_options=stream_options,
+            extra_body=run.extra_body,
+            temperature=run.temperature or 1.0,
+            top_p=run.top_p,
+            response_format=run.response_format,
+            parallel_tool_calls=run.parallel_tool_calls if use_tools else None,  # 同样可条件传递
+            audio=run.audio,
+            modalities=run.modalities,
+        )
+        '''
         response_stream = llm.run(
             messages=messages,
             model=run.model,
@@ -203,7 +229,7 @@ class ThreadRunner:
             audio=run.audio,
             modalities=run.modalities,
         )
-
+        '''
         # create message callback
         create_message_callback = partial(
             MessageService.new_message,