feat: add thinking mode support for agents
This commit is contained in:
@@ -312,6 +312,7 @@ export default {
|
|||||||
availablePlaceholders: 'Available Placeholders',
|
availablePlaceholders: 'Available Placeholders',
|
||||||
placeholderHint: 'Type {{ to trigger autocomplete',
|
placeholderHint: 'Type {{ to trigger autocomplete',
|
||||||
temperature: 'Temperature',
|
temperature: 'Temperature',
|
||||||
|
thinking: 'Thinking Mode',
|
||||||
welcomeMessage: 'Welcome Message',
|
welcomeMessage: 'Welcome Message',
|
||||||
welcomeMessagePlaceholder: 'Message displayed when this agent is selected',
|
welcomeMessagePlaceholder: 'Message displayed when this agent is selected',
|
||||||
suggestedPrompts: 'Suggested Prompts',
|
suggestedPrompts: 'Suggested Prompts',
|
||||||
|
|||||||
@@ -842,6 +842,7 @@ export default {
|
|||||||
availablePlaceholders: "可用占位符",
|
availablePlaceholders: "可用占位符",
|
||||||
placeholderHint: "输入 {'{{'} 触发自动补全",
|
placeholderHint: "输入 {'{{'} 触发自动补全",
|
||||||
temperature: "温度",
|
temperature: "温度",
|
||||||
|
thinking: "思考模式",
|
||||||
welcomeMessage: "欢迎消息",
|
welcomeMessage: "欢迎消息",
|
||||||
welcomeMessagePlaceholder: "选择该智能体时显示的欢迎消息",
|
welcomeMessagePlaceholder: "选择该智能体时显示的欢迎消息",
|
||||||
suggestedPrompts: "推荐问题",
|
suggestedPrompts: "推荐问题",
|
||||||
|
|||||||
@@ -307,6 +307,17 @@
|
|||||||
<t-input-number v-model="formData.config.max_completion_tokens" :min="100" :max="100000" :step="100" theme="column" />
|
<t-input-number v-model="formData.config.max_completion_tokens" :min="100" :max="100000" :step="100" theme="column" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- 思考模式 -->
|
||||||
|
<div class="setting-row">
|
||||||
|
<div class="setting-info">
|
||||||
|
<label>{{ $t('agent.editor.thinking') || '思考模式' }}</label>
|
||||||
|
<p class="desc">启用模型的扩展思考能力(需要模型支持)</p>
|
||||||
|
</div>
|
||||||
|
<div class="setting-control">
|
||||||
|
<t-switch v-model="thinkingEnabled" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -1173,6 +1184,7 @@ const defaultFormData = {
|
|||||||
rerank_model_id: '',
|
rerank_model_id: '',
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
max_completion_tokens: 2048,
|
max_completion_tokens: 2048,
|
||||||
|
thinking: false, // 默认禁用思考模式
|
||||||
// Agent模式设置
|
// Agent模式设置
|
||||||
max_iterations: 10,
|
max_iterations: 10,
|
||||||
allowed_tools: [] as string[],
|
allowed_tools: [] as string[],
|
||||||
@@ -1223,6 +1235,12 @@ const agentMode = computed({
|
|||||||
|
|
||||||
const isAgentMode = computed(() => agentMode.value === 'smart-reasoning');
|
const isAgentMode = computed(() => agentMode.value === 'smart-reasoning');
|
||||||
|
|
||||||
|
// 思考模式计算属性(直接绑定 boolean)
|
||||||
|
const thinkingEnabled = computed({
|
||||||
|
get: () => formData.value.config.thinking === true,
|
||||||
|
set: (val: boolean) => { formData.value.config.thinking = val; }
|
||||||
|
});
|
||||||
|
|
||||||
// 是否为内置智能体
|
// 是否为内置智能体
|
||||||
const isBuiltinAgent = computed(() => {
|
const isBuiltinAgent = computed(() => {
|
||||||
return formData.value.is_builtin === true;
|
return formData.value.is_builtin === true;
|
||||||
|
|||||||
@@ -24,16 +24,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<!-- 非 Agent 模式下才显示传统的 markdown 渲染 -->
|
<!-- 非 Agent 模式下才显示传统的 markdown 渲染 -->
|
||||||
<div ref="parentMd" v-if="!session.hideContent && !session.isAgentMode">
|
<div ref="parentMd" v-if="!session.hideContent && !session.isAgentMode">
|
||||||
<!-- 消息正在总结中则渲染加载动画 -->
|
|
||||||
<div v-if="session.thinking" class="thinking-loading">
|
|
||||||
<div class="loading-typing">
|
|
||||||
<span></span>
|
|
||||||
<span></span>
|
|
||||||
<span></span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
|
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
|
||||||
<div class="content-wrapper">
|
<!-- 只有当有实际内容时才显示包围框 -->
|
||||||
|
<div class="content-wrapper" v-if="hasActualContent">
|
||||||
<div class="ai-markdown-template markdown-content">
|
<div class="ai-markdown-template markdown-content">
|
||||||
<div v-for="(token, index) in markdownTokens" :key="index" v-html="renderToken(token)"></div>
|
<div v-for="(token, index) in markdownTokens" :key="index" v-html="renderToken(token)"></div>
|
||||||
</div>
|
</div>
|
||||||
@@ -136,6 +129,12 @@ const markdownTokens = computed(() => {
|
|||||||
return marked.lexer(safeMarkdown);
|
return marked.lexer(safeMarkdown);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 计算属性:判断是否有实际内容(非空且不只是空白)
|
||||||
|
const hasActualContent = computed(() => {
|
||||||
|
const text = props.content || props.session?.content || '';
|
||||||
|
return text && text.trim().length > 0;
|
||||||
|
});
|
||||||
|
|
||||||
// 渲染单个 token 为 HTML
|
// 渲染单个 token 为 HTML
|
||||||
const renderToken = (token) => {
|
const renderToken = (token) => {
|
||||||
try {
|
try {
|
||||||
@@ -428,8 +427,7 @@ onBeforeUnmount(() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.thinking-loading {
|
.thinking-loading {
|
||||||
margin-left: 16px;
|
padding: 8px 0;
|
||||||
margin-bottom: 8px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading-typing {
|
.loading-typing {
|
||||||
|
|||||||
@@ -1,38 +1,32 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class='deep-think'>
|
<div class='deep-think'>
|
||||||
<t-collapse :default-value="deepSession.thinking ? [0] : []" expand-icon-placement="right" :expand-icon="true"
|
<div class="think-header" @click="toggleFold">
|
||||||
:borderless="true" @change="handlePanelChange">
|
<div class="think-title">
|
||||||
<t-collapse-panel>
|
<span v-if="deepSession.thinking" class="thinking-status">
|
||||||
<template #expandIcon>
|
<img class="thinking-gif" src="@/assets/img/think.gif" :alt="$t('chat.thinkingAlt')">
|
||||||
<div v-if="!deepSession.thinking" class="chevron">
|
<span class="thinking-text">{{ $t('chat.thinking') }}</span>
|
||||||
<t-icon :name="!isFold ? 'chevron-up' : 'chevron-down'" />
|
</span>
|
||||||
</div>
|
<span v-else class="done-status">
|
||||||
</template>
|
<img class="done-icon" src="@/assets/img/Frame3718.svg" :alt="$t('chat.deepThoughtAlt')">
|
||||||
<template #header>
|
<span class="done-text">{{ $t('chat.deepThoughtCompleted') }}</span>
|
||||||
<div class="deep-title">
|
</span>
|
||||||
<div v-if="deepSession.thinking" class="thinking">
|
</div>
|
||||||
<img class="img_gif" src="@/assets/img/think.gif" :alt="$t('chat.thinkingAlt')">{{ $t('chat.thinking') }}
|
<div class="toggle-icon-wrapper">
|
||||||
</div>
|
<t-icon :name="isFold ? 'chevron-down' : 'chevron-up'" class="toggle-icon" />
|
||||||
<div v-else class="done">
|
</div>
|
||||||
<img class="icon deep_icon" src="@/assets/img/Frame3718.svg" :alt="$t('chat.deepThoughtAlt')"></img>{{ $t('chat.deepThoughtCompleted') }}
|
</div>
|
||||||
</div>
|
<div class="think-content" v-show="!isFold || deepSession.thinking">
|
||||||
</div>
|
<div ref="contentInnerRef" class="content-inner" v-html="safeProcessThinkContent(deepSession.thinkContent)"></div>
|
||||||
</template>
|
</div>
|
||||||
<div class="content">
|
|
||||||
<span v-html="safeProcessThinkContent(deepSession.thinkContent)"></span>
|
|
||||||
</div>
|
|
||||||
</t-collapse-panel>
|
|
||||||
|
|
||||||
</t-collapse>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script setup>
|
<script setup>
|
||||||
import { onMounted, watch, computed, ref, reactive, defineProps } from 'vue';
|
import { watch, ref, defineProps, onMounted, nextTick } from 'vue';
|
||||||
import { sanitizeHTML } from '@/utils/security';
|
import { sanitizeHTML } from '@/utils/security';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
|
|
||||||
const isFold = ref(true)
|
const isFold = ref(false)
|
||||||
|
const contentInnerRef = ref(null)
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
// 必填项
|
// 必填项
|
||||||
@@ -42,18 +36,45 @@ const props = defineProps({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
// 初始化时检查:如果 thinking 已完成(从历史记录加载),默认折叠
|
||||||
() => props.deepSession,
|
|
||||||
(newVal) => { },
|
|
||||||
{ deep: true }
|
|
||||||
);
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
})
|
if (props.deepSession?.thinking === false) {
|
||||||
const showHide = () => {
|
isFold.value = true;
|
||||||
isFold.value = !isFold.value;
|
}
|
||||||
}
|
});
|
||||||
const handlePanelChange = (val) => {
|
|
||||||
isFold.value = !val.length ? true : false;
|
// 监听 thinking 状态变化,自动折叠
|
||||||
|
watch(
|
||||||
|
() => props.deepSession?.thinking,
|
||||||
|
(newVal, oldVal) => {
|
||||||
|
// 当 thinking 从 true 变为 false 时,自动折叠 thinking 内容
|
||||||
|
// 只在流式输出场景下触发(oldVal 为 true)
|
||||||
|
if (oldVal === true && newVal === false) {
|
||||||
|
isFold.value = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// 监听内容变化,自动滚动到底部
|
||||||
|
watch(
|
||||||
|
() => props.deepSession?.thinkContent,
|
||||||
|
() => {
|
||||||
|
// 只在 thinking 进行中时滚动
|
||||||
|
if (props.deepSession?.thinking) {
|
||||||
|
nextTick(() => {
|
||||||
|
if (contentInnerRef.value) {
|
||||||
|
contentInnerRef.value.scrollTop = contentInnerRef.value.scrollHeight;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const toggleFold = () => {
|
||||||
|
// 只有 thinking 完成后才能折叠/展开
|
||||||
|
if (!props.deepSession?.thinking) {
|
||||||
|
isFold.value = !isFold.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 安全地处理思考内容,防止XSS攻击
|
// 安全地处理思考内容,防止XSS攻击
|
||||||
@@ -71,83 +92,103 @@ const safeProcessThinkContent = (content) => {
|
|||||||
</script>
|
</script>
|
||||||
<style lang="less" scoped>
|
<style lang="less" scoped>
|
||||||
.deep-think {
|
.deep-think {
|
||||||
background: #30323605;
|
display: flex;
|
||||||
border-radius: 2px;
|
flex-direction: column;
|
||||||
|
font-size: 12px;
|
||||||
|
width: 100%;
|
||||||
|
border-radius: 8px;
|
||||||
|
background-color: #ffffff;
|
||||||
|
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
|
||||||
|
overflow: hidden;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
|
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
|
||||||
|
margin: -8px 0px 10px 0px;
|
||||||
|
|
||||||
:deep(.t-collapse.t--border-less .t-collapse-panel__body) {
|
.think-header {
|
||||||
background: #30323605 !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
:deep(.t-collapse-panel__wrapper .t-collapse-panel__header) {
|
|
||||||
background: #30323605 !important;
|
|
||||||
padding: 4px;
|
|
||||||
|
|
||||||
&:hover {
|
|
||||||
background-color: #30323605 !important;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
:deep(.t-collapse-panel__wrapper .t-collapse-panel__content) {
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chevron {
|
|
||||||
color: #00000099;
|
|
||||||
font-size: 14px;
|
|
||||||
padding: 0 2px 1px 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.deep-title {
|
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
padding: 6px 14px;
|
||||||
|
color: #333333;
|
||||||
|
font-weight: 500;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
// padding: 4px;
|
user-select: none;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
background: #30323605;
|
background-color: rgba(7, 192, 95, 0.04);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.think-title {
|
||||||
|
|
||||||
.thinking {
|
|
||||||
font-size: 14px;
|
|
||||||
color: #000000e6;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.img_gif {
|
.thinking-status {
|
||||||
width: 16px;
|
|
||||||
height: 16px;
|
|
||||||
margin-right: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.done {
|
|
||||||
font-size: 14px;
|
|
||||||
color: #00000099;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
.deep_icon {
|
.thinking-gif {
|
||||||
width: 16px;
|
width: 16px;
|
||||||
height: 16px;
|
height: 16px;
|
||||||
color: #07c05f;
|
margin-right: 8px;
|
||||||
margin-right: 4px;
|
}
|
||||||
|
|
||||||
|
.thinking-text {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #333333;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.done-status {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
|
||||||
|
.done-icon {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
margin-right: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.done-text {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #333333;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-icon-wrapper {
|
||||||
|
font-size: 14px;
|
||||||
|
padding: 0 2px 1px 2px;
|
||||||
|
color: #07c05f;
|
||||||
|
|
||||||
|
.toggle-icon {
|
||||||
|
transition: transform 0.2s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.content {
|
.think-content {
|
||||||
line-height: 23px;
|
border-top: 1px solid #f0f0f0;
|
||||||
font-size: 14px;
|
|
||||||
font-family: "PingFang SC";
|
.content-inner {
|
||||||
color: #00000099;
|
padding: 8px 14px;
|
||||||
margin: 6px 8px 4px;
|
font-size: 12px;
|
||||||
word-break: break-all;
|
line-height: 1.6;
|
||||||
text-align: justify;
|
color: #666666;
|
||||||
|
max-height: 200px;
|
||||||
|
overflow-y: auto;
|
||||||
|
word-break: break-word;
|
||||||
|
|
||||||
|
&::-webkit-scrollbar {
|
||||||
|
width: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
&::-webkit-scrollbar-thumb {
|
||||||
|
background: rgba(0, 0, 0, 0.1);
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -243,12 +243,21 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => {
|
|||||||
item.thinkContent = "";
|
item.thinkContent = "";
|
||||||
item.content = item.content;
|
item.content = item.content;
|
||||||
item.showThink = false;
|
item.showThink = false;
|
||||||
|
item.thinking = false;
|
||||||
} else if (item.content.includes('<\/think>')) {
|
} else if (item.content.includes('<\/think>')) {
|
||||||
|
// 历史消息中包含完整的 <think>...</think> 标签,说明 thinking 已完成
|
||||||
const arr = item.content.trim().split('<\/think>');
|
const arr = item.content.trim().split('<\/think>');
|
||||||
item.showThink = true;
|
item.showThink = true;
|
||||||
|
item.thinking = false; // 关键:标记 thinking 已完成,使 deepThink 默认折叠
|
||||||
item.thinkContent = arr[0].trim().replace('<think>', '');
|
item.thinkContent = arr[0].trim().replace('<think>', '');
|
||||||
let index = item.content.trim().lastIndexOf('<\/think>')
|
let index = item.content.trim().lastIndexOf('<\/think>')
|
||||||
item.content = item.content.substring(index + 8);
|
item.content = item.content.substring(index + 8);
|
||||||
|
} else if (item.content.includes('<think>')) {
|
||||||
|
// 内容包含 <think> 但没有 </think>,说明 thinking 还在进行中(不太可能出现在历史消息中)
|
||||||
|
item.showThink = true;
|
||||||
|
item.thinking = true;
|
||||||
|
item.thinkContent = item.content.replace('<think>', '').trim();
|
||||||
|
item.content = '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -408,16 +417,55 @@ onChunk((data) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否是 Agent 模式的响应
|
// 判断是否是 Agent 模式的响应
|
||||||
const isAgentResponse = data.response_type === 'thinking' ||
|
// 注意:'answer', 'complete', 'references' 类型可能在两种模式下都存在
|
||||||
data.response_type === 'tool_call' ||
|
// 只有 'thinking', 'tool_call', 'tool_result', 'reflection' 是 Agent 专有的
|
||||||
data.response_type === 'tool_result' ||
|
const isAgentOnlyResponse = data.response_type === 'thinking' ||
|
||||||
data.response_type === 'references' ||
|
data.response_type === 'tool_call' ||
|
||||||
data.response_type === 'answer' ||
|
data.response_type === 'tool_result' ||
|
||||||
data.response_type === 'reflection' ||
|
data.response_type === 'reflection';
|
||||||
data.response_type === 'stop';
|
|
||||||
|
// 检查当前消息是否已经是 Agent 模式
|
||||||
|
const lastMessage = messagesList[messagesList.length - 1];
|
||||||
|
const isCurrentlyAgentMode = lastMessage?.isAgentMode === true;
|
||||||
|
|
||||||
|
// 如果是 Agent 专有的响应类型,或者当前消息已经是 Agent 模式,则走 Agent 处理
|
||||||
|
const shouldHandleAsAgent = isAgentOnlyResponse || isCurrentlyAgentMode;
|
||||||
|
|
||||||
|
// 处理 references 事件 - 在两种模式下都需要处理,但不改变模式
|
||||||
|
if (data.response_type === 'references') {
|
||||||
|
// 如果当前是 Agent 模式,走 Agent 处理
|
||||||
|
if (isCurrentlyAgentMode) {
|
||||||
|
handleAgentChunk(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 非 Agent 模式:将 references 保存到消息中供 botmsg 使用
|
||||||
|
let existingMessage = messagesList.findLast((item) => item.request_id === data.id || item.id === data.id);
|
||||||
|
|
||||||
|
// 如果消息还不存在,先创建一个空的 assistant 消息
|
||||||
|
if (!existingMessage) {
|
||||||
|
existingMessage = {
|
||||||
|
id: data.id,
|
||||||
|
request_id: data.id,
|
||||||
|
role: 'assistant',
|
||||||
|
content: '',
|
||||||
|
showThink: false,
|
||||||
|
thinkContent: '',
|
||||||
|
thinking: false,
|
||||||
|
is_completed: false,
|
||||||
|
knowledge_references: []
|
||||||
|
};
|
||||||
|
messagesList.push(existingMessage);
|
||||||
|
loading.value = false; // 消息已创建,关闭 loading
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
existingMessage.knowledge_references = data.knowledge_references || data.data?.references || [];
|
||||||
|
console.log('[References] Saved to message, count:', existingMessage.knowledge_references.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Agent 模式处理(包括 stop 事件)
|
// Agent 模式处理(包括 stop 事件)
|
||||||
if (isAgentResponse || messagesList[messagesList.length - 1]?.isAgentMode) {
|
if (shouldHandleAsAgent) {
|
||||||
// 在 handleAgentChunk 中处理 loading 状态
|
// 在 handleAgentChunk 中处理 loading 状态
|
||||||
handleAgentChunk(data);
|
handleAgentChunk(data);
|
||||||
|
|
||||||
@@ -432,7 +480,23 @@ onChunk((data) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 原有的知识库 QA 处理逻辑
|
// 原有的知识库 QA 处理逻辑(非 Agent 模式)
|
||||||
|
// answer 内容中可能包含 <think>...</think> 标签
|
||||||
|
|
||||||
|
// 检查消息是否已经完成,如果已完成则忽略后续的完成事件(防止空内容覆盖)
|
||||||
|
const existingMessage = messagesList.findLast((item) => {
|
||||||
|
if (item.request_id === data.id) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return item.id === data.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 如果消息已完成且当前事件是完成事件(done=true 且无内容),直接忽略
|
||||||
|
if (existingMessage?.is_completed && data.done && !data.content) {
|
||||||
|
console.log('[Non-Agent] Ignoring duplicate completion event for completed message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
fullContent.value += data.content;
|
fullContent.value += data.content;
|
||||||
let obj = { ...data, content: '', role: 'assistant', showThink: false, is_completed: false };
|
let obj = { ...data, content: '', role: 'assistant', showThink: false, is_completed: false };
|
||||||
|
|
||||||
@@ -451,13 +515,6 @@ onChunk((data) => {
|
|||||||
obj.content = fullContent.value;
|
obj.content = fullContent.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否已有消息,如果没有则说明这是第一次,关闭 loading
|
|
||||||
const existingMessage = messagesList.findLast((item) => {
|
|
||||||
if (item.request_id === obj.id) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return item.id === obj.id;
|
|
||||||
});
|
|
||||||
if (!existingMessage) {
|
if (!existingMessage) {
|
||||||
loading.value = false; // 消息即将创建,关闭 loading
|
loading.value = false; // 消息即将创建,关闭 loading
|
||||||
}
|
}
|
||||||
@@ -704,8 +761,20 @@ const handleAgentChunk = (data) => {
|
|||||||
case 'answer':
|
case 'answer':
|
||||||
// 最终答案
|
// 最终答案
|
||||||
message.thinking = false;
|
message.thinking = false;
|
||||||
message.content = (message.content || '') + (data.content || '');
|
|
||||||
fullContent.value += data.content || '';
|
console.log('[Answer Event] Received:', {
|
||||||
|
has_content: !!data.content,
|
||||||
|
content_length: data.content?.length || 0,
|
||||||
|
done: data.done,
|
||||||
|
current_message_content_length: message.content?.length || 0
|
||||||
|
});
|
||||||
|
|
||||||
|
// 只有当有实际内容时才追加,避免空内容覆盖
|
||||||
|
if (data.content) {
|
||||||
|
message.content = (message.content || '') + data.content;
|
||||||
|
fullContent.value += data.content;
|
||||||
|
console.log('[Answer] Content appended, new length:', message.content.length);
|
||||||
|
}
|
||||||
|
|
||||||
// Add or update answer event in agentEventStream
|
// Add or update answer event in agentEventStream
|
||||||
if (!message.agentEventStream) message.agentEventStream = [];
|
if (!message.agentEventStream) message.agentEventStream = [];
|
||||||
@@ -718,13 +787,19 @@ const handleAgentChunk = (data) => {
|
|||||||
done: false
|
done: false
|
||||||
};
|
};
|
||||||
message.agentEventStream.push(answerEvent);
|
message.agentEventStream.push(answerEvent);
|
||||||
|
console.log('[Answer] Created new answer event in stream');
|
||||||
}
|
}
|
||||||
|
|
||||||
answerEvent.content = message.content;
|
// 只有当有实际内容时才更新 answerEvent.content
|
||||||
answerEvent.done = data.done;
|
if (data.content) {
|
||||||
|
answerEvent.content = message.content;
|
||||||
|
console.log('[Answer] answerEvent.content updated, length:', answerEvent.content.length);
|
||||||
|
}
|
||||||
|
|
||||||
if (data.done) {
|
// 只在第一次收到 done:true 时标记完成,忽略后续重复的完成事件
|
||||||
console.log('[Agent] Answer done, content length:', message.content?.length || 0);
|
if (data.done && !answerEvent.done) {
|
||||||
|
answerEvent.done = true;
|
||||||
|
console.log('[Agent] Answer done, content length:', message.content?.length || 0, 'answerEvent.content length:', answerEvent.content?.length || 0);
|
||||||
|
|
||||||
// 完成 - 关闭所有状态
|
// 完成 - 关闭所有状态
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
@@ -735,9 +810,19 @@ const handleAgentChunk = (data) => {
|
|||||||
|
|
||||||
// 标题生成已改为异步事件推送,不再需要在这里手动调用
|
// 标题生成已改为异步事件推送,不再需要在这里手动调用
|
||||||
// 如果标题还未生成,前端会通过 SSE 事件接收
|
// 如果标题还未生成,前端会通过 SSE 事件接收
|
||||||
|
} else if (data.done && answerEvent.done) {
|
||||||
|
console.log('[Answer] Ignoring duplicate done event, current content preserved:', answerEvent.content?.length || 0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'complete':
|
||||||
|
// 整个流式响应完成事件 - 确保状态正确关闭
|
||||||
|
console.log('[Agent] Complete event received');
|
||||||
|
loading.value = false;
|
||||||
|
isReplying.value = false;
|
||||||
|
// 不清空 message.content,保持已有内容
|
||||||
|
break;
|
||||||
|
|
||||||
case 'stop':
|
case 'stop':
|
||||||
// 停止事件 - 添加到事件流并标记对话完成
|
// 停止事件 - 添加到事件流并标记对话完成
|
||||||
console.log('[Agent] Stop event received');
|
console.log('[Agent] Stop event received');
|
||||||
|
|||||||
@@ -688,12 +688,13 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
|||||||
iteration int,
|
iteration int,
|
||||||
sessionID string,
|
sessionID string,
|
||||||
) (*types.ChatResponse, error) {
|
) (*types.ChatResponse, error) {
|
||||||
logger.Infof(ctx, "[Agent][Thinking][Iteration-%d] Starting thinking stream with temperature=%.2f, tools=%d",
|
logger.Infof(ctx, "[Agent][Thinking][Iteration-%d] Starting thinking stream with temperature=%.2f, tools=%d, thinking=%v",
|
||||||
iteration+1, e.config.Temperature, len(tools))
|
iteration+1, e.config.Temperature, len(tools), e.config.Thinking)
|
||||||
|
|
||||||
opts := &chat.ChatOptions{
|
opts := &chat.ChatOptions{
|
||||||
Temperature: e.config.Temperature,
|
Temperature: e.config.Temperature,
|
||||||
Tools: tools,
|
Tools: tools,
|
||||||
|
Thinking: e.config.Thinking,
|
||||||
}
|
}
|
||||||
logger.Debug(context.Background(), "[Agent] streamLLM opts tool_choice=auto temperature=", e.config.Temperature)
|
logger.Debug(context.Background(), "[Agent] streamLLM opts tool_choice=auto temperature=", e.config.Temperature)
|
||||||
|
|
||||||
@@ -832,7 +833,7 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
|
|||||||
fullAnswer, _, err := e.streamLLMToEventBus(
|
fullAnswer, _, err := e.streamLLMToEventBus(
|
||||||
ctx,
|
ctx,
|
||||||
messages,
|
messages,
|
||||||
&chat.ChatOptions{Temperature: e.config.Temperature},
|
&chat.ChatOptions{Temperature: e.config.Temperature, Thinking: e.config.Thinking},
|
||||||
func(chunk *types.StreamResponse, fullContent string) {
|
func(chunk *types.StreamResponse, fullContent string) {
|
||||||
if chunk.Content != "" {
|
if chunk.Content != "" {
|
||||||
logger.Debugf(ctx, "[Agent][FinalAnswer] Emitting answer chunk: %d chars", len(chunk.Content))
|
logger.Debugf(ctx, "[Agent][FinalAnswer] Emitting answer chunk: %d chars", len(chunk.Content))
|
||||||
|
|||||||
@@ -99,9 +99,13 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Start goroutine to consume channel and emit events directly
|
// Start goroutine to consume channel and emit events directly
|
||||||
|
// For non-agent mode, thinking content is embedded in answer stream with <think> tags
|
||||||
|
// This ensures consistent display between streaming and history loading
|
||||||
go func() {
|
go func() {
|
||||||
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
|
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
|
||||||
var finalContent string
|
var finalContent string
|
||||||
|
var thinkingStarted bool
|
||||||
|
var thinkingEnded bool
|
||||||
|
|
||||||
for response := range responseChan {
|
for response := range responseChan {
|
||||||
// Handle error responses from the stream
|
// Handle error responses from the stream
|
||||||
@@ -122,8 +126,53 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For non-agent mode: embed thinking content with <think> tags in answer stream
|
||||||
|
// This ensures the frontend uses deepThink.vue component consistently
|
||||||
|
if response.ResponseType == types.ResponseTypeThinking {
|
||||||
|
content := response.Content
|
||||||
|
// Add <think> tag at the beginning of thinking content
|
||||||
|
if !thinkingStarted {
|
||||||
|
content = "<think>" + content
|
||||||
|
thinkingStarted = true
|
||||||
|
}
|
||||||
|
// Add </think> tag at the end of thinking content
|
||||||
|
if response.Done && !thinkingEnded {
|
||||||
|
content = content + "</think>"
|
||||||
|
thinkingEnded = true
|
||||||
|
}
|
||||||
|
finalContent += content
|
||||||
|
if err := eventBus.Emit(ctx, types.Event{
|
||||||
|
ID: answerID,
|
||||||
|
Type: types.EventType(event.EventAgentFinalAnswer),
|
||||||
|
SessionID: chatManage.SessionID,
|
||||||
|
Data: event.AgentFinalAnswerData{
|
||||||
|
Content: content,
|
||||||
|
Done: false, // Thinking is not the final answer
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
logger.Errorf(ctx, "Failed to emit thinking as answer event: %v", err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Emit event for each answer chunk
|
// Emit event for each answer chunk
|
||||||
if response.ResponseType == types.ResponseTypeAnswer {
|
if response.ResponseType == types.ResponseTypeAnswer {
|
||||||
|
// If we had thinking but it wasn't explicitly ended, close the think tag
|
||||||
|
if thinkingStarted && !thinkingEnded {
|
||||||
|
thinkingEnded = true
|
||||||
|
finalContent += "</think>"
|
||||||
|
if err := eventBus.Emit(ctx, types.Event{
|
||||||
|
ID: answerID,
|
||||||
|
Type: types.EventType(event.EventAgentFinalAnswer),
|
||||||
|
SessionID: chatManage.SessionID,
|
||||||
|
Data: event.AgentFinalAnswerData{
|
||||||
|
Content: "</think>",
|
||||||
|
Done: false,
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
logger.Errorf(ctx, "Failed to emit think close tag: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
finalContent += response.Content
|
finalContent += response.Content
|
||||||
if err := eventBus.Emit(ctx, types.Event{
|
if err := eventBus.Emit(ctx, types.Event{
|
||||||
ID: answerID,
|
ID: answerID,
|
||||||
|
|||||||
@@ -487,6 +487,12 @@ func (s *sessionService) KnowledgeQA(
|
|||||||
summaryConfig.MaxCompletionTokens = customAgent.Config.MaxCompletionTokens
|
summaryConfig.MaxCompletionTokens = customAgent.Config.MaxCompletionTokens
|
||||||
logger.Infof(ctx, "Using custom agent's max_completion_tokens: %d", customAgent.Config.MaxCompletionTokens)
|
logger.Infof(ctx, "Using custom agent's max_completion_tokens: %d", customAgent.Config.MaxCompletionTokens)
|
||||||
}
|
}
|
||||||
|
// Override thinking mode from agent config
|
||||||
|
// Agent-level thinking setting takes full control (no global fallback)
|
||||||
|
summaryConfig.Thinking = customAgent.Config.Thinking
|
||||||
|
if customAgent.Config.Thinking != nil {
|
||||||
|
logger.Infof(ctx, "Using custom agent's thinking: %v", *customAgent.Config.Thinking)
|
||||||
|
}
|
||||||
// Override retrieval strategy settings
|
// Override retrieval strategy settings
|
||||||
if customAgent.Config.EmbeddingTopK > 0 {
|
if customAgent.Config.EmbeddingTopK > 0 {
|
||||||
embeddingTopK = customAgent.Config.EmbeddingTopK
|
embeddingTopK = customAgent.Config.EmbeddingTopK
|
||||||
@@ -1104,6 +1110,7 @@ func (s *sessionService) AgentQA(
|
|||||||
HistoryTurns: customAgent.Config.HistoryTurns,
|
HistoryTurns: customAgent.Config.HistoryTurns,
|
||||||
MCPSelectionMode: customAgent.Config.MCPSelectionMode,
|
MCPSelectionMode: customAgent.Config.MCPSelectionMode,
|
||||||
MCPServices: customAgent.Config.MCPServices,
|
MCPServices: customAgent.Config.MCPServices,
|
||||||
|
Thinking: customAgent.Config.Thinking,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve knowledge bases: request-level @ mentions take priority over agent config
|
// Resolve knowledge bases: request-level @ mentions take priority over agent config
|
||||||
|
|||||||
@@ -96,14 +96,14 @@ func buildStreamResponse(evt interfaces.StreamEvent, requestID string) *types.St
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sendCompletionEvent sends a final completion event to the client
|
// sendCompletionEvent sends a final completion event to the client
|
||||||
|
// NOTE: This is now a no-op because:
|
||||||
|
// 1. The 'complete' event from handleComplete already signals stream completion
|
||||||
|
// 2. Sending an extra empty 'answer' event with done:true causes frontend issues
|
||||||
|
// (multiple done events can confuse state management)
|
||||||
|
// The frontend should use 'complete' response_type to detect stream completion
|
||||||
func sendCompletionEvent(c *gin.Context, requestID string) {
|
func sendCompletionEvent(c *gin.Context, requestID string) {
|
||||||
c.SSEvent("message", &types.StreamResponse{
|
// Intentionally empty - completion is signaled by the 'complete' event
|
||||||
ID: requestID,
|
// which is already sent before this function is called
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
|
||||||
Content: "",
|
|
||||||
Done: true,
|
|
||||||
})
|
|
||||||
c.Writer.Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// createAgentQueryEvent creates a standard agent query event
|
// createAgentQueryEvent creates a standard agent query event
|
||||||
|
|||||||
@@ -323,6 +323,10 @@ func (h *Handler) executeNormalModeQA(reqCtx *qaRequestContext, generateTitle bo
|
|||||||
streamCtx := h.setupSSEStream(reqCtx, generateTitle)
|
streamCtx := h.setupSSEStream(reqCtx, generateTitle)
|
||||||
|
|
||||||
// Setup completion handler for normal mode
|
// Setup completion handler for normal mode
|
||||||
|
// Note: Thinking content is now embedded in answer stream with <think> tags
|
||||||
|
// by chat_completion_stream.go, so we don't need separate thinking event handling
|
||||||
|
var completionHandled bool // Prevent duplicate completion handling
|
||||||
|
|
||||||
streamCtx.eventBus.On(event.EventAgentFinalAnswer, func(ctx context.Context, evt event.Event) error {
|
streamCtx.eventBus.On(event.EventAgentFinalAnswer, func(ctx context.Context, evt event.Event) error {
|
||||||
data, ok := evt.Data.(event.AgentFinalAnswerData)
|
data, ok := evt.Data.(event.AgentFinalAnswerData)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -330,14 +334,22 @@ func (h *Handler) executeNormalModeQA(reqCtx *qaRequestContext, generateTitle bo
|
|||||||
}
|
}
|
||||||
streamCtx.assistantMessage.Content += data.Content
|
streamCtx.assistantMessage.Content += data.Content
|
||||||
if data.Done {
|
if data.Done {
|
||||||
|
// Prevent duplicate completion handling
|
||||||
|
if completionHandled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
completionHandled = true
|
||||||
|
|
||||||
logger.Infof(streamCtx.asyncCtx, "Knowledge QA service completed for session: %s", sessionID)
|
logger.Infof(streamCtx.asyncCtx, "Knowledge QA service completed for session: %s", sessionID)
|
||||||
|
// Content already contains <think>...</think> tags from chat_completion_stream.go
|
||||||
h.completeAssistantMessage(streamCtx.asyncCtx, streamCtx.assistantMessage)
|
h.completeAssistantMessage(streamCtx.asyncCtx, streamCtx.assistantMessage)
|
||||||
|
// Emit EventAgentComplete - this will trigger handleComplete which sends the SSE complete event
|
||||||
|
// Note: Don't cancel context here, let the SSE handler close naturally after receiving the complete event
|
||||||
streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{
|
streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{
|
||||||
Type: event.EventAgentComplete,
|
Type: event.EventAgentComplete,
|
||||||
SessionID: sessionID,
|
SessionID: sessionID,
|
||||||
Data: event.AgentCompleteData{FinalAnswer: streamCtx.assistantMessage.Content},
|
Data: event.AgentCompleteData{FinalAnswer: streamCtx.assistantMessage.Content},
|
||||||
})
|
})
|
||||||
streamCtx.cancel()
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
@@ -87,18 +88,41 @@ type ChatConfig struct {
|
|||||||
|
|
||||||
// NewChat 创建聊天实例
|
// NewChat 创建聊天实例
|
||||||
func NewChat(config *ChatConfig, ollamaService *ollama.OllamaService) (Chat, error) {
|
func NewChat(config *ChatConfig, ollamaService *ollama.OllamaService) (Chat, error) {
|
||||||
var chat Chat
|
|
||||||
var err error
|
|
||||||
switch strings.ToLower(string(config.Source)) {
|
switch strings.ToLower(string(config.Source)) {
|
||||||
case string(types.ModelSourceLocal):
|
case string(types.ModelSourceLocal):
|
||||||
chat, err = NewOllamaChat(config, ollamaService)
|
return NewOllamaChat(config, ollamaService)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return chat, nil
|
|
||||||
case string(types.ModelSourceRemote):
|
case string(types.ModelSourceRemote):
|
||||||
return NewRemoteAPIChat(config)
|
return NewRemoteChat(config)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported chat model source: %s", config.Source)
|
return nil, fmt.Errorf("unsupported chat model source: %s", config.Source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewRemoteChat 根据 provider 创建远程聊天实例
|
||||||
|
func NewRemoteChat(config *ChatConfig) (Chat, error) {
|
||||||
|
providerName := provider.ProviderName(config.Provider)
|
||||||
|
if providerName == "" {
|
||||||
|
providerName = provider.DetectProvider(config.BaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch providerName {
|
||||||
|
case provider.ProviderLKEAP:
|
||||||
|
// LKEAP 有特殊的 thinking 参数格式
|
||||||
|
return NewLKEAPChat(config)
|
||||||
|
case provider.ProviderAliyun:
|
||||||
|
// 检查是否为 Qwen3 模型(需要特殊处理 enable_thinking)
|
||||||
|
if provider.IsQwen3Model(config.ModelName) {
|
||||||
|
return NewQwenChat(config)
|
||||||
|
}
|
||||||
|
return NewRemoteAPIChat(config)
|
||||||
|
case provider.ProviderDeepSeek:
|
||||||
|
// DeepSeek 不支持 tool_choice
|
||||||
|
return NewDeepSeekChat(config)
|
||||||
|
case provider.ProviderGeneric:
|
||||||
|
// Generic provider (如 vLLM) 使用 ChatTemplateKwargs
|
||||||
|
return NewGenericChat(config)
|
||||||
|
default:
|
||||||
|
// 其他 provider 使用标准 OpenAI 兼容实现
|
||||||
|
return NewRemoteAPIChat(config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LKEAPChat 腾讯云知识引擎原子能力 (LKEAP) 聊天实现
|
||||||
|
// 支持 DeepSeek-R1, DeepSeek-V3 系列模型,具备思维链能力
|
||||||
|
// 参考:https://cloud.tencent.com/document/product/1772/115963
|
||||||
|
//
|
||||||
|
// 与标准 OpenAI API 的区别:
|
||||||
|
// 1. thinking 参数格式不同:LKEAP 使用 {"type": "enabled"/"disabled"}
|
||||||
|
// 2. 仅 DeepSeek V3.x 系列需要显式设置 thinking 参数,R1 系列默认开启
|
||||||
|
type LKEAPChat struct {
|
||||||
|
*RemoteAPIChat
|
||||||
|
}
|
||||||
|
|
||||||
|
// LKEAPThinkingConfig 思维链配置(LKEAP 特有格式)
|
||||||
|
type LKEAPThinkingConfig struct {
|
||||||
|
Type string `json:"type"` // "enabled" 或 "disabled"
|
||||||
|
}
|
||||||
|
|
||||||
|
// LKEAPChatCompletionRequest LKEAP 自定义请求结构体
|
||||||
|
type LKEAPChatCompletionRequest struct {
|
||||||
|
openai.ChatCompletionRequest
|
||||||
|
Thinking *LKEAPThinkingConfig `json:"thinking,omitempty"` // 思维链开关(仅 V3.x 系列)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLKEAPChat 创建 LKEAP 聊天实例
|
||||||
|
func NewLKEAPChat(config *ChatConfig) (*LKEAPChat, error) {
|
||||||
|
// 确保 provider 设置正确
|
||||||
|
config.Provider = string(provider.ProviderLKEAP)
|
||||||
|
|
||||||
|
remoteChat, err := NewRemoteAPIChat(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chat := &LKEAPChat{
|
||||||
|
RemoteAPIChat: remoteChat,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求自定义器,添加 LKEAP 特有的 thinking 参数
|
||||||
|
remoteChat.SetRequestCustomizer(chat.customizeRequest)
|
||||||
|
|
||||||
|
return chat, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isDeepSeekV3Model 检查是否为 DeepSeek V3.x 系列模型
|
||||||
|
func (c *LKEAPChat) isDeepSeekV3Model() bool {
|
||||||
|
return strings.Contains(strings.ToLower(c.GetModelName()), "deepseek-v3")
|
||||||
|
}
|
||||||
|
|
||||||
|
// customizeRequest 自定义 LKEAP 请求
|
||||||
|
func (c *LKEAPChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
|
||||||
|
// 仅对 DeepSeek V3.x 系列模型需要特殊处理 thinking 参数
|
||||||
|
// R1 系列模型默认开启思维链,无需额外参数
|
||||||
|
if !c.isDeepSeekV3Model() || opts == nil || opts.Thinking == nil {
|
||||||
|
return nil, false // 使用标准请求
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建 LKEAP 特有请求
|
||||||
|
lkeapReq := LKEAPChatCompletionRequest{
|
||||||
|
ChatCompletionRequest: *req,
|
||||||
|
}
|
||||||
|
|
||||||
|
thinkingType := "disabled"
|
||||||
|
if *opts.Thinking {
|
||||||
|
thinkingType = "enabled"
|
||||||
|
}
|
||||||
|
lkeapReq.Thinking = &LKEAPThinkingConfig{Type: thinkingType}
|
||||||
|
|
||||||
|
return lkeapReq, true // 使用原始 HTTP 请求
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeepSeekChat DeepSeek 模型聊天实现
|
||||||
|
// DeepSeek 模型不支持 tool_choice 参数
|
||||||
|
type DeepSeekChat struct {
|
||||||
|
*RemoteAPIChat
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDeepSeekChat 创建 DeepSeek 聊天实例
|
||||||
|
func NewDeepSeekChat(config *ChatConfig) (*DeepSeekChat, error) {
|
||||||
|
config.Provider = string(provider.ProviderDeepSeek)
|
||||||
|
|
||||||
|
remoteChat, err := NewRemoteAPIChat(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chat := &DeepSeekChat{
|
||||||
|
RemoteAPIChat: remoteChat,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求自定义器
|
||||||
|
remoteChat.SetRequestCustomizer(chat.customizeRequest)
|
||||||
|
|
||||||
|
return chat, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// customizeRequest 自定义 DeepSeek 请求
|
||||||
|
func (c *DeepSeekChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
|
||||||
|
// DeepSeek 模型不支持 tool_choice,需要清除
|
||||||
|
if opts != nil && opts.ToolChoice != "" {
|
||||||
|
logger.Infof(context.Background(), "deepseek model, skip tool_choice")
|
||||||
|
req.ToolChoice = nil
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenericChat 通用 OpenAI 兼容实现(如 vLLM)
|
||||||
|
// 支持 ChatTemplateKwargs 参数
|
||||||
|
type GenericChat struct {
|
||||||
|
*RemoteAPIChat
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGenericChat 创建通用聊天实例
|
||||||
|
func NewGenericChat(config *ChatConfig) (*GenericChat, error) {
|
||||||
|
config.Provider = string(provider.ProviderGeneric)
|
||||||
|
|
||||||
|
remoteChat, err := NewRemoteAPIChat(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chat := &GenericChat{
|
||||||
|
RemoteAPIChat: remoteChat,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求自定义器
|
||||||
|
remoteChat.SetRequestCustomizer(chat.customizeRequest)
|
||||||
|
|
||||||
|
return chat, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// customizeRequest 自定义 Generic 请求
|
||||||
|
func (c *GenericChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
|
||||||
|
// Generic provider(如 vLLM)使用 ChatTemplateKwargs 传递 thinking 参数
|
||||||
|
thinking := false
|
||||||
|
if opts != nil && opts.Thinking != nil {
|
||||||
|
thinking = *opts.Thinking
|
||||||
|
}
|
||||||
|
req.ChatTemplateKwargs = map[string]interface{}{
|
||||||
|
"enable_thinking": thinking,
|
||||||
|
}
|
||||||
|
return nil, false // 使用标准请求(已修改)
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
// QwenChat 阿里云 Qwen 模型聊天实现
|
||||||
|
// Qwen3 模型需要特殊处理 enable_thinking 参数
|
||||||
|
type QwenChat struct {
|
||||||
|
*RemoteAPIChat
|
||||||
|
}
|
||||||
|
|
||||||
|
// QwenChatCompletionRequest Qwen 模型的自定义请求结构体
|
||||||
|
type QwenChatCompletionRequest struct {
|
||||||
|
openai.ChatCompletionRequest
|
||||||
|
EnableThinking *bool `json:"enable_thinking,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewQwenChat 创建 Qwen 聊天实例
|
||||||
|
func NewQwenChat(config *ChatConfig) (*QwenChat, error) {
|
||||||
|
config.Provider = string(provider.ProviderAliyun)
|
||||||
|
|
||||||
|
remoteChat, err := NewRemoteAPIChat(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chat := &QwenChat{
|
||||||
|
RemoteAPIChat: remoteChat,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求自定义器
|
||||||
|
remoteChat.SetRequestCustomizer(chat.customizeRequest)
|
||||||
|
|
||||||
|
return chat, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isQwen3Model 检查是否为 Qwen3 模型
|
||||||
|
func (c *QwenChat) isQwen3Model() bool {
|
||||||
|
return provider.IsQwen3Model(c.GetModelName())
|
||||||
|
}
|
||||||
|
|
||||||
|
// customizeRequest 自定义 Qwen 请求
|
||||||
|
func (c *QwenChat) customizeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
|
||||||
|
// 仅 Qwen3 模型需要特殊处理
|
||||||
|
if !c.isQwen3Model() {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 非流式请求需要显式禁用 thinking
|
||||||
|
if !isStream {
|
||||||
|
qwenReq := QwenChatCompletionRequest{
|
||||||
|
ChatCompletionRequest: *req,
|
||||||
|
}
|
||||||
|
enableThinking := false
|
||||||
|
qwenReq.EnableThinking = &enableThinking
|
||||||
|
return qwenReq, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
+381
-335
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
@@ -13,23 +14,22 @@ import (
|
|||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RemoteAPIChat 实现了基于的聊天
|
// RemoteAPIChat 实现了基于 OpenAI 兼容 API 的聊天
|
||||||
|
// 这是一个通用实现,不包含任何 provider 特定的逻辑
|
||||||
type RemoteAPIChat struct {
|
type RemoteAPIChat struct {
|
||||||
modelName string
|
modelName string
|
||||||
client *openai.Client
|
client *openai.Client
|
||||||
modelID string
|
modelID string
|
||||||
baseURL string
|
baseURL string
|
||||||
apiKey string
|
apiKey string
|
||||||
provider provider.ProviderName // Provider identifier for routing
|
provider provider.ProviderName
|
||||||
|
|
||||||
|
// requestCustomizer 允许子类自定义请求
|
||||||
|
// 返回自定义请求体(如果为 nil 则使用标准请求)和是否需要使用原始 HTTP 请求
|
||||||
|
requestCustomizer func(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (customReq any, useRawHTTP bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QwenChatCompletionRequest 用于 qwen 模型的自定义请求结构体
|
// NewRemoteAPIChat 创建远程 API 聊天实例
|
||||||
type QwenChatCompletionRequest struct {
|
|
||||||
openai.ChatCompletionRequest
|
|
||||||
EnableThinking *bool `json:"enable_thinking,omitempty"` // qwen 模型专用字段
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRemoteAPIChat 调用远程API 聊天实例
|
|
||||||
func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
|
func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
|
||||||
apiKey := chatConfig.APIKey
|
apiKey := chatConfig.APIKey
|
||||||
config := openai.DefaultConfig(apiKey)
|
config := openai.DefaultConfig(apiKey)
|
||||||
@@ -37,7 +37,6 @@ func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
|
|||||||
config.BaseURL = baseURL
|
config.BaseURL = baseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect or use configured provider
|
|
||||||
providerName := provider.ProviderName(chatConfig.Provider)
|
providerName := provider.ProviderName(chatConfig.Provider)
|
||||||
if providerName == "" {
|
if providerName == "" {
|
||||||
providerName = provider.DetectProvider(chatConfig.BaseURL)
|
providerName = provider.DetectProvider(chatConfig.BaseURL)
|
||||||
@@ -53,20 +52,23 @@ func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertMessages 转换消息格式为OpenAI格式
|
// SetRequestCustomizer 设置请求自定义器
|
||||||
func (c *RemoteAPIChat) convertMessages(messages []Message) []openai.ChatCompletionMessage {
|
func (c *RemoteAPIChat) SetRequestCustomizer(customizer func(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool)) {
|
||||||
|
c.requestCustomizer = customizer
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvertMessages 转换消息格式为 OpenAI 格式(导出供子类使用)
|
||||||
|
func (c *RemoteAPIChat) ConvertMessages(messages []Message) []openai.ChatCompletionMessage {
|
||||||
openaiMessages := make([]openai.ChatCompletionMessage, 0, len(messages))
|
openaiMessages := make([]openai.ChatCompletionMessage, 0, len(messages))
|
||||||
for _, msg := range messages {
|
for _, msg := range messages {
|
||||||
openaiMsg := openai.ChatCompletionMessage{
|
openaiMsg := openai.ChatCompletionMessage{
|
||||||
Role: msg.Role,
|
Role: msg.Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理内容:对于 assistant 角色,内容可能为空(当有 tool_calls 时)
|
|
||||||
if msg.Content != "" {
|
if msg.Content != "" {
|
||||||
openaiMsg.Content = msg.Content
|
openaiMsg.Content = msg.Content
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 tool calls(assistant 角色)
|
|
||||||
if len(msg.ToolCalls) > 0 {
|
if len(msg.ToolCalls) > 0 {
|
||||||
openaiMsg.ToolCalls = make([]openai.ToolCall, 0, len(msg.ToolCalls))
|
openaiMsg.ToolCalls = make([]openai.ToolCall, 0, len(msg.ToolCalls))
|
||||||
for _, tc := range msg.ToolCalls {
|
for _, tc := range msg.ToolCalls {
|
||||||
@@ -82,7 +84,6 @@ func (c *RemoteAPIChat) convertMessages(messages []Message) []openai.ChatComplet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 tool 角色消息(工具返回结果)
|
|
||||||
if msg.Role == "tool" {
|
if msg.Role == "tool" {
|
||||||
openaiMsg.ToolCallID = msg.ToolCallID
|
openaiMsg.ToolCallID = msg.ToolCallID
|
||||||
openaiMsg.Name = msg.Name
|
openaiMsg.Name = msg.Name
|
||||||
@@ -93,44 +94,14 @@ func (c *RemoteAPIChat) convertMessages(messages []Message) []openai.ChatComplet
|
|||||||
return openaiMessages
|
return openaiMessages
|
||||||
}
|
}
|
||||||
|
|
||||||
// isQwenModel 检查是否为 qwen 模型
|
// BuildChatCompletionRequest 构建标准聊天请求参数(导出供子类使用)
|
||||||
func (c *RemoteAPIChat) isAliyunQwen3Model() bool {
|
func (c *RemoteAPIChat) BuildChatCompletionRequest(messages []Message, opts *ChatOptions, isStream bool) openai.ChatCompletionRequest {
|
||||||
return c.provider == provider.ProviderAliyun && provider.IsQwen3Model(c.modelName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// isDeepSeekModel 检查是否为 DeepSeek 模型
|
|
||||||
func (c *RemoteAPIChat) isDeepSeekModel() bool {
|
|
||||||
return provider.IsDeepSeekModel(c.modelName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildQwenChatCompletionRequest 构建 qwen 模型的聊天请求参数
|
|
||||||
func (c *RemoteAPIChat) buildQwenChatCompletionRequest(messages []Message,
|
|
||||||
opts *ChatOptions, isStream bool,
|
|
||||||
) QwenChatCompletionRequest {
|
|
||||||
req := QwenChatCompletionRequest{
|
|
||||||
ChatCompletionRequest: c.buildChatCompletionRequest(messages, opts, isStream),
|
|
||||||
}
|
|
||||||
|
|
||||||
// 对于 qwen 模型,在非流式调用中强制设置 enable_thinking: false
|
|
||||||
if !isStream {
|
|
||||||
enableThinking := false
|
|
||||||
req.EnableThinking = &enableThinking
|
|
||||||
}
|
|
||||||
return req
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildChatCompletionRequest 构建聊天请求参数
|
|
||||||
func (c *RemoteAPIChat) buildChatCompletionRequest(messages []Message,
|
|
||||||
opts *ChatOptions, isStream bool,
|
|
||||||
) openai.ChatCompletionRequest {
|
|
||||||
req := openai.ChatCompletionRequest{
|
req := openai.ChatCompletionRequest{
|
||||||
Model: c.modelName,
|
Model: c.modelName,
|
||||||
Messages: c.convertMessages(messages),
|
Messages: c.ConvertMessages(messages),
|
||||||
Stream: isStream,
|
Stream: isStream,
|
||||||
}
|
}
|
||||||
thinking := false
|
|
||||||
|
|
||||||
// 添加可选参数
|
|
||||||
if opts != nil {
|
if opts != nil {
|
||||||
if opts.Temperature > 0 {
|
if opts.Temperature > 0 {
|
||||||
req.Temperature = float32(opts.Temperature)
|
req.Temperature = float32(opts.Temperature)
|
||||||
@@ -150,11 +121,8 @@ func (c *RemoteAPIChat) buildChatCompletionRequest(messages []Message,
|
|||||||
if opts.PresencePenalty > 0 {
|
if opts.PresencePenalty > 0 {
|
||||||
req.PresencePenalty = float32(opts.PresencePenalty)
|
req.PresencePenalty = float32(opts.PresencePenalty)
|
||||||
}
|
}
|
||||||
if opts.Thinking != nil {
|
|
||||||
thinking = *opts.Thinking
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理 Tools(函数定义)
|
// 处理 Tools
|
||||||
if len(opts.Tools) > 0 {
|
if len(opts.Tools) > 0 {
|
||||||
req.Tools = make([]openai.Tool, 0, len(opts.Tools))
|
req.Tools = make([]openai.Tool, 0, len(opts.Tools))
|
||||||
for _, tool := range opts.Tools {
|
for _, tool := range opts.Tools {
|
||||||
@@ -166,39 +134,24 @@ func (c *RemoteAPIChat) buildChatCompletionRequest(messages []Message,
|
|||||||
Description: tool.Function.Description,
|
Description: tool.Function.Description,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// 转换 Parameters (map[string]interface{} -> JSON Schema)
|
|
||||||
if tool.Function.Parameters != nil {
|
if tool.Function.Parameters != nil {
|
||||||
// Parameters 已经是 JSON Schema 格式的 map,直接使用
|
|
||||||
openaiTool.Function.Parameters = tool.Function.Parameters
|
openaiTool.Function.Parameters = tool.Function.Parameters
|
||||||
}
|
}
|
||||||
req.Tools = append(req.Tools, openaiTool)
|
req.Tools = append(req.Tools, openaiTool)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 ToolChoice
|
// 处理 ToolChoice(标准实现)
|
||||||
// ToolChoice 可以是字符串或 ToolChoice 对象
|
|
||||||
// 对于 "auto", "none", "required" 直接使用字符串
|
|
||||||
// 对于特定工具名称,使用 ToolChoice 对象
|
|
||||||
// 注意:某些模型(如 DeepSeek)不支持 tool_choice,需要跳过设置
|
|
||||||
if opts.ToolChoice != "" {
|
if opts.ToolChoice != "" {
|
||||||
// DeepSeek 模型不支持 tool_choice,跳过设置(默认行为会自动使用工具)
|
switch opts.ToolChoice {
|
||||||
if c.isDeepSeekModel() {
|
case "none", "required", "auto":
|
||||||
// 对于 DeepSeek,不设置 tool_choice,让 API 使用默认行为
|
req.ToolChoice = opts.ToolChoice
|
||||||
// 如果有 tools,DeepSeek 会自动使用
|
default:
|
||||||
logger.Infof(context.Background(), "deepseek model, skip tool_choice")
|
req.ToolChoice = openai.ToolChoice{
|
||||||
} else {
|
Type: "function",
|
||||||
switch opts.ToolChoice {
|
Function: openai.ToolFunction{
|
||||||
case "none", "required", "auto":
|
Name: opts.ToolChoice,
|
||||||
// 直接使用字符串
|
},
|
||||||
req.ToolChoice = opts.ToolChoice
|
|
||||||
default:
|
|
||||||
// 特定工具名称,使用 ToolChoice 对象
|
|
||||||
req.ToolChoice = openai.ToolChoice{
|
|
||||||
Type: "function",
|
|
||||||
Function: openai.ToolFunction{
|
|
||||||
Name: opts.ToolChoice,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,50 +164,80 @@ func (c *RemoteAPIChat) buildChatCompletionRequest(messages []Message,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatTemplateKwargs is only supported by custom backends like vLLM.
|
|
||||||
// Official APIs (OpenAI, Aliyun, Zhipu, etc.) do not support this parameter
|
|
||||||
// and will return 400 Bad Request if it's included.
|
|
||||||
if c.provider == provider.ProviderGeneric {
|
|
||||||
req.ChatTemplateKwargs = map[string]interface{}{
|
|
||||||
"enable_thinking": thinking,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log LLM request for debugging
|
|
||||||
if jsonData, err := json.MarshalIndent(req, "", " "); err == nil {
|
|
||||||
logger.Infof(context.Background(), "[LLM Request] model=%s, stream=%v, request:\n%s", c.modelName, isStream, string(jsonData))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log tools/functions separately for clarity
|
|
||||||
if len(req.Tools) > 0 {
|
|
||||||
toolNames := make([]string, 0, len(req.Tools))
|
|
||||||
for _, tool := range req.Tools {
|
|
||||||
toolNames = append(toolNames, tool.Function.Name)
|
|
||||||
}
|
|
||||||
logger.Infof(context.Background(), "[LLM Request] tools_count=%d, tool_names=%v", len(req.Tools), toolNames)
|
|
||||||
}
|
|
||||||
|
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logRequest 记录请求日志
|
||||||
|
func (c *RemoteAPIChat) logRequest(ctx context.Context, req any, isStream bool) {
|
||||||
|
if jsonData, err := json.MarshalIndent(req, "", " "); err == nil {
|
||||||
|
logger.Infof(ctx, "[LLM Request] model=%s, stream=%v, request:\n%s", c.modelName, isStream, string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Chat 进行非流式聊天
|
// Chat 进行非流式聊天
|
||||||
func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *ChatOptions) (*types.ChatResponse, error) {
|
func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *ChatOptions) (*types.ChatResponse, error) {
|
||||||
// 如果是 qwen 模型,使用自定义请求
|
req := c.BuildChatCompletionRequest(messages, opts, false)
|
||||||
if c.isAliyunQwen3Model() {
|
|
||||||
return c.chatWithQwen(ctx, messages, opts)
|
// 检查是否需要自定义请求
|
||||||
|
if c.requestCustomizer != nil {
|
||||||
|
customReq, useRawHTTP := c.requestCustomizer(&req, opts, false)
|
||||||
|
if useRawHTTP && customReq != nil {
|
||||||
|
return c.chatWithRawHTTP(ctx, customReq)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建请求参数
|
c.logRequest(ctx, req, false)
|
||||||
req := c.buildChatCompletionRequest(messages, opts, false)
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
resp, err := c.client.CreateChatCompletion(ctx, req)
|
resp, err := c.client.CreateChatCompletion(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create chat completion: %w", err)
|
return nil, fmt.Errorf("create chat completion: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c.parseCompletionResponse(&resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// chatWithRawHTTP 使用原始 HTTP 请求进行聊天(供自定义请求使用)
|
||||||
|
func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, customReq any) (*types.ChatResponse, error) {
|
||||||
|
jsonData, err := json.Marshal(customReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "[LLM Request] model=%s, raw HTTP request:\n%s", c.modelName, string(jsonData))
|
||||||
|
|
||||||
|
endpoint := c.baseURL + "/chat/completions"
|
||||||
|
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
httpReq.Header.Set("Content-Type", "application/json")
|
||||||
|
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(httpReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var chatResp openai.ChatCompletionResponse
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&chatResp); err != nil {
|
||||||
|
return nil, fmt.Errorf("decode response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseCompletionResponse(&chatResp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseCompletionResponse 解析非流式响应
|
||||||
|
func (c *RemoteAPIChat) parseCompletionResponse(resp *openai.ChatCompletionResponse) (*types.ChatResponse, error) {
|
||||||
if len(resp.Choices) == 0 {
|
if len(resp.Choices) == 0 {
|
||||||
return nil, fmt.Errorf("no response from OpenAI")
|
return nil, fmt.Errorf("no response from API")
|
||||||
}
|
}
|
||||||
|
|
||||||
choice := resp.Choices[0]
|
choice := resp.Choices[0]
|
||||||
@@ -272,91 +255,6 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转换 Tool Calls
|
|
||||||
if len(choice.Message.ToolCalls) > 0 {
|
|
||||||
response.ToolCalls = make([]types.LLMToolCall, 0, len(choice.Message.ToolCalls))
|
|
||||||
for _, tc := range choice.Message.ToolCalls {
|
|
||||||
response.ToolCalls = append(response.ToolCalls, types.LLMToolCall{
|
|
||||||
ID: tc.ID,
|
|
||||||
Type: string(tc.Type),
|
|
||||||
Function: types.FunctionCall{
|
|
||||||
Name: tc.Function.Name,
|
|
||||||
Arguments: tc.Function.Arguments,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// chatWithQwen 使用自定义请求处理 qwen 模型
|
|
||||||
func (c *RemoteAPIChat) chatWithQwen(
|
|
||||||
ctx context.Context,
|
|
||||||
messages []Message,
|
|
||||||
opts *ChatOptions,
|
|
||||||
) (*types.ChatResponse, error) {
|
|
||||||
// 构建 qwen 请求参数
|
|
||||||
req := c.buildQwenChatCompletionRequest(messages, opts, false)
|
|
||||||
|
|
||||||
// 序列化请求
|
|
||||||
jsonData, err := json.Marshal(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("marshal request: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建 URL
|
|
||||||
endpoint := c.baseURL + "/chat/completions"
|
|
||||||
|
|
||||||
// 创建 HTTP 请求
|
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("create request: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置请求头
|
|
||||||
httpReq.Header.Set("Content-Type", "application/json")
|
|
||||||
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(httpReq)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("send request: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// 检查响应状态
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("API request failed with status: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析响应
|
|
||||||
var chatResp openai.ChatCompletionResponse
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&chatResp); err != nil {
|
|
||||||
return nil, fmt.Errorf("decode response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(chatResp.Choices) == 0 {
|
|
||||||
return nil, fmt.Errorf("no response from API")
|
|
||||||
}
|
|
||||||
|
|
||||||
choice := chatResp.Choices[0]
|
|
||||||
response := &types.ChatResponse{
|
|
||||||
Content: choice.Message.Content,
|
|
||||||
FinishReason: string(choice.FinishReason),
|
|
||||||
Usage: struct {
|
|
||||||
PromptTokens int `json:"prompt_tokens"`
|
|
||||||
CompletionTokens int `json:"completion_tokens"`
|
|
||||||
TotalTokens int `json:"total_tokens"`
|
|
||||||
}{
|
|
||||||
PromptTokens: chatResp.Usage.PromptTokens,
|
|
||||||
CompletionTokens: chatResp.Usage.CompletionTokens,
|
|
||||||
TotalTokens: chatResp.Usage.TotalTokens,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// 转换 Tool Calls
|
|
||||||
if len(choice.Message.ToolCalls) > 0 {
|
if len(choice.Message.ToolCalls) > 0 {
|
||||||
response.ToolCalls = make([]types.LLMToolCall, 0, len(choice.Message.ToolCalls))
|
response.ToolCalls = make([]types.LLMToolCall, 0, len(choice.Message.ToolCalls))
|
||||||
for _, tc := range choice.Message.ToolCalls {
|
for _, tc := range choice.Message.ToolCalls {
|
||||||
@@ -375,162 +273,295 @@ func (c *RemoteAPIChat) chatWithQwen(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ChatStream 进行流式聊天
|
// ChatStream 进行流式聊天
|
||||||
func (c *RemoteAPIChat) ChatStream(ctx context.Context,
|
func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
|
||||||
messages []Message, opts *ChatOptions,
|
req := c.BuildChatCompletionRequest(messages, opts, true)
|
||||||
) (<-chan types.StreamResponse, error) {
|
|
||||||
// 构建请求参数
|
// 检查是否需要自定义请求
|
||||||
req := c.buildChatCompletionRequest(messages, opts, true)
|
if c.requestCustomizer != nil {
|
||||||
|
customReq, useRawHTTP := c.requestCustomizer(&req, opts, true)
|
||||||
|
if useRawHTTP && customReq != nil {
|
||||||
|
return c.chatStreamWithRawHTTP(ctx, customReq)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logRequest(ctx, req, true)
|
||||||
|
|
||||||
// 创建流式响应通道
|
|
||||||
streamChan := make(chan types.StreamResponse)
|
streamChan := make(chan types.StreamResponse)
|
||||||
|
|
||||||
// 启动流式请求
|
|
||||||
stream, err := c.client.CreateChatCompletionStream(ctx, req)
|
stream, err := c.client.CreateChatCompletionStream(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
close(streamChan)
|
close(streamChan)
|
||||||
return nil, fmt.Errorf("create chat completion stream: %w", err)
|
return nil, fmt.Errorf("create chat completion stream: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 在后台处理流式响应
|
go c.processStream(ctx, stream, streamChan)
|
||||||
go func() {
|
|
||||||
defer close(streamChan)
|
|
||||||
defer stream.Close()
|
|
||||||
|
|
||||||
toolCallMap := make(map[int]*types.LLMToolCall)
|
|
||||||
lastFunctionName := make(map[int]string)
|
|
||||||
nameNotified := make(map[int]bool)
|
|
||||||
|
|
||||||
buildOrderedToolCalls := func() []types.LLMToolCall {
|
|
||||||
if len(toolCallMap) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
result := make([]types.LLMToolCall, 0, len(toolCallMap))
|
|
||||||
for i := 0; i < len(toolCallMap); i++ {
|
|
||||||
if tc, ok := toolCallMap[i]; ok && tc != nil {
|
|
||||||
result = append(result, *tc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(result) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
response, err := stream.Recv()
|
|
||||||
if err != nil {
|
|
||||||
// Check if it's a normal end of stream (io.EOF)
|
|
||||||
if err.Error() == "EOF" {
|
|
||||||
// Normal end of stream, send final response with collected tool calls
|
|
||||||
streamChan <- types.StreamResponse{
|
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
|
||||||
Content: "",
|
|
||||||
Done: true,
|
|
||||||
ToolCalls: buildOrderedToolCalls(),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Actual error, send error response
|
|
||||||
streamChan <- types.StreamResponse{
|
|
||||||
ResponseType: types.ResponseTypeError,
|
|
||||||
Content: err.Error(),
|
|
||||||
Done: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(response.Choices) > 0 {
|
|
||||||
delta := response.Choices[0].Delta
|
|
||||||
isDone := string(response.Choices[0].FinishReason) != ""
|
|
||||||
|
|
||||||
// 收集 tool calls(流式响应中 tool calls 可能分多次返回)
|
|
||||||
if len(delta.ToolCalls) > 0 {
|
|
||||||
for _, tc := range delta.ToolCalls {
|
|
||||||
// 检查是否已经存在该 tool call(通过 index)
|
|
||||||
var toolCallIndex int
|
|
||||||
if tc.Index != nil {
|
|
||||||
toolCallIndex = *tc.Index
|
|
||||||
}
|
|
||||||
toolCallEntry, exists := toolCallMap[toolCallIndex]
|
|
||||||
if !exists || toolCallEntry == nil {
|
|
||||||
toolCallEntry = &types.LLMToolCall{
|
|
||||||
Type: string(tc.Type),
|
|
||||||
Function: types.FunctionCall{
|
|
||||||
Name: "",
|
|
||||||
Arguments: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
toolCallMap[toolCallIndex] = toolCallEntry
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新 ID、类型
|
|
||||||
if tc.ID != "" {
|
|
||||||
toolCallEntry.ID = tc.ID
|
|
||||||
}
|
|
||||||
if tc.Type != "" {
|
|
||||||
toolCallEntry.Type = string(tc.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 累积函数名称(可能分多次返回)
|
|
||||||
if tc.Function.Name != "" {
|
|
||||||
toolCallEntry.Function.Name += tc.Function.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
// 累积参数(可能为部分 JSON)
|
|
||||||
argsUpdated := false
|
|
||||||
if tc.Function.Arguments != "" {
|
|
||||||
toolCallEntry.Function.Arguments += tc.Function.Arguments
|
|
||||||
argsUpdated = true
|
|
||||||
}
|
|
||||||
|
|
||||||
currName := toolCallEntry.Function.Name
|
|
||||||
if currName != "" &&
|
|
||||||
currName == lastFunctionName[toolCallIndex] &&
|
|
||||||
argsUpdated &&
|
|
||||||
!nameNotified[toolCallIndex] &&
|
|
||||||
toolCallEntry.ID != "" {
|
|
||||||
streamChan <- types.StreamResponse{
|
|
||||||
ResponseType: types.ResponseTypeToolCall,
|
|
||||||
Content: "",
|
|
||||||
Done: false,
|
|
||||||
Data: map[string]interface{}{
|
|
||||||
"tool_name": currName,
|
|
||||||
"tool_call_id": toolCallEntry.ID,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
nameNotified[toolCallIndex] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
lastFunctionName[toolCallIndex] = currName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发送内容块
|
|
||||||
if delta.Content != "" {
|
|
||||||
streamChan <- types.StreamResponse{
|
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
|
||||||
Content: delta.Content,
|
|
||||||
Done: isDone,
|
|
||||||
ToolCalls: buildOrderedToolCalls(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果是最后一次响应,确保发送包含所有 tool calls 的响应
|
|
||||||
if isDone && len(toolCallMap) > 0 {
|
|
||||||
streamChan <- types.StreamResponse{
|
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
|
||||||
Content: "",
|
|
||||||
Done: true,
|
|
||||||
ToolCalls: buildOrderedToolCalls(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return streamChan, nil
|
return streamChan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// chatStreamWithRawHTTP 使用原始 HTTP 请求进行流式聊天
|
||||||
|
func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, customReq any) (<-chan types.StreamResponse, error) {
|
||||||
|
jsonData, err := json.Marshal(customReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "[LLM Stream] model=%s, raw HTTP request:\n%s", c.modelName, string(jsonData))
|
||||||
|
|
||||||
|
endpoint := c.baseURL + "/chat/completions"
|
||||||
|
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
httpReq.Header.Set("Content-Type", "application/json")
|
||||||
|
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||||
|
httpReq.Header.Set("Accept", "text/event-stream")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(httpReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
streamChan := make(chan types.StreamResponse)
|
||||||
|
|
||||||
|
go c.processRawHTTPStream(ctx, resp, streamChan)
|
||||||
|
|
||||||
|
return streamChan, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processStream 处理 OpenAI SDK 流式响应
|
||||||
|
func (c *RemoteAPIChat) processStream(ctx context.Context, stream *openai.ChatCompletionStream, streamChan chan types.StreamResponse) {
|
||||||
|
defer close(streamChan)
|
||||||
|
defer stream.Close()
|
||||||
|
|
||||||
|
state := newStreamState()
|
||||||
|
|
||||||
|
for {
|
||||||
|
response, err := stream.Recv()
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == "EOF" {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
|
Content: "",
|
||||||
|
Done: true,
|
||||||
|
ToolCalls: state.buildOrderedToolCalls(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeError,
|
||||||
|
Content: err.Error(),
|
||||||
|
Done: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(response.Choices) > 0 {
|
||||||
|
c.processStreamDelta(ctx, &response.Choices[0], state, streamChan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// processRawHTTPStream 处理原始 HTTP 流式响应
|
||||||
|
func (c *RemoteAPIChat) processRawHTTPStream(ctx context.Context, resp *http.Response, streamChan chan types.StreamResponse) {
|
||||||
|
defer close(streamChan)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
state := newStreamState()
|
||||||
|
reader := NewSSEReader(resp.Body)
|
||||||
|
|
||||||
|
for {
|
||||||
|
event, err := reader.ReadEvent()
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() != "EOF" {
|
||||||
|
logger.Errorf(ctx, "Stream read error: %v", err)
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeError,
|
||||||
|
Content: err.Error(),
|
||||||
|
Done: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if event == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Done {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
|
Content: "",
|
||||||
|
Done: true,
|
||||||
|
ToolCalls: state.buildOrderedToolCalls(),
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.Data == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var streamResp openai.ChatCompletionStreamResponse
|
||||||
|
if err := json.Unmarshal(event.Data, &streamResp); err != nil {
|
||||||
|
logger.Errorf(ctx, "Failed to parse stream response: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(streamResp.Choices) > 0 {
|
||||||
|
c.processStreamDelta(ctx, &streamResp.Choices[0], state, streamChan)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// streamState 流式处理状态
|
||||||
|
type streamState struct {
|
||||||
|
toolCallMap map[int]*types.LLMToolCall
|
||||||
|
lastFunctionName map[int]string
|
||||||
|
nameNotified map[int]bool
|
||||||
|
hasThinking bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStreamState() *streamState {
|
||||||
|
return &streamState{
|
||||||
|
toolCallMap: make(map[int]*types.LLMToolCall),
|
||||||
|
lastFunctionName: make(map[int]string),
|
||||||
|
nameNotified: make(map[int]bool),
|
||||||
|
hasThinking: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *streamState) buildOrderedToolCalls() []types.LLMToolCall {
|
||||||
|
if len(s.toolCallMap) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
result := make([]types.LLMToolCall, 0, len(s.toolCallMap))
|
||||||
|
for i := 0; i < len(s.toolCallMap); i++ {
|
||||||
|
if tc, ok := s.toolCallMap[i]; ok && tc != nil {
|
||||||
|
result = append(result, *tc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// processStreamDelta 处理流式响应的单个 delta
|
||||||
|
func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.ChatCompletionStreamChoice, state *streamState, streamChan chan types.StreamResponse) {
|
||||||
|
delta := choice.Delta
|
||||||
|
isDone := string(choice.FinishReason) != ""
|
||||||
|
|
||||||
|
// 处理 tool calls
|
||||||
|
if len(delta.ToolCalls) > 0 {
|
||||||
|
c.processToolCallsDelta(delta.ToolCalls, state, streamChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送思考内容(ReasoningContent,支持 DeepSeek 等模型)
|
||||||
|
if delta.ReasoningContent != "" {
|
||||||
|
state.hasThinking = true
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeThinking,
|
||||||
|
Content: delta.ReasoningContent,
|
||||||
|
Done: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送回答内容
|
||||||
|
if delta.Content != "" {
|
||||||
|
// If we had thinking content and this is the first answer chunk,
|
||||||
|
// send a thinking done event first
|
||||||
|
if state.hasThinking {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeThinking,
|
||||||
|
Content: "",
|
||||||
|
Done: true,
|
||||||
|
}
|
||||||
|
state.hasThinking = false // Only send once
|
||||||
|
}
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
|
Content: delta.Content,
|
||||||
|
Done: isDone,
|
||||||
|
ToolCalls: state.buildOrderedToolCalls(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if isDone && len(state.toolCallMap) > 0 {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
|
Content: "",
|
||||||
|
Done: true,
|
||||||
|
ToolCalls: state.buildOrderedToolCalls(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// processToolCallsDelta 处理 tool calls 的增量更新
|
||||||
|
func (c *RemoteAPIChat) processToolCallsDelta(toolCalls []openai.ToolCall, state *streamState, streamChan chan types.StreamResponse) {
|
||||||
|
for _, tc := range toolCalls {
|
||||||
|
var toolCallIndex int
|
||||||
|
if tc.Index != nil {
|
||||||
|
toolCallIndex = *tc.Index
|
||||||
|
}
|
||||||
|
toolCallEntry, exists := state.toolCallMap[toolCallIndex]
|
||||||
|
if !exists || toolCallEntry == nil {
|
||||||
|
toolCallEntry = &types.LLMToolCall{
|
||||||
|
Type: string(tc.Type),
|
||||||
|
Function: types.FunctionCall{
|
||||||
|
Name: "",
|
||||||
|
Arguments: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
state.toolCallMap[toolCallIndex] = toolCallEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.ID != "" {
|
||||||
|
toolCallEntry.ID = tc.ID
|
||||||
|
}
|
||||||
|
if tc.Type != "" {
|
||||||
|
toolCallEntry.Type = string(tc.Type)
|
||||||
|
}
|
||||||
|
if tc.Function.Name != "" {
|
||||||
|
toolCallEntry.Function.Name += tc.Function.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
argsUpdated := false
|
||||||
|
if tc.Function.Arguments != "" {
|
||||||
|
toolCallEntry.Function.Arguments += tc.Function.Arguments
|
||||||
|
argsUpdated = true
|
||||||
|
}
|
||||||
|
|
||||||
|
currName := toolCallEntry.Function.Name
|
||||||
|
if currName != "" &&
|
||||||
|
currName == state.lastFunctionName[toolCallIndex] &&
|
||||||
|
argsUpdated &&
|
||||||
|
!state.nameNotified[toolCallIndex] &&
|
||||||
|
toolCallEntry.ID != "" {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeToolCall,
|
||||||
|
Content: "",
|
||||||
|
Done: false,
|
||||||
|
Data: map[string]interface{}{
|
||||||
|
"tool_name": currName,
|
||||||
|
"tool_call_id": toolCallEntry.ID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
state.nameNotified[toolCallIndex] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
state.lastFunctionName[toolCallIndex] = currName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetModelName 获取模型名称
|
// GetModelName 获取模型名称
|
||||||
func (c *RemoteAPIChat) GetModelName() string {
|
func (c *RemoteAPIChat) GetModelName() string {
|
||||||
return c.modelName
|
return c.modelName
|
||||||
@@ -540,3 +571,18 @@ func (c *RemoteAPIChat) GetModelName() string {
|
|||||||
func (c *RemoteAPIChat) GetModelID() string {
|
func (c *RemoteAPIChat) GetModelID() string {
|
||||||
return c.modelID
|
return c.modelID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProvider 获取 provider 名称
|
||||||
|
func (c *RemoteAPIChat) GetProvider() provider.ProviderName {
|
||||||
|
return c.provider
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBaseURL 获取 baseURL
|
||||||
|
func (c *RemoteAPIChat) GetBaseURL() string {
|
||||||
|
return c.baseURL
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAPIKey 获取 apiKey
|
||||||
|
func (c *RemoteAPIChat) GetAPIKey() string {
|
||||||
|
return c.apiKey
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SSEEvent 表示一个 Server-Sent Events 事件
|
||||||
|
type SSEEvent struct {
|
||||||
|
Data []byte
|
||||||
|
Done bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSEReader 用于读取 SSE 流
|
||||||
|
type SSEReader struct {
|
||||||
|
scanner *bufio.Scanner
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSSEReader 创建 SSE 读取器
|
||||||
|
func NewSSEReader(reader io.Reader) *SSEReader {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
// 设置更大的缓冲区以处理长行(思维链内容可能很长)
|
||||||
|
buf := make([]byte, 1024*1024)
|
||||||
|
scanner.Buffer(buf, 1024*1024)
|
||||||
|
return &SSEReader{scanner: scanner}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadEvent 读取下一个 SSE 事件
|
||||||
|
func (r *SSEReader) ReadEvent() (*SSEEvent, error) {
|
||||||
|
for r.scanner.Scan() {
|
||||||
|
line := r.scanner.Text()
|
||||||
|
|
||||||
|
// 空行,跳过
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否为结束标记
|
||||||
|
if line == "data: [DONE]" {
|
||||||
|
return &SSEEvent{Done: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析 data 行
|
||||||
|
if strings.HasPrefix(line, "data: ") {
|
||||||
|
jsonStr := line[6:]
|
||||||
|
return &SSEEvent{Data: []byte(jsonStr)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他行(如 event:, id: 等)跳过
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("EOF")
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// LKEAPBaseURL 腾讯云知识引擎原子能力 (LKEAP) 兼容 OpenAI 协议的 BaseURL
|
||||||
|
LKEAPBaseURL = "https://api.lkeap.cloud.tencent.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LKEAPProvider 实现腾讯云 LKEAP 的 Provider 接口
|
||||||
|
// 支持 DeepSeek-R1, DeepSeek-V3 系列模型,具备思维链能力
|
||||||
|
type LKEAPProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&LKEAPProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 LKEAP provider 的元数据
|
||||||
|
func (p *LKEAPProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderLKEAP,
|
||||||
|
DisplayName: "腾讯云 LKEAP",
|
||||||
|
Description: "DeepSeek-R1, DeepSeek-V3 系列模型,支持思维链",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: LKEAPBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 LKEAP provider 配置
|
||||||
|
func (p *LKEAPProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for LKEAP provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLKEAPDeepSeekV3Model 检查是否为 DeepSeek V3.x 系列模型
|
||||||
|
// V3.x 系列支持通过 Thinking 参数控制思维链开关
|
||||||
|
func IsLKEAPDeepSeekV3Model(modelName string) bool {
|
||||||
|
return strings.Contains(strings.ToLower(modelName), "deepseek-v3")
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLKEAPDeepSeekR1Model 检查是否为 DeepSeek R1 系列模型
|
||||||
|
// R1 系列默认开启思维链
|
||||||
|
func IsLKEAPDeepSeekR1Model(modelName string) bool {
|
||||||
|
return strings.Contains(strings.ToLower(modelName), "deepseek-r1")
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLKEAPThinkingModel 检查是否为支持思维链的 LKEAP 模型
|
||||||
|
func IsLKEAPThinkingModel(modelName string) bool {
|
||||||
|
return IsLKEAPDeepSeekR1Model(modelName) || IsLKEAPDeepSeekV3Model(modelName)
|
||||||
|
}
|
||||||
@@ -51,6 +51,8 @@ const (
|
|||||||
ProviderQiniu ProviderName = "qiniu"
|
ProviderQiniu ProviderName = "qiniu"
|
||||||
// 美团 LongCat AI
|
// 美团 LongCat AI
|
||||||
ProviderLongCat ProviderName = "longcat"
|
ProviderLongCat ProviderName = "longcat"
|
||||||
|
// 腾讯云 LKEAP (知识引擎原子能力)
|
||||||
|
ProviderLKEAP ProviderName = "lkeap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AllProviders 返回所有注册的提供者名称
|
// AllProviders 返回所有注册的提供者名称
|
||||||
@@ -74,6 +76,7 @@ func AllProviders() []ProviderName {
|
|||||||
ProviderJina,
|
ProviderJina,
|
||||||
ProviderMimo,
|
ProviderMimo,
|
||||||
ProviderLongCat,
|
ProviderLongCat,
|
||||||
|
ProviderLKEAP,
|
||||||
ProviderGPUStack,
|
ProviderGPUStack,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,6 +241,8 @@ func DetectProvider(baseURL string) ProviderName {
|
|||||||
return ProviderQianfan
|
return ProviderQianfan
|
||||||
case containsAny(baseURL, "longcat.chat"):
|
case containsAny(baseURL, "longcat.chat"):
|
||||||
return ProviderLongCat
|
return ProviderLongCat
|
||||||
|
case containsAny(baseURL, "lkeap.cloud.tencent.com", "api.lkeap"):
|
||||||
|
return ProviderLKEAP
|
||||||
default:
|
default:
|
||||||
return ProviderGeneric
|
return ProviderGeneric
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ type AgentConfig struct {
|
|||||||
// MCP service selection
|
// MCP service selection
|
||||||
MCPSelectionMode string `json:"mcp_selection_mode"` // MCP selection mode: "all", "selected", "none"
|
MCPSelectionMode string `json:"mcp_selection_mode"` // MCP selection mode: "all", "selected", "none"
|
||||||
MCPServices []string `json:"mcp_services"` // Selected MCP service IDs (when mode is "selected")
|
MCPServices []string `json:"mcp_services"` // Selected MCP service IDs (when mode is "selected")
|
||||||
|
// Whether to enable thinking mode (for models that support extended thinking)
|
||||||
|
Thinking *bool `json:"thinking"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionAgentConfig represents session-level agent configuration
|
// SessionAgentConfig represents session-level agent configuration
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ type CustomAgentConfig struct {
|
|||||||
Temperature float64 `yaml:"temperature" json:"temperature"`
|
Temperature float64 `yaml:"temperature" json:"temperature"`
|
||||||
// Maximum completion tokens (only for normal mode)
|
// Maximum completion tokens (only for normal mode)
|
||||||
MaxCompletionTokens int `yaml:"max_completion_tokens" json:"max_completion_tokens"`
|
MaxCompletionTokens int `yaml:"max_completion_tokens" json:"max_completion_tokens"`
|
||||||
|
// Whether to enable thinking mode (for models that support extended thinking)
|
||||||
|
Thinking *bool `yaml:"thinking" json:"thinking"`
|
||||||
|
|
||||||
// ===== Agent Mode Settings =====
|
// ===== Agent Mode Settings =====
|
||||||
// Maximum iterations for ReAct loop (only for agent type)
|
// Maximum iterations for ReAct loop (only for agent type)
|
||||||
|
|||||||
Reference in New Issue
Block a user