Skip to content

Commit f0f7985

Browse files
committed
feat: enhance request payload handling for AI messages with tool calls
--bug=1068446@tapd-62980211 --user=刘瑞斌 【应用】AI对话开启思考过程并引用智能体作为技能,对话调用技能后,返回报错 https://www.tapd.cn/62980211/s/1902267
1 parent 04e6d6d commit f0f7985

File tree

1 file changed

+39
-1
lines changed
  • apps/models_provider/impl/aliyun_bai_lian_model_provider/model

1 file changed

+39
-1
lines changed

apps/models_provider/impl/aliyun_bai_lian_model_provider/model/llm.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22
# -*- coding: UTF-8 -*-
3-
from typing import Dict
3+
from typing import Dict, Any
4+
5+
from langchain_core.language_models import LanguageModelInput
6+
from langchain_core.messages import AIMessage
47

58
from models_provider.base_model_provider import MaxKBBaseModel
69
from models_provider.impl.base_chat_open_ai import BaseChatOpenAI
@@ -23,3 +26,38 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
2326
streaming=True,
2427
**optional_params,
2528
)
29+
30+
def _get_request_payload(
31+
self,
32+
input_: LanguageModelInput,
33+
*,
34+
stop: list[str] | None = None,
35+
**kwargs: Any,
36+
) -> dict:
37+
# Collect reasoning_content from AIMessages with tool_calls before base conversion.
38+
# When enable_thinking=true, Bailian API requires reasoning_content on any assistant
39+
# message that contains tool_calls (defaults to "" when not present).
40+
messages = self._convert_input(input_).to_messages()
41+
reasoning_content_map = {}
42+
for i, msg in enumerate(messages):
43+
if (
44+
isinstance(msg, AIMessage)
45+
and (msg.tool_calls or msg.invalid_tool_calls)
46+
):
47+
reasoning_content_map[i] = msg.additional_kwargs.get(
48+
"reasoning_content") or ""
49+
50+
payload = super()._get_request_payload(input_, stop=stop, **kwargs)
51+
52+
# Inject reasoning_content into assistant messages with tool_calls so that
53+
# Bailian deepseek thinking mode does not reject the request with a 400 error.
54+
if "messages" in payload and reasoning_content_map:
55+
for i, message in enumerate(payload["messages"]):
56+
if (
57+
i in reasoning_content_map
58+
and message.get("role") == "assistant"
59+
and message.get("tool_calls")
60+
):
61+
message["reasoning_content"] = reasoning_content_map[i]
62+
63+
return payload

0 commit comments

Comments
 (0)