mirror of
https://github.com/jie65535/JChatGPT.git
synced 2026-06-23 00:49:31 +08:00
Add reasoning_content replay for tool calls
This commit is contained in:
@@ -547,6 +547,7 @@ object JChatGPT : KotlinPlugin(
|
||||
val startedAt = OffsetDateTime.now().toEpochSecond().toInt()
|
||||
val responseFlow = chatCompletions(history)
|
||||
var responseMessageBuilder: StringBuilder? = null
|
||||
var reasoningContentBuilder: StringBuilder? = null
|
||||
val responseToolCalls = mutableListOf<ToolCall.Function>()
|
||||
val toolCallTasks = mutableListOf<Deferred<ChatMessage>>()
|
||||
var lastTokenUsage: Usage? = null
|
||||
@@ -554,6 +555,15 @@ object JChatGPT : KotlinPlugin(
|
||||
responseFlow.collect { chunk ->
|
||||
val delta = chunk.choices[0].delta ?: return@collect
|
||||
|
||||
// 处理推理内容更新
|
||||
if (delta.reasoningContent != null) {
|
||||
if (reasoningContentBuilder == null) {
|
||||
reasoningContentBuilder = StringBuilder(delta.reasoningContent)
|
||||
} else {
|
||||
reasoningContentBuilder.append(delta.reasoningContent)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理内容更新
|
||||
if (delta.content != null) {
|
||||
if (responseMessageBuilder == null) {
|
||||
@@ -620,10 +630,15 @@ object JChatGPT : KotlinPlugin(
|
||||
val responseContent = responseMessageBuilder?.replace(thinkRegex, "")?.trim()
|
||||
logger.info("LLM Response: $responseContent")
|
||||
// 记录AI回答
|
||||
// reasoning_content仅在工具调用时需要回传(DeepSeek规范),否则丢弃
|
||||
// toolCalls空列表转null,避免序列化为"tool_calls":[]导致DeepSeek V4报400
|
||||
// explicitNulls=false确保null字段不会序列化到JSON中,兼容所有API
|
||||
history.add(
|
||||
ChatMessage.Assistant(
|
||||
ChatMessage(
|
||||
role = ChatRole.Assistant,
|
||||
content = responseContent,
|
||||
toolCalls = responseToolCalls
|
||||
toolCalls = responseToolCalls.ifEmpty { null },
|
||||
reasoningContent = if (responseToolCalls.isNotEmpty()) reasoningContentBuilder?.toString() else null
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -39,18 +39,28 @@ class ReasoningAgent : BaseAgent(
|
||||
|
||||
val prompt = args.getValue("prompt").jsonPrimitive.content
|
||||
val answerContent = StringBuilder()
|
||||
val reasoningContent = StringBuilder()
|
||||
llm.chatCompletions(ChatCompletionRequest(
|
||||
model = ModelId(PluginConfig.reasoningModel),
|
||||
messages = listOf(ChatMessage.User(prompt))
|
||||
)).collect {
|
||||
if (it.choices.isNotEmpty()) {
|
||||
val delta = it.choices[0].delta ?: return@collect
|
||||
if (!delta.reasoningContent.isNullOrEmpty()) {
|
||||
reasoningContent.append(delta.reasoningContent)
|
||||
}
|
||||
if (!delta.content.isNullOrEmpty()) {
|
||||
answerContent.append(delta.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return answerContent.toString().ifEmpty { "推理出错,结果为空" }
|
||||
val result = answerContent.toString()
|
||||
val reasoning = reasoningContent.toString()
|
||||
return when {
|
||||
result.isNotEmpty() -> result
|
||||
reasoning.isNotEmpty() -> reasoning
|
||||
else -> "推理出错,结果为空"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user