feat(thinkingControl): implement thinking mode configuration in model editor

- Added a new thinking mode control field in the ModelEditorDialog, allowing users to specify how the agent's thinking mode is formatted for API requests.
- Introduced localization strings for thinking mode labels and descriptions in English, Korean, Russian, and Chinese, enhancing user experience across multiple languages.
- Updated the model form data structure to include thinking control settings, ensuring proper handling during model edits and saves.
- Implemented utility functions for resolving and defaulting thinking control values based on provider and model, improving backend integration.
- Added tests for the new thinking control functionality to ensure reliability and correctness in handling user configurations.
This commit is contained in:
wizardchen
2026-06-05 14:42:08 +08:00
committed by lyingbug
parent 063809896a
commit e2a41ecf0a
26 changed files with 2075 additions and 1206 deletions
+202 -23
View File
@@ -324,6 +324,31 @@
<span class="form-desc form-desc--inline">{{ $t('model.editor.supportsVisionDesc') }}</span>
</div>
</div>
<!-- Chat + 远程 API:思考模式参数格式 -->
<div v-if="showThinkingControlField" class="form-item">
<label class="form-label">{{ $t('model.editor.thinkingControlLabel') }}</label>
<t-select
v-model="formData.thinkingControl"
:key="`thinking-${formData.id}-${formData.thinkingControl}`"
:popup-props="{ overlayClassName: 'thinking-control-select-popup' }"
@change="onThinkingControlManualPick"
>
<t-option
v-for="opt in thinkingControlOptions"
:key="opt.value"
:value="opt.value"
:label="opt.label"
:show-overflow-tooltip="false"
>
<div class="thinking-control-option">
<span class="thinking-control-option__title">{{ opt.label }}</span>
<span class="thinking-control-option__hint">{{ opt.hint }}</span>
</div>
</t-option>
</t-select>
<p class="form-desc">{{ $t('model.editor.thinkingControlDesc') }}</p>
</div>
</section>
</t-form>
@@ -342,6 +367,11 @@ import {
} from '@/api/model'
import { useI18n } from 'vue-i18n'
import { useUIStore } from '@/stores/ui'
import {
defaultThinkingControl,
resolveThinkingControl,
type ThinkingControlValue,
} from '@/utils/thinkingControl'
import SettingDrawer from '@/components/settings/SettingDrawer.vue'
import CredentialResource, {
type CredentialFieldDef,
@@ -367,6 +397,8 @@ interface ModelFormData {
interfaceType?: 'ollama' | 'openai'
isDefault: boolean
supportsVision?: boolean
/** extra_config.thinking_control — how agent thinking on/off maps to API fields. */
thinkingControl?: string
// 自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)
customHeaders?: CustomHeaderItem[]
/** LKEAP Rerank:腾讯云 SecretKey(创建时写入 app_secret) */
@@ -564,6 +596,51 @@ const dialogVisible = computed({
const isEdit = computed(() => !!props.modelData)
const showThinkingControlField = computed(() =>
props.modelType === 'chat' && formData.value.source === 'remote',
)
const resolvedThinkingControl = (): ThinkingControlValue =>
defaultThinkingControl(
formData.value.provider || '',
formData.value.modelName || '',
)
/** 用户是否手动改过思考参数格式(改过则不再自动覆盖,直到换服务商) */
const thinkingControlManual = ref(false)
/** 正在从 modelData 灌入表单,忽略厂商/来源控件的程序化 change 副作用 */
const hydratingForm = ref(false)
const onThinkingControlManualPick = () => {
thinkingControlManual.value = true
}
const syncThinkingControlToForm = (force = false) => {
if (!showThinkingControlField.value) return
if (!force && !isEdit.value && thinkingControlManual.value) return
formData.value.thinkingControl = resolvedThinkingControl()
}
const applyThinkingControlFromModelData = () => {
if (!props.modelData || props.modelType !== 'chat' || formData.value.source !== 'remote') return
thinkingControlManual.value = !!props.modelData.thinkingControl
formData.value.thinkingControl = resolveThinkingControl(
props.modelData.thinkingControl,
formData.value.provider || props.modelData.provider || '',
formData.value.modelName || props.modelData.modelName || '',
)
}
const thinkingControlOptions = computed(() => {
const keys = ['none', 'chatTemplateKwargs', 'enableThinking', 'thinkingType'] as const
const values = ['none', 'chat_template_kwargs', 'enable_thinking', 'thinking_type'] as const
return keys.map((key, i) => ({
value: values[i],
label: t(`model.editor.thinkingControl.${key}.label`),
hint: t(`model.editor.thinkingControl.${key}.hint`),
}))
})
// Header icon for the SettingDrawer — uses the same TDesign icon name table
// as the model card list, so the drawer's leading badge visually matches the
// card the user just clicked on.
@@ -697,6 +774,7 @@ const formData = ref<ModelFormData>({
interfaceType: 'ollama',
isDefault: false,
supportsVision: false,
thinkingControl: defaultThinkingControl('generic', ''),
customHeaders: [],
appSecret: '',
lkeapRegion: 'ap-guangzhou',
@@ -838,39 +916,53 @@ watch(() => props.visible, (val) => {
const currentId = props.modelData?.id ?? null
if (props.modelData) {
// 编辑:始终用最新的 modelData 覆盖。apiKey field is left blank — in
// edit mode the credential is owned by the <CredentialResource> card,
// not by this form's apiKey field.
formData.value = {
...props.modelData,
apiKey: '',
customHeaders: Array.isArray(props.modelData.customHeaders)
? props.modelData.customHeaders.map(h => ({ key: h.key, value: h.value }))
: []
hydratingForm.value = true
try {
if (props.modelData) {
// 编辑:始终用最新的 modelData 覆盖。apiKey field is left blank — in
// edit mode the credential is owned by the <CredentialResource> card,
// not by this form's apiKey field.
formData.value = {
...props.modelData,
apiKey: '',
customHeaders: Array.isArray(props.modelData.customHeaders)
? props.modelData.customHeaders.map(h => ({ key: h.key, value: h.value }))
: [],
}
applyThinkingControlFromModelData()
} else if (lastOpenedModelId.value !== null || !formData.value.id) {
// 上次是编辑某个模型,或第一次新增 → 重置成空白
resetForm()
}
} else if (lastOpenedModelId.value !== null || !formData.value.id) {
// 上次是编辑某个模型,或第一次新增 → 重置成空白
resetForm()
}
// 否则:连续两次"新增"打开(中间是点遮罩/ESC 关闭的)→ 保留上次填写
// 否则:连续两次"新增"打开(中间是点遮罩/ESC 关闭的)→ 保留上次填写
lastOpenedModelId.value = currentId
lastOpenedModelId.value = currentId
// ReRank 模型强制使用 remote 来源(Ollama 不支持 ReRank)
if (props.modelType === 'rerank') {
formData.value.source = 'remote'
}
// ReRank 模型强制使用 remote 来源(Ollama 不支持 ReRank)
if (props.modelType === 'rerank') {
formData.value.source = 'remote'
}
// 如果当前 provider 是 WeKnoraCloud,检查凭证状态
if (formData.value.provider === 'weknoracloud') {
checkWkcCredentialStatus()
// 如果当前 provider 是 WeKnoraCloud,检查凭证状态
if (formData.value.provider === 'weknoracloud') {
checkWkcCredentialStatus()
}
if (showThinkingControlField.value && !isEdit.value) {
thinkingControlManual.value = false
syncThinkingControlToForm(true)
}
} finally {
nextTick(() => {
hydratingForm.value = false
})
}
}
})
// 重置表单
const resetForm = () => {
thinkingControlManual.value = false
formData.value = {
id: generateId(),
name: '', // 保留字段但不使用,保存时用 modelName
@@ -884,6 +976,7 @@ const resetForm = () => {
interfaceType: undefined,
isDefault: false,
supportsVision: false,
thinkingControl: defaultThinkingControl('generic', ''),
customHeaders: [],
appSecret: '',
lkeapRegion: 'ap-guangzhou',
@@ -920,8 +1013,43 @@ const handleProviderChange = (value: string) => {
if (value === 'weknoracloud') {
checkWkcCredentialStatus()
}
if (hydratingForm.value) return
if (props.modelType !== 'chat' || formData.value.source !== 'remote') return
if (!isEdit.value) {
thinkingControlManual.value = false
syncThinkingControlToForm(true)
return
}
// 编辑时仅用户主动换厂商才跟随默认
thinkingControlManual.value = false
syncThinkingControlToForm(true)
}
watch(
() => [formData.value.source, formData.value.provider, formData.value.modelName] as const,
([source, provider, modelName], [prevSource, prevProvider, prevModelName]) => {
if (hydratingForm.value || isEdit.value) return
if (props.modelType !== 'chat' || source !== 'remote') return
if (source === prevSource && provider === prevProvider && modelName === prevModelName) return
const providerChanged = provider !== prevProvider
if (providerChanged) {
thinkingControlManual.value = false
syncThinkingControlToForm(true)
return
}
if (!thinkingControlManual.value) {
syncThinkingControlToForm(true)
return
}
const prevDefault = defaultThinkingControl(prevProvider || '', prevModelName || '')
if (formData.value.thinkingControl === prevDefault) {
syncThinkingControlToForm(true)
}
},
)
// 监听来源变化,重置校验状态(已合并到下面的 watch)
// 生成唯一ID
@@ -1388,6 +1516,16 @@ watch(() => formData.value.source, () => {
downloading.value = false
downloadProgress.value = 0
currentDownloadModel.value = ''
if (
!hydratingForm.value
&& !isEdit.value
&& formData.value.source === 'remote'
&& props.modelType === 'chat'
) {
thinkingControlManual.value = false
syncThinkingControlToForm(true)
}
})
// 监听模型名称变化,清理维度检测状态
@@ -1871,6 +2009,14 @@ const handleCancel = () => {
&--inline {
margin: 0;
}
&--recommend {
color: var(--td-brand-color);
}
&--warn {
color: var(--td-warning-color);
}
}
.vision-toggle {
@@ -1969,6 +2115,39 @@ const handleCancel = () => {
<!-- 非 scoped 样式:t-select popup 渲染到 body 下,scoped 样式无法覆盖 -->
<style lang="less">
.thinking-control-select-popup {
min-width: 22rem;
max-width: min(28rem, calc(100vw - 2rem));
padding: 4px;
.t-select-option {
height: auto !important;
padding: 8px 10px;
border-radius: 6px;
margin: 2px 0;
white-space: normal;
}
}
.thinking-control-option {
display: flex;
flex-direction: column;
gap: 2px;
line-height: 1.35;
min-width: 0;
&__title {
font-size: 13px;
color: var(--td-text-color-primary);
}
&__hint {
font-size: 12px;
color: var(--td-text-color-placeholder);
word-break: break-word;
}
}
.provider-select-popup {
// 容器留点呼吸:避免选项贴着 popup 圆角
padding: 4px;
+21
View File
@@ -3325,6 +3325,27 @@ export default {
remoteDimensionDetected: 'Detected vector dimension: {value}',
supportsVisionLabel: 'Supports Vision / Multimodal',
supportsVisionDesc: 'Whether the model accepts image and multimodal input',
thinkingControlLabel: 'Thinking mode request format',
thinkingControlDesc:
'Controls how the agent’s “Thinking mode” on/off switch is written to the API. We pre-select based on vendor/model when possible; change it to match your API docs. With “Do not send”, the agent Thinking mode switch has no effect.',
thinkingControl: {
none: {
label: 'Do not send thinking fields',
hint: 'Agent “Thinking mode” switch has no effect; thinking parameters are not sent in requests',
},
chatTemplateKwargs: {
label: 'chat_template_kwargs',
hint: 'Custom OpenAI-compatible gateways, NVIDIA NIM, vLLM / local Qwen',
},
enableThinking: {
label: 'enable_thinking',
hint: 'Alibaba DashScope: qwen3, qwen-plus, qwen-max, qwen-turbo',
},
thinkingType: {
label: 'thinking.type',
hint: 'Volcengine Ark; Tencent LKEAP (DeepSeek V3, etc.; default for LKEAP; use “Do not send” for R1)',
},
},
dimensionHint: 'Model selected. Click "Detect Dimension" to fetch the vector dimension automatically.',
loadModelListFailed: 'Failed to load model list',
listRefreshed: 'List refreshed',
+21
View File
@@ -2542,6 +2542,27 @@ export default {
remoteDimensionDetected: "감지된 벡터 차원: {value}",
supportsVisionLabel: "비전/멀티모달 지원",
supportsVisionDesc: "모델의 이미지 등 멀티모달 입력 지원 여부",
thinkingControlLabel: "사고 모드 매개변수 형식",
thinkingControlDesc:
"에이전트 「사고 모드」 켜기/끄기 시 API에 어떻게 기록할지 결정합니다. 벤더/모델에 따라 미리 선택되며, 실제 API와 다르면 문서에 맞게 수정하세요. 「전송 안 함」을 선택하면 에이전트 「사고 모드」 스위치가 효과가 없습니다.",
thinkingControl: {
none: {
label: "사고 매개변수 전송 안 함",
hint: "에이전트 「사고 모드」 스위치가 효과 없음, 요청에 사고 관련 매개변수를 보내지 않음",
},
chatTemplateKwargs: {
label: "chat_template_kwargs",
hint: "사용자 정의 OpenAI 호환, NVIDIA NIM, vLLM / 로컬 Qwen 배포",
},
enableThinking: {
label: "enable_thinking",
hint: "Alibaba DashScope: qwen3, qwen-plus, qwen-max, qwen-turbo",
},
thinkingType: {
label: "thinking.type",
hint: 'Volcengine Ark; Tencent LKEAP (DeepSeek V3 등, LKEAP 기본값; R1은 「전송 안 함」)',
},
},
dimensionHint:
'모델이 선택되었습니다. "차원 감지" 버튼을 클릭하여 벡터 차원을 자동으로 가져옵니다',
loadModelListFailed: "모델 목록 로드 실패",
+21
View File
@@ -2259,6 +2259,27 @@ export default {
remoteDimensionDetected: 'Обнаружена размерность: {value}',
supportsVisionLabel: 'Поддержка визуального / мультимодального ввода',
supportsVisionDesc: 'Поддерживает ли модель изображения и другой мультимодальный ввод',
thinkingControlLabel: 'Формат параметров режима размышления',
thinkingControlDesc:
'Определяет, как переключатель «Режим размышления» агента записывается в API. При возможности выбирается по поставщику/модели; при несоответствии измените по документации API. При выборе «Не отправлять» переключатель «Режим размышления» агента не действует.',
thinkingControl: {
none: {
label: 'Не отправлять параметры размышления',
hint: 'Переключатель «Режим размышления» агента не действует; параметры размышления не отправляются в запросе',
},
chatTemplateKwargs: {
label: 'chat_template_kwargs',
hint: 'Пользовательские OpenAI-совместимые шлюзы, NVIDIA NIM, vLLM / локальный Qwen',
},
enableThinking: {
label: 'enable_thinking',
hint: 'Alibaba DashScope: qwen3, qwen-plus, qwen-max, qwen-turbo',
},
thinkingType: {
label: 'thinking.type',
hint: 'Volcengine Ark; Tencent LKEAP (DeepSeek V3 и др.; по умолчанию для LKEAP; для R1 — «Не отправлять»)',
},
},
dimensionHint: 'Модель выбрана. Нажмите «Определить размерность», чтобы автоматически получить значение.',
loadModelListFailed: 'Не удалось загрузить список моделей',
listRefreshed: 'Список обновлён',
+21
View File
@@ -2542,6 +2542,27 @@ export default {
remoteDimensionDetected: "检测到向量维度:{value}",
supportsVisionLabel: "支持视觉/多模态",
supportsVisionDesc: "模型是否支持图片等多模态输入",
thinkingControlLabel: "思考模式参数格式",
thinkingControlDesc:
"决定智能体「思考模式」开/关时如何写入 API。已尝试按厂商/模型预选,若与实际情况不符请按 API 文档手动修改;选「不写入」时,智能体「思考模式」开关不生效。",
thinkingControl: {
none: {
label: "不写入思考参数",
hint: "智能体「思考模式」开关不生效,不会在请求中写入思考相关参数",
},
chatTemplateKwargs: {
label: "chat_template_kwargs",
hint: "自定义 OpenAI 兼容、NVIDIA NIM、vLLM / 本地 Qwen 部署",
},
enableThinking: {
label: "enable_thinking",
hint: "阿里云 DashScope:qwen3、qwen-plus、qwen-max、qwen-turbo",
},
thinkingType: {
label: "thinking.type",
hint: "火山引擎 Ark;腾讯云 LKEAP(DeepSeek V3 等,选 LKEAP 时默认此项;R1 请改「不写入」)",
},
},
dimensionHint: '模型已选择,点击"检测维度"按钮自动获取向量维度',
loadModelListFailed: "加载模型列表失败",
listRefreshed: "列表已刷新",
@@ -0,0 +1,36 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { defaultThinkingControl } from './thinkingControl.ts'
// Cases mirror internal/models/chat/provider_test.go TestResolveProvider thinking defaults.
test('defaultThinkingControl matches backend provider adapters', () => {
const cases: Array<[string, string, ReturnType<typeof defaultThinkingControl>]> = [
['generic', 'anything', 'chat_template_kwargs'],
['nvidia', 'anything', 'chat_template_kwargs'],
['volcengine', 'doubao', 'thinking_type'],
['aliyun', 'qwen3-32b', 'enable_thinking'],
['aliyun', 'qwen-plus', 'enable_thinking'],
['aliyun', 'gpt-4', 'none'],
['lkeap', '', 'thinking_type'],
['lkeap', 'deepseek-v3.1', 'thinking_type'],
['lkeap', 'deepseek-r1', 'none'],
['openai', 'gpt-4o', 'none'],
['openai', 'gpt-5', 'none'],
['azure_openai', 'gpt-4', 'none'],
['deepseek', 'deepseek-chat', 'none'],
['zhipu', 'glm-4', 'none'],
['gemini', 'gemini-2.0', 'none'],
['siliconflow', 'qwen3-8b', 'none'],
['hunyuan', 'hunyuan-turbo', 'none'],
['moonshot', 'moonshot-v1-8k', 'none'],
['weknoracloud', 'anything', 'none'],
]
for (const [provider, model, want] of cases) {
assert.equal(
defaultThinkingControl(provider, model),
want,
`${provider}/${model}`,
)
}
})
+71
View File
@@ -0,0 +1,71 @@
/** Mirrors backend internal/models/provider.IsQwenThinkingModel */
export function isQwenThinkingModel(modelName: string): boolean {
const lower = modelName.trim().toLowerCase()
return (
lower.startsWith('qwen3')
|| lower.startsWith('qwen-plus')
|| lower.startsWith('qwen-max')
|| lower.startsWith('qwen-turbo')
)
}
/** Mirrors backend internal/models/provider.IsLKEAPDeepSeekR1Model */
export function isLkeapDeepSeekR1Model(modelName: string): boolean {
return modelName.toLowerCase().includes('deepseek-r1')
}
export type ThinkingControlValue =
| 'none'
| 'chat_template_kwargs'
| 'enable_thinking'
| 'thinking_type'
const THINKING_CONTROL_VALUES: ThinkingControlValue[] = [
'none',
'chat_template_kwargs',
'enable_thinking',
'thinking_type',
]
/**
* Default thinking_control for provider+model.
* Must stay aligned with chat.resolveProvider(...).Thinking() in provider.go.
*/
export function defaultThinkingControl(
provider: string,
modelName = '',
): ThinkingControlValue {
const p = provider.trim().toLowerCase()
const model = modelName.trim()
switch (p) {
case 'aliyun':
return isQwenThinkingModel(model) ? 'enable_thinking' : 'none'
case 'lkeap':
// R1 系列后端不发 thinking 参数;其余(含未填模型名)按 LKEAP 的 thinking.type 格式预选
if (model && isLkeapDeepSeekR1Model(model)) return 'none'
return 'thinking_type'
case 'generic':
case 'nvidia':
return 'chat_template_kwargs'
case 'volcengine':
return 'thinking_type'
default:
// openai, azure_openai, anthropic, zhipu, deepseek, gemini, siliconflow,
// hunyuan, moonshot, openrouter, weknoracloud, … → baseProvider / noThinking
return 'none'
}
}
/** Resolve stored extra_config or fall back to the provider default. */
export function resolveThinkingControl(
saved: string | undefined,
provider: string,
modelName = '',
): ThinkingControlValue {
const v = saved?.trim().toLowerCase()
if (THINKING_CONTROL_VALUES.includes(v as ThinkingControlValue)) {
return v as ThinkingControlValue
}
return defaultThinkingControl(provider, modelName)
}
@@ -723,6 +723,17 @@
</div>
</div>
<!-- 思考模式(写入 config.thinking,映射到 LLM 请求参数) -->
<div class="setting-row">
<div class="setting-info">
<label>{{ $t('agent.editor.thinking') }}</label>
<p class="desc">{{ $t('agentEditor.desc.thinking') }}</p>
</div>
<div class="setting-control">
<t-switch v-model="thinkingEnabled" />
</div>
</div>
<!-- 最大迭代次数 -->
<div class="setting-row">
<div class="setting-info">
+17 -9
View File
@@ -162,6 +162,8 @@ function convertToLegacyFormat(model: ModelConfig) {
? Object.entries(model.parameters.custom_headers).map(([key, value]) => ({ key, value: String(value) }))
: [],
lkeapRegion: model.parameters.extra_config?.region || 'ap-guangzhou',
// 原始存库值,编辑弹窗内再 resolve(避免打开时被推断值覆盖)
thinkingControl: model.parameters.extra_config?.thinking_control,
_modelType: backendTypeToModelType[model.type] || 'chat' as ModelType,
// Preserve the credential metadata map so the editor dialog can render
// the "Configured" state without an extra round-trip.
@@ -375,14 +377,20 @@ const handleModelSave = async (modelData: any) => {
const trimmedAppSecret = (modelData.appSecret ?? '').trim()
const appSecretFields: { app_secret?: string } =
!editingModel.value && trimmedAppSecret ? { app_secret: trimmedAppSecret } : {}
const lkeapExtra =
modelData.provider === 'lkeap' && currentModelType.value === 'rerank'
? {
extra_config: {
region: (modelData.lkeapRegion || 'ap-guangzhou').trim(),
},
}
: {}
const extraConfig: Record<string, string> = {}
if (modelData.provider === 'lkeap' && currentModelType.value === 'rerank') {
extraConfig.region = (modelData.lkeapRegion || 'ap-guangzhou').trim()
}
if (
currentModelType.value === 'chat'
&& modelData.source === 'remote'
&& modelData.thinkingControl
) {
extraConfig.thinking_control = modelData.thinkingControl
}
const extraConfigFields = Object.keys(extraConfig).length > 0
? { extra_config: extraConfig }
: {}
const apiModelData: ModelConfig = {
name: modelData.modelName.trim(),
@@ -395,7 +403,7 @@ const handleModelSave = async (modelData: any) => {
...apiKeyFields,
...appSecretFields,
provider: modelData.provider || '',
...lkeapExtra,
...extraConfigFields,
...(Object.keys(customHeadersMap).length > 0 ? { custom_headers: customHeadersMap } : {}),
...(currentModelType.value === 'embedding' && modelData.dimension ? {
embedding_parameters: {
@@ -1685,3 +1685,4 @@ onMounted(loadAll)
}
</style>
+8 -3
View File
@@ -153,6 +153,7 @@ func (c *AnthropicChat) Chat(ctx context.Context, messages []Message, opts *Chat
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, chatResp.Content)
}
logUsage(ctx, c.modelName, &chatResp.Usage)
return chatResp, nil
}
@@ -167,7 +168,9 @@ func (c *AnthropicChat) Chat(ctx context.Context, messages []Message, opts *Chat
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
}
return c.parseResponse(&chatResp), nil
result := c.parseResponse(&chatResp)
logUsage(ctx, c.modelName, &result.Usage)
return result, nil
}
func (c *AnthropicChat) ChatStream(ctx context.Context, messages []Message, opts *ChatOptions) (<-chan types.StreamResponse, error) {
@@ -204,7 +207,7 @@ func (c *AnthropicChat) ChatStream(ctx context.Context, messages []Message, opts
}
streamChan := make(chan types.StreamResponse)
go processAnthropicStream(resp, streamChan)
go processAnthropicStream(ctx, c.modelName, resp, streamChan)
return streamChan, nil
}
@@ -382,7 +385,7 @@ func parseAnthropicSSE(reader io.Reader) (*types.ChatResponse, error) {
}, nil
}
func processAnthropicStream(resp *http.Response, streamChan chan types.StreamResponse) {
func processAnthropicStream(ctx context.Context, model string, resp *http.Response, streamChan chan types.StreamResponse) {
defer close(streamChan)
defer resp.Body.Close()
@@ -394,6 +397,7 @@ func processAnthropicStream(resp *http.Response, streamChan chan types.StreamRes
event, err := sseReader.ReadEvent()
if err != nil {
if err == io.EOF {
logUsage(ctx, model, usage)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
@@ -411,6 +415,7 @@ func processAnthropicStream(resp *http.Response, streamChan chan types.StreamRes
return
}
if event.Done {
logUsage(ctx, model, usage)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
+4 -24
View File
@@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/Tencent/WeKnora/internal/models/provider"
@@ -150,7 +149,9 @@ func NewChat(config *ChatConfig, ollamaService *ollama.OllamaService) (Chat, err
return wrapChatLangfuse(c, err)
}
// NewRemoteChat 根据 provider 创建远程聊天实例
// NewRemoteChat 根据 provider 创建远程聊天实例。
// Anthropic 走独立的 Messages 协议实现;其余 OpenAI 兼容供应商统一由
// RemoteAPIChat 处理,provider 特定行为在构造时通过 providerAdapter 解析。
func NewRemoteChat(config *ChatConfig) (Chat, error) {
providerName := provider.ProviderName(config.Provider)
if providerName == "" {
@@ -159,26 +160,5 @@ func NewRemoteChat(config *ChatConfig) (Chat, error) {
if providerName == provider.ProviderAnthropic {
return NewAnthropicChat(config)
}
remoteChat, err := NewRemoteAPIChat(config)
if err != nil {
return nil, err
}
// Look up provider-specific behavior from spec registry
if spec := findProviderSpec(providerName, config.ModelName); spec != nil {
if spec.RequestCustomizer != nil {
remoteChat.SetRequestCustomizer(spec.RequestCustomizer)
}
if spec.EndpointCustomizer != nil {
remoteChat.SetEndpointCustomizer(spec.EndpointCustomizer)
}
if spec.HeaderCustomizer != nil {
remoteChat.SetHeaderCustomizer(func(req *http.Request, body []byte) error {
return spec.HeaderCustomizer(remoteChat, req, body)
})
}
}
return remoteChat, nil
return NewRemoteAPIChat(config)
}
-252
View File
@@ -1,252 +0,0 @@
package chat
import (
"context"
"net/http"
"strings"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/provider"
modelutils "github.com/Tencent/WeKnora/internal/models/utils"
"github.com/google/uuid"
"github.com/sashabaranov/go-openai"
)
// ProviderSpec describes provider-specific behavior for chat completions.
// Each spec is registered with a ProviderName and optionally a model matcher.
type ProviderSpec struct {
Provider provider.ProviderName
// ModelMatcher: if non-nil, this spec only applies when the model name matches.
// Used for sub-provider routing (e.g. Qwen3 within Aliyun).
ModelMatcher func(modelName string) bool
// RequestCustomizer: provider-specific request modification.
RequestCustomizer func(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool)
// EndpointCustomizer: provider-specific endpoint URL override.
EndpointCustomizer func(baseURL string, modelID string, isStream bool) string
// HeaderCustomizer: provider-specific raw HTTP header customization.
HeaderCustomizer func(chat *RemoteAPIChat, req *http.Request, body []byte) error
}
// chatProviderSpecs is the ordered list of provider specs.
// Order matters: more specific specs (with ModelMatcher) should come before generic ones.
var chatProviderSpecs = []ProviderSpec{
// WeKnoraCloud
{
Provider: provider.ProviderWeKnoraCloud,
RequestCustomizer: weKnoraCloudRequestCustomizer,
EndpointCustomizer: func(baseURL string, _ string, _ bool) string {
return strings.TrimRight(baseURL, "/") + "/api/v1/chat/completions"
},
HeaderCustomizer: weKnoraCloudHeaderCustomizer,
},
// Aliyun Qwen Thinking Models (must be before generic Aliyun)
{
Provider: provider.ProviderAliyun,
ModelMatcher: func(name string) bool { return provider.IsQwenThinkingModel(name) },
RequestCustomizer: qwenThinkingRequestCustomizer,
},
// LKEAP
{
Provider: provider.ProviderLKEAP,
RequestCustomizer: lkeapRequestCustomizer,
},
// DeepSeek
{
Provider: provider.ProviderDeepSeek,
RequestCustomizer: deepseekRequestCustomizer,
},
// Generic (vLLM)
{
Provider: provider.ProviderGeneric,
RequestCustomizer: genericRequestCustomizer,
},
// Volcengine (火山引擎 Ark)
{
Provider: provider.ProviderVolcengine,
RequestCustomizer: volcengineRequestCustomizer,
},
// NVIDIA
{
Provider: provider.ProviderNvidia,
RequestCustomizer: genericRequestCustomizer,
},
}
// findProviderSpec finds the matching spec for the given provider and model name.
func findProviderSpec(providerName provider.ProviderName, modelName string) *ProviderSpec {
for i := range chatProviderSpecs {
spec := &chatProviderSpecs[i]
if spec.Provider != providerName {
continue
}
if spec.ModelMatcher != nil && !spec.ModelMatcher(modelName) {
continue
}
return spec
}
return nil
}
// --- Type definitions (moved from wrapper files) ---
// QwenChatCompletionRequest Qwen 模型的自定义请求结构体
type QwenChatCompletionRequest struct {
openai.ChatCompletionRequest
EnableThinking *bool `json:"enable_thinking,omitempty"`
}
// ThinkingConfig 思维链配置(LKEAP / Volcengine 等通用格式)
type ThinkingConfig struct {
Type string `json:"type"` // "enabled" 或 "disabled"
}
// ThinkingChatCompletionRequest 带 thinking 字段的自定义请求结构体
// 适用于 LKEAP、Volcengine 等使用 { "thinking": { "type": "enabled" } } 格式的 provider
type ThinkingChatCompletionRequest struct {
openai.ChatCompletionRequest
Thinking *ThinkingConfig `json:"thinking,omitempty"`
}
// --- Customizer functions ---
// weKnoraCloudRequestCustomizer 构造 WeKnoraCloud 请求。
// WeKnoraCloud 走 OpenAI 兼容格式,除了 MultiContent 需要降级为纯文本 Content 之外,
// 其他字段(tools / tool_choice / parallel_tool_calls / response_format / stream_options 等)直接透传,
// 以保证 function calling 等能力可用。
func weKnoraCloudRequestCustomizer(req *openai.ChatCompletionRequest, _ *ChatOptions, isStream bool) (any, bool) {
cloudReq := *req
cloudReq.Stream = isStream
cloudReq.Messages = convertToWeKnoraCloudMessagesFromOpenAI(req.Messages)
return cloudReq, true
}
func weKnoraCloudHeaderCustomizer(chat *RemoteAPIChat, req *http.Request, body []byte) error {
requestID := uuid.NewString()
headers := modelutils.Sign(chat.appID, chat.appSecret, requestID, string(body))
for k, v := range headers {
req.Header.Set(k, v)
}
return nil
}
// convertToWeKnoraCloudMessagesFromOpenAI 将 MultiContent 降级为纯文本,
// 其它字段(tool_calls / tool_call_id / name 等)完全保留,保证 tool 协议正常。
func convertToWeKnoraCloudMessagesFromOpenAI(messages []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
result := make([]openai.ChatCompletionMessage, 0, len(messages))
for _, m := range messages {
msg := m
if msg.Content == "" && len(msg.MultiContent) > 0 {
var textParts []string
for _, part := range msg.MultiContent {
if part.Type == openai.ChatMessagePartTypeText && part.Text != "" {
textParts = append(textParts, part.Text)
}
}
msg.Content = strings.Join(textParts, "\n")
msg.MultiContent = nil
}
result = append(result, msg)
}
return result
}
// qwenThinkingRequestCustomizer 自定义 Qwen 系列(阿里云)模型的思考请求
func qwenThinkingRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool,
) (any, bool) {
if !isStream {
// Qwen3 模型在非流式请求时需要显式禁用 thinking
qwenReq := QwenChatCompletionRequest{
ChatCompletionRequest: *req,
}
enableThinking := false
qwenReq.EnableThinking = &enableThinking
return qwenReq, true
}
// 流式请求:根据 opts.Thinking 启用思考
qwenReq := QwenChatCompletionRequest{
ChatCompletionRequest: *req,
}
thinking := false
if opts != nil && opts.Thinking != nil {
thinking = *opts.Thinking
}
qwenReq.EnableThinking = &thinking
// 必须返回 true 以使用 raw HTTP,否则 SDK 会过滤掉 enable_thinking 字段
return qwenReq, true
}
// lkeapRequestCustomizer 自定义 LKEAP 请求
// 仅对 DeepSeek V3.x 系列模型设置 thinking 参数;R1 系列默认开启思维链
// 参考:https://cloud.tencent.com/document/product/1772/115963
func lkeapRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
modelName := req.Model
if !strings.Contains(strings.ToLower(modelName), "deepseek-v3") || opts == nil || opts.Thinking == nil {
return nil, false
}
lkeapReq := ThinkingChatCompletionRequest{
ChatCompletionRequest: *req,
}
thinkingType := "disabled"
if *opts.Thinking {
thinkingType = "enabled"
}
lkeapReq.Thinking = &ThinkingConfig{Type: thinkingType}
return lkeapReq, true
}
// deepseekRequestCustomizer 自定义 DeepSeek 请求
// DeepSeek 模型不支持 tool_choice 参数,需要清除
func deepseekRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
if opts != nil && opts.ToolChoice != "" {
logger.Infof(context.Background(), "deepseek model, skip tool_choice")
req.ToolChoice = nil
}
return nil, false
}
// genericRequestCustomizer 自定义 Generic 请求
// Generic provider(如 vLLM)使用 ChatTemplateKwargs 传递 thinking 参数
func genericRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
thinking := false
if opts != nil && opts.Thinking != nil {
thinking = *opts.Thinking
}
req.ChatTemplateKwargs = map[string]interface{}{
"enable_thinking": thinking,
}
return req, true
}
// volcengineRequestCustomizer 自定义火山引擎请求
// 火山引擎使用 thinking 参数控制深度思考,格式同 LKEAP: { "type": "enabled"/"disabled" }
func volcengineRequestCustomizer(
req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool,
) (any, bool) {
if opts == nil || opts.Thinking == nil {
return nil, false
}
vcReq := ThinkingChatCompletionRequest{
ChatCompletionRequest: *req,
}
thinkingType := "disabled"
if *opts.Thinking {
thinkingType = "enabled"
}
vcReq.Thinking = &ThinkingConfig{Type: thinkingType}
return vcReq, true
}
+11 -24
View File
@@ -154,18 +154,17 @@ func (c *OllamaChat) Chat(ctx context.Context, messages []Message, opts *ChatOpt
return nil, fmt.Errorf("聊天请求失败: %w", err)
}
totalTokens := promptTokens + completionTokens
logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
c.modelName, promptTokens, completionTokens, totalTokens)
usage := types.TokenUsage{
PromptTokens: promptTokens,
CompletionTokens: completionTokens,
TotalTokens: promptTokens + completionTokens,
}
logUsage(ctx, c.modelName, &usage)
return &types.ChatResponse{
Content: responseContent,
ToolCalls: toolCalls,
Usage: types.TokenUsage{
PromptTokens: promptTokens,
CompletionTokens: completionTokens,
TotalTokens: totalTokens,
},
Usage: usage,
}, nil
}
@@ -193,27 +192,16 @@ func (c *OllamaChat) ChatStream(
go func() {
defer close(streamChan)
hasThinking := false
var thinking thinkingEmitter
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
// 发送思考内容(支持 Qwen3、DeepSeek 等推理模型)
if resp.Message.Thinking != "" {
hasThinking = true
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeThinking,
Content: resp.Message.Thinking,
Done: false,
}
thinking.emit(streamChan, resp.Message.Thinking)
}
if resp.Message.Content != "" {
// 思考阶段结束后,发送思考完成事件
if hasThinking {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeThinking,
Done: true,
}
hasThinking = false
}
thinking.finish(streamChan)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: resp.Message.Content,
@@ -266,9 +254,8 @@ func (c *OllamaChat) ChatStream(
CompletionTokens: resp.EvalCount,
TotalTokens: resp.PromptEvalCount + resp.EvalCount,
}
logger.Infof(ctx, "[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d",
c.modelName, usage.PromptTokens, usage.CompletionTokens, usage.TotalTokens)
}
logUsage(ctx, c.modelName, usage)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Done: true,
+183
View File
@@ -0,0 +1,183 @@
package chat
import (
"fmt"
"github.com/sashabaranov/go-openai"
)
// ConvertMessages 转换消息格式为 OpenAI 格式(导出供子类使用)
func (c *RemoteAPIChat) ConvertMessages(messages []Message) []openai.ChatCompletionMessage {
openaiMessages := make([]openai.ChatCompletionMessage, 0, len(messages))
for _, msg := range messages {
openaiMsg := openai.ChatCompletionMessage{
Role: msg.Role,
}
// 优先处理多内容消息(包含图片等)
if len(msg.MultiContent) > 0 {
openaiMsg.MultiContent = make([]openai.ChatMessagePart, 0, len(msg.MultiContent))
for _, part := range msg.MultiContent {
switch part.Type {
case "text":
openaiMsg.MultiContent = append(openaiMsg.MultiContent, openai.ChatMessagePart{
Type: openai.ChatMessagePartTypeText,
Text: part.Text,
})
case "image_url":
if part.ImageURL != nil {
openaiMsg.MultiContent = append(openaiMsg.MultiContent, openai.ChatMessagePart{
Type: openai.ChatMessagePartTypeImageURL,
ImageURL: &openai.ChatMessageImageURL{
URL: part.ImageURL.URL,
Detail: openai.ImageURLDetail(part.ImageURL.Detail),
},
})
}
}
}
} else if len(msg.Images) > 0 && msg.Role == "user" {
parts := make([]openai.ChatMessagePart, 0, len(msg.Images)+1)
for _, imgURL := range msg.Images {
resolved := resolveImageURLForLLM(imgURL)
parts = append(parts, openai.ChatMessagePart{
Type: openai.ChatMessagePartTypeImageURL,
ImageURL: &openai.ChatMessageImageURL{
URL: resolved,
Detail: openai.ImageURLDetailAuto,
},
})
}
parts = append(parts, openai.ChatMessagePart{
Type: openai.ChatMessagePartTypeText,
Text: msg.Content,
})
openaiMsg.MultiContent = parts
} else if msg.Content != "" {
openaiMsg.Content = msg.Content
}
if len(msg.ToolCalls) > 0 {
openaiMsg.ToolCalls = make([]openai.ToolCall, 0, len(msg.ToolCalls))
for _, tc := range msg.ToolCalls {
toolType := openai.ToolType(tc.Type)
openaiMsg.ToolCalls = append(openaiMsg.ToolCalls, openai.ToolCall{
ID: tc.ID,
Type: toolType,
Function: openai.FunctionCall{
Name: tc.Function.Name,
Arguments: tc.Function.Arguments,
},
})
}
}
if msg.Role == "tool" {
openaiMsg.ToolCallID = msg.ToolCallID
openaiMsg.Name = msg.Name
}
// Round-trip reasoning_content on assistant turns. MiMo and DeepSeek V3.2+
// thinking mode reject multi-turn requests where the prior assistant
// message lacks its reasoning_content with HTTP 400 ("The reasoning_content
// in the thinking mode must be passed back to the API."). Providers that
// don't recognize the field ignore it harmlessly.
if msg.Role == "assistant" && msg.ReasoningContent != "" {
openaiMsg.ReasoningContent = msg.ReasoningContent
}
openaiMessages = append(openaiMessages, openaiMsg)
}
return openaiMessages
}
// BuildChatCompletionRequest 构建标准聊天请求参数(导出供子类使用)。
//
// 这是一个不含任何 provider 特定逻辑的通用实现:所有采样参数(temperature /
// top_p / penalties)与 max_tokens 都按 opts 直接映射。供应商相关的特判
// (OpenAI o-series / GPT-5 改用 max_completion_tokens、Moonshot 固定温度等)
// 由对应的 providerAdapter.ShapeRequest 在事后施加,见 provider.go。
func (c *RemoteAPIChat) BuildChatCompletionRequest(
messages []Message, opts *ChatOptions, isStream bool,
) openai.ChatCompletionRequest {
req := openai.ChatCompletionRequest{
Model: c.modelName,
Messages: c.ConvertMessages(messages),
Stream: isStream,
}
if isStream {
req.StreamOptions = &openai.StreamOptions{IncludeUsage: true}
}
if opts == nil {
return req
}
req.Temperature = float32(opts.Temperature)
if opts.TopP > 0 {
req.TopP = float32(opts.TopP)
}
if opts.FrequencyPenalty > 0 {
req.FrequencyPenalty = float32(opts.FrequencyPenalty)
}
if opts.PresencePenalty > 0 {
req.PresencePenalty = float32(opts.PresencePenalty)
}
if opts.MaxTokens > 0 {
req.MaxTokens = opts.MaxTokens
}
if opts.MaxCompletionTokens > 0 {
req.MaxCompletionTokens = opts.MaxCompletionTokens
}
// 处理 Tools
if len(opts.Tools) > 0 {
req.Tools = make([]openai.Tool, 0, len(opts.Tools))
for _, tool := range opts.Tools {
toolType := openai.ToolType(tool.Type)
openaiTool := openai.Tool{
Type: toolType,
Function: &openai.FunctionDefinition{
Name: tool.Function.Name,
Description: tool.Function.Description,
},
}
if tool.Function.Parameters != nil {
openaiTool.Function.Parameters = tool.Function.Parameters
}
req.Tools = append(req.Tools, openaiTool)
}
}
// 处理 ParallelToolCalls
if opts.ParallelToolCalls != nil {
val := *opts.ParallelToolCalls
req.ParallelToolCalls = val
}
// 处理 ToolChoice(标准实现)
if opts.ToolChoice != "" {
switch opts.ToolChoice {
case "none", "required", "auto":
req.ToolChoice = opts.ToolChoice
default:
req.ToolChoice = openai.ToolChoice{
Type: "function",
Function: openai.ToolFunction{
Name: opts.ToolChoice,
},
}
}
}
if len(opts.Format) > 0 {
req.ResponseFormat = &openai.ChatCompletionResponseFormat{
Type: openai.ChatCompletionResponseFormatTypeJSONObject,
}
req.Messages[len(req.Messages)-1].Content += fmt.Sprintf("\nUse this JSON schema: %s", opts.Format)
}
return req
}
+539
View File
@@ -0,0 +1,539 @@
package chat
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/sashabaranov/go-openai"
)
// parseCompletionResponse 解析非流式响应
func (c *RemoteAPIChat) parseCompletionResponse(resp *openai.ChatCompletionResponse) (*types.ChatResponse, error) {
if len(resp.Choices) == 0 {
return nil, fmt.Errorf("no response from API")
}
choice := resp.Choices[0]
// 处理思考模型的输出:移除 <think></think> 标签包裹的思考过程
// 为设置了 Thinking=false 但模型仍返回思考内容的情况和部分不支持Thinking=false的思考模型(例如Miniax-M2.1)提供兜底策略
content := removeThinkingContent(choice.Message.Content)
response := &types.ChatResponse{
Content: content,
FinishReason: string(choice.FinishReason),
Usage: types.TokenUsage{
PromptTokens: resp.Usage.PromptTokens,
CompletionTokens: resp.Usage.CompletionTokens,
TotalTokens: resp.Usage.TotalTokens,
CachedTokens: cachedTokens(resp.Usage.PromptTokensDetails),
},
}
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
}
// removeThinkingContent 移除思考模型输出中的 <think></think> 思考过程
// 仅当内容以 <think> 开头时才处理
func removeThinkingContent(content string) string {
const thinkStartTag = "<think>"
const thinkEndTag = "</think>"
trimmed := strings.TrimSpace(content)
if !strings.HasPrefix(trimmed, thinkStartTag) {
return content
}
// 查找最后一个 </think> 标签(处理嵌套情况)
if lastEndIdx := strings.LastIndex(trimmed, thinkEndTag); lastEndIdx != -1 {
if result := strings.TrimSpace(trimmed[lastEndIdx+len(thinkEndTag):]); result != "" {
return result
}
return ""
}
return "" // 未找到 </think>,可能思考内容过长被截断,返回空字符串
}
// processStream 处理 OpenAI SDK 流式响应
func (c *RemoteAPIChat) processStream(
ctx context.Context,
stream *openai.ChatCompletionStream,
streamChan chan types.StreamResponse,
dumper *streamPacketDumper,
) {
defer close(streamChan)
defer stream.Close()
state := newStreamState()
for {
response, err := stream.Recv()
if err != nil {
if err == io.EOF {
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: toolCalls,
Usage: state.usage,
FinishReason: state.lastFinishReason,
}
} else {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeError,
Content: err.Error(),
Done: true,
}
}
return
}
if dumper != nil {
dumper.WritePacket(response)
}
if response.Usage != nil {
state.usage = &types.TokenUsage{
PromptTokens: response.Usage.PromptTokens,
CompletionTokens: response.Usage.CompletionTokens,
TotalTokens: response.Usage.TotalTokens,
CachedTokens: cachedTokens(response.Usage.PromptTokensDetails),
}
}
if len(response.Choices) > 0 {
c.processStreamDelta(ctx, &response.Choices[0], state, streamChan, response.Choices[0].Delta.ReasoningContent)
}
}
}
// processRawHTTPStream 处理原始 HTTP 流式响应
func (c *RemoteAPIChat) processRawHTTPStream(
ctx context.Context,
resp *http.Response,
streamChan chan types.StreamResponse,
dumper *streamPacketDumper,
) {
defer close(streamChan)
defer resp.Body.Close()
state := newStreamState()
reader := NewSSEReader(resp.Body)
for {
event, err := reader.ReadEvent()
if err != nil {
if err == io.EOF {
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: toolCalls,
Usage: state.usage,
}
} else {
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 {
logUsage(ctx, c.modelName, state.usage)
toolCalls := state.buildOrderedToolCalls()
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: toolCalls,
Usage: state.usage,
}
return
}
if event.Data == nil {
continue
}
if dumper != nil {
// 保留上游 SSE data 行的原始 JSON,不经过中间结构体裁剪。
raw := make([]byte, len(event.Data))
copy(raw, event.Data)
dumper.WritePacketRaw(raw)
}
// 使用局部结构体进行一次性解析,同时捕捉标准字段和 vLLM 的 reasoning 字段,避免性能损失
var streamResp struct {
openai.ChatCompletionStreamResponse
Choices []struct {
Index int `json:"index"`
Delta struct {
openai.ChatCompletionStreamChoiceDelta
Reasoning string `json:"reasoning,omitempty"`
} `json:"delta"`
FinishReason openai.FinishReason `json:"finish_reason"`
} `json:"choices"`
}
if err := json.Unmarshal(event.Data, &streamResp); err != nil {
logger.Errorf(ctx, "Failed to parse stream response: %v", err)
continue
}
if streamResp.Usage != nil {
state.usage = &types.TokenUsage{
PromptTokens: streamResp.Usage.PromptTokens,
CompletionTokens: streamResp.Usage.CompletionTokens,
TotalTokens: streamResp.Usage.TotalTokens,
CachedTokens: cachedTokens(streamResp.Usage.PromptTokensDetails),
}
}
if len(streamResp.Choices) > 0 {
choice := streamResp.Choices[0]
// 统一获取逻辑(支持标准和 vLLM 两种路径)
reasoning := choice.Delta.Reasoning
if reasoning == "" {
reasoning = choice.Delta.ReasoningContent
}
// 构造一个标准 SDK 兼容的 choice 对象传给下游,保证现有逻辑完全不动
sdkChoice := openai.ChatCompletionStreamChoice{
Index: choice.Index,
Delta: choice.Delta.ChatCompletionStreamChoiceDelta,
FinishReason: choice.FinishReason,
}
c.processStreamDelta(ctx, &sdkChoice, state, streamChan, reasoning)
}
}
}
// streamState 流式处理状态
type streamState struct {
thinkingEmitter
toolCallMap map[int]*types.LLMToolCall
lastFunctionName map[int]string
nameNotified map[int]bool
fieldExtractors map[int]*jsonFieldExtractor // per tool-call-index extractors for streaming field extraction
usage *types.TokenUsage // captured from the final stream chunk when include_usage is enabled
lastFinishReason string // last observed finish_reason for EOF handler fallback
// Diagnostic flags (fire-once) used to log earliest signals of tool_call
// presence/absence at the OpenAI-protocol level. These are independent of
// the higher-level ResponseTypeToolCall marker (which only fires once
// function name has stabilized) and let us distinguish between
// (A) no tool_calls field ever observed (true natural-stop), and
// (B) tool_calls field observed but marker not yet emitted.
firstToolCallSeen bool // true once any delta carried tool_calls
noToolCallStopLogged bool // true once we logged "stop without tool_calls"
firstContentSeen bool // true once delta.Content first appeared
firstReasoningSeen bool // true once reasoning_content first appeared
streamStartedAt time.Time
}
func newStreamState() *streamState {
return &streamState{
toolCallMap: make(map[int]*types.LLMToolCall),
lastFunctionName: make(map[int]string),
nameNotified: make(map[int]bool),
fieldExtractors: make(map[int]*jsonFieldExtractor),
streamStartedAt: time.Now(),
}
}
// elapsedMs returns the milliseconds elapsed since the stream state was
// initialized. Used to attach time-since-stream-start to fire-once diagnostic
// logs so a single grep can reveal the temporal layout of a single stream
// (TTFC / TTFT / first-tool-call / natural-stop confirmation, etc).
func (s *streamState) elapsedMs() int64 {
if s.streamStartedAt.IsZero() {
return 0
}
return time.Since(s.streamStartedAt).Milliseconds()
}
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,
reasoningContent string,
) {
delta := choice.Delta
isDone := string(choice.FinishReason) != ""
// Track finish_reason for EOF handler fallback
if isDone {
state.lastFinishReason = string(choice.FinishReason)
}
// 处理 tool calls
if len(delta.ToolCalls) > 0 {
c.processToolCallsDelta(ctx, delta.ToolCalls, state, streamChan)
}
// Earliest reliable "no tool_calls" signal at the OpenAI-protocol level:
// finish_reason=stop arrived AND we never observed a tool_calls field on
// any prior delta. Logged once per stream so callers can grep for the
// natural-stop entry point without waiting for the higher-level summary.
if isDone &&
string(choice.FinishReason) == "stop" &&
!state.firstToolCallSeen &&
!state.noToolCallStopLogged {
logger.Infof(ctx, "[LLM Stream] Natural-stop at OpenAI layer "+
"(finish=stop, tool_calls field never observed, thinking_seen=%t, "+
"first_content_seen=%t, elapsed_ms=%d)",
state.active, state.firstContentSeen, state.elapsedMs())
state.noToolCallStopLogged = true
}
// 发送思考内容(ReasoningContent,支持 DeepSeek 等模型)
if reasoningContent != "" {
// Earliest reasoning_content signal at the OpenAI-protocol level. Fired
// once per stream so we can distinguish "model emitted thinking before
// answer" vs "model never produced thinking" when triaging logs.
if !state.firstReasoningSeen {
state.firstReasoningSeen = true
logger.Infof(ctx, "[LLM Stream] First reasoning_content at OpenAI layer "+
"(len=%d, preview=%q, elapsed_ms=%d)",
len(reasoningContent), truncateForDebug(reasoningContent, 80), state.elapsedMs())
}
state.emit(streamChan, reasoningContent)
}
// 发送回答内容
if delta.Content != "" {
// Earliest delta.Content signal at the OpenAI-protocol level. Fired once
// per stream so we can measure TTFC (time-to-first-content) and tell
// "answer started before any tool_call" from "tool_call came first".
if !state.firstContentSeen {
state.firstContentSeen = true
logger.Infof(ctx, "[LLM Stream] First delta.Content at OpenAI layer "+
"(len=%d, preview=%q, tool_call_seen=%t, thinking_seen=%t, elapsed_ms=%d)",
len(delta.Content), truncateForDebug(delta.Content, 80),
state.firstToolCallSeen, state.firstReasoningSeen, state.elapsedMs())
}
// If we had thinking content and this is the first answer chunk,
// send a thinking done event first.
state.finish(streamChan)
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: delta.Content,
Done: isDone,
ToolCalls: state.buildOrderedToolCalls(),
FinishReason: string(choice.FinishReason),
}
}
if isDone && len(state.toolCallMap) > 0 {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Content: "",
Done: true,
ToolCalls: state.buildOrderedToolCalls(),
FinishReason: string(choice.FinishReason),
}
}
// Ensure thinking done is sent when stream finishes without any answer content
// (e.g., model only produced reasoning then hit finish_reason with empty content).
if isDone {
state.finish(streamChan)
}
// Catch-all: isDone but none of the above branches sent a response with
// FinishReason (empty content, no tool calls, no thinking). This prevents
// the finish_reason from being lost in the streaming pipeline.
if isDone && delta.Content == "" && len(state.toolCallMap) == 0 {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeAnswer,
Done: true,
FinishReason: string(choice.FinishReason),
}
}
}
// processToolCallsDelta 处理 tool calls 的增量更新
func (c *RemoteAPIChat) processToolCallsDelta(
ctx context.Context,
toolCalls []openai.ToolCall,
state *streamState,
streamChan chan types.StreamResponse,
) {
// Earliest signal at the OpenAI-protocol level that this stream will
// produce at least one tool call. Fires *before* the function name has
// stabilized, i.e. earlier than the higher-level ResponseTypeToolCall
// marker downstream consumers see. Useful for distinguishing
// "tool_calls field arrived but marker not yet emitted" from
// "tool_calls field truly absent" when triaging stream behavior.
if !state.firstToolCallSeen && len(toolCalls) > 0 {
state.firstToolCallSeen = true
var firstID, firstName string
for _, tc := range toolCalls {
if tc.ID != "" {
firstID = tc.ID
}
if tc.Function.Name != "" {
firstName = tc.Function.Name
}
if firstID != "" || firstName != "" {
break
}
}
logger.Infof(ctx, "[LLM Stream] First tool_calls delta at OpenAI layer "+
"(count=%d, first_id=%q, first_name=%q, "+
"first_content_seen=%t, thinking_seen=%t, elapsed_ms=%d)",
len(toolCalls), firstID, firstName,
state.firstContentSeen, state.firstReasoningSeen, state.elapsedMs())
}
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 != "" {
// 防御性校验:解决部分供应商(如vLLM Ascend等)在每个流 Chunk 中重复发送完整工具名的问题。
// 如果当前已存名字与新收到名字一致,则视为冗余重复,不进行叠加。
if toolCallEntry.Function.Name != 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
// Stream thinking tool's thought field as thinking-type chunks
if toolCallEntry.Function.Name == "thinking" && argsUpdated {
extractor, exists := state.fieldExtractors[toolCallIndex]
if !exists {
extractor = newJSONFieldExtractor("thought")
state.fieldExtractors[toolCallIndex] = extractor
}
thoughtChunk := extractor.Feed(tc.Function.Arguments)
if thoughtChunk != "" {
streamChan <- types.StreamResponse{
ResponseType: types.ResponseTypeThinking,
Content: thoughtChunk,
Done: false,
Data: map[string]interface{}{
"source": "thinking_tool",
"tool_call_id": toolCallEntry.ID,
},
}
}
}
}
}
// cachedTokens returns the cached prompt-token count from an OpenAI-compatible
// usage detail block, or zero when the provider did not report one. Some
// providers omit PromptTokensDetails entirely, so the nil guard is required.
//
// Note on provider semantics:
// - Implicit-cache providers (OpenAI, Azure OpenAI, DeepSeek, …) populate
// `cached_tokens` automatically whenever the prompt prefix matches a
// previous request — no caller opt-in is required.
// - Explicit-cache providers (Qwen on Aliyun, Anthropic Claude, …) only
// populate `cached_tokens` after the caller attaches `cache_control:
// {"type": "ephemeral"}` to the relevant message / content block. This
// helper still returns zero for those providers until that opt-in is
// applied upstream of the request.
func cachedTokens(d *openai.PromptTokensDetails) int {
if d == nil {
return 0
}
return d.CachedTokens
}
+247
View File
@@ -0,0 +1,247 @@
package chat
import (
"net/http"
"strings"
"github.com/Tencent/WeKnora/internal/models/provider"
modelutils "github.com/Tencent/WeKnora/internal/models/utils"
"github.com/google/uuid"
"github.com/sashabaranov/go-openai"
)
// authCreds carries the credentials a providerAdapter needs to authenticate a
// raw HTTP request. APIKey covers the common Bearer / api-key cases; AppID and
// AppSecret are only used by signing providers (WeKnoraCloud).
type authCreds struct {
APIKey string
AppID string
AppSecret string
}
// providerAdapter captures everything provider-specific about an
// OpenAI-compatible chat backend. Every method has a sensible default on
// baseProvider, so a new provider is added by embedding baseProvider and
// overriding only the one or two methods that actually differ.
type providerAdapter interface {
// Name is the provider this adapter handles.
Name() provider.ProviderName
// Matches reports whether this adapter applies to the given model name.
// Used for sub-provider routing (e.g. Qwen thinking models within Aliyun,
// reasoning models within OpenAI). Default: true.
Matches(model string) bool
// Thinking is how this provider encodes ChatOptions.Thinking. Default: none.
Thinking() ThinkingStrategy
// ShapeRequest applies in-place parameter quirks to the standard request
// (stripping unsupported fields, pinning temperature, …). Default: noop.
ShapeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool)
// TransformMessages rewrites the converted messages (e.g. downgrading
// multi-content to plain text). Default: identity.
TransformMessages(msgs []openai.ChatCompletionMessage) []openai.ChatCompletionMessage
// Endpoint overrides the request URL. Empty string means the standard
// "<baseURL>/chat/completions" handled by the caller. Default: "".
Endpoint(baseURL, modelID string, isStream bool) string
// Auth sets authentication headers on a raw HTTP request. Default: Bearer.
Auth(req *http.Request, creds authCreds, body []byte)
// ForceRawHTTP forces the raw HTTP path even when the body is standard
// (needed by providers that must sign the exact request bytes). Default: false.
ForceRawHTTP() bool
}
// baseProvider supplies the default behavior for every providerAdapter method.
// It is also the fallback returned by resolveProvider for unknown providers:
// Bearer auth, standard endpoint, no thinking, no request shaping.
type baseProvider struct{}
func (baseProvider) Name() provider.ProviderName { return "" }
func (baseProvider) Matches(string) bool { return true }
func (baseProvider) Thinking() ThinkingStrategy { return noThinking{} }
func (baseProvider) ShapeRequest(*openai.ChatCompletionRequest, *ChatOptions, bool) {}
func (baseProvider) TransformMessages(msgs []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
return msgs
}
func (baseProvider) Endpoint(string, string, bool) string { return "" }
func (baseProvider) Auth(req *http.Request, creds authCreds, _ []byte) {
req.Header.Set("Authorization", "Bearer "+creds.APIKey)
}
func (baseProvider) ForceRawHTTP() bool { return false }
// --- WeKnoraCloud: custom endpoint + request signing + multi-content downgrade ---
type weKnoraCloudProvider struct{ baseProvider }
func (weKnoraCloudProvider) Name() provider.ProviderName { return provider.ProviderWeKnoraCloud }
func (weKnoraCloudProvider) Endpoint(baseURL, _ string, _ bool) string {
return strings.TrimRight(baseURL, "/") + "/api/v1/chat/completions"
}
func (weKnoraCloudProvider) ForceRawHTTP() bool { return true }
func (weKnoraCloudProvider) Auth(req *http.Request, creds authCreds, body []byte) {
requestID := uuid.NewString()
headers := modelutils.Sign(creds.AppID, creds.AppSecret, requestID, string(body))
for k, v := range headers {
req.Header.Set(k, v)
}
}
// TransformMessages downgrades MultiContent to plain text while preserving
// tool_calls / tool_call_id / name so the function-calling protocol keeps working.
func (weKnoraCloudProvider) TransformMessages(messages []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
result := make([]openai.ChatCompletionMessage, 0, len(messages))
for _, m := range messages {
msg := m
if msg.Content == "" && len(msg.MultiContent) > 0 {
var textParts []string
for _, part := range msg.MultiContent {
if part.Type == openai.ChatMessagePartTypeText && part.Text != "" {
textParts = append(textParts, part.Text)
}
}
msg.Content = strings.Join(textParts, "\n")
msg.MultiContent = nil
}
result = append(result, msg)
}
return result
}
// --- Aliyun Qwen thinking models: enable_thinking (always sent, forced off non-stream) ---
type qwenThinkingProvider struct{ baseProvider }
func (qwenThinkingProvider) Name() provider.ProviderName { return provider.ProviderAliyun }
func (qwenThinkingProvider) Matches(model string) bool { return provider.IsQwenThinkingModel(model) }
func (qwenThinkingProvider) Thinking() ThinkingStrategy {
return enableThinking{alwaysSend: true, disableOnNonStream: true}
}
// --- LKEAP: thinking via { "thinking": { "type": ... } }, only for DeepSeek V3.x ---
// R1 series enables chain-of-thought by default and is left untouched (falls
// back to baseProvider). See https://cloud.tencent.com/document/product/1772/115963
type lkeapProvider struct{ baseProvider }
func (lkeapProvider) Name() provider.ProviderName { return provider.ProviderLKEAP }
func (lkeapProvider) Matches(model string) bool {
return strings.Contains(strings.ToLower(model), "deepseek-v3")
}
func (lkeapProvider) Thinking() ThinkingStrategy { return thinkingTypeField{} }
// --- DeepSeek: does not support tool_choice ---
type deepseekProvider struct{ baseProvider }
func (deepseekProvider) Name() provider.ProviderName { return provider.ProviderDeepSeek }
func (deepseekProvider) ShapeRequest(req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool) {
if opts != nil && opts.ToolChoice != "" {
req.ToolChoice = nil
}
}
// --- Generic (vLLM) / NVIDIA: thinking via chat_template_kwargs ---
type genericProvider struct{ baseProvider }
func (genericProvider) Name() provider.ProviderName { return provider.ProviderGeneric }
func (genericProvider) Thinking() ThinkingStrategy { return chatTemplateKwargs{} }
type nvidiaProvider struct{ baseProvider }
func (nvidiaProvider) Name() provider.ProviderName { return provider.ProviderNvidia }
func (nvidiaProvider) Thinking() ThinkingStrategy { return chatTemplateKwargs{} }
// --- Volcengine (火山引擎 Ark): thinking via { "thinking": { "type": ... } } ---
type volcengineProvider struct{ baseProvider }
func (volcengineProvider) Name() provider.ProviderName { return provider.ProviderVolcengine }
func (volcengineProvider) Thinking() ThinkingStrategy { return thinkingTypeField{} }
// --- Azure OpenAI: api-key auth (reasoning variant also strips sampling params) ---
type azureProvider struct{ baseProvider }
func (azureProvider) Name() provider.ProviderName { return provider.ProviderAzureOpenAI }
func (azureProvider) Auth(req *http.Request, creds authCreds, _ []byte) {
req.Header.Set("api-key", creds.APIKey)
}
type azureReasoningProvider struct{ azureProvider }
func (azureReasoningProvider) Matches(model string) bool {
return provider.IsOpenAIReasoningOrGPT5Model(model)
}
func (azureReasoningProvider) ShapeRequest(req *openai.ChatCompletionRequest, _ *ChatOptions, _ bool) {
shapeOpenAIReasoning(req)
}
// --- OpenAI reasoning / GPT-5: no sampling params, must use max_completion_tokens ---
type openAIReasoningProvider struct{ baseProvider }
func (openAIReasoningProvider) Name() provider.ProviderName { return provider.ProviderOpenAI }
func (openAIReasoningProvider) Matches(model string) bool {
return provider.IsOpenAIReasoningOrGPT5Model(model)
}
func (openAIReasoningProvider) ShapeRequest(req *openai.ChatCompletionRequest, _ *ChatOptions, _ bool) {
shapeOpenAIReasoning(req)
}
// --- Moonshot: v1 models accept only temperature=1 ---
type moonshotProvider struct{ baseProvider }
func (moonshotProvider) Name() provider.ProviderName { return provider.ProviderMoonshot }
func (moonshotProvider) Matches(model string) bool {
return provider.IsMoonshotFixedTempModel(model)
}
func (moonshotProvider) ShapeRequest(req *openai.ChatCompletionRequest, _ *ChatOptions, _ bool) {
// Pin temperature to 1 and drop the other sampling params, matching the
// pre-refactor behavior where these fields were never set for this model.
req.Temperature = 1
req.TopP = 0
req.FrequencyPenalty = 0
req.PresencePenalty = 0
}
// shapeOpenAIReasoning strips sampling params (unsupported by o-series / GPT-5)
// and migrates max_tokens to max_completion_tokens. See issue #1283.
func shapeOpenAIReasoning(req *openai.ChatCompletionRequest) {
req.Temperature = 0
req.TopP = 0
req.FrequencyPenalty = 0
req.PresencePenalty = 0
if req.MaxCompletionTokens == 0 && req.MaxTokens > 0 {
req.MaxCompletionTokens = req.MaxTokens
}
req.MaxTokens = 0
}
// providerRegistry is ordered: more specific adapters (those with a real
// Matches predicate) must precede the generic catch-all for the same provider.
var providerRegistry = []providerAdapter{
weKnoraCloudProvider{},
qwenThinkingProvider{},
lkeapProvider{},
deepseekProvider{},
genericProvider{},
volcengineProvider{},
nvidiaProvider{},
azureReasoningProvider{},
azureProvider{},
openAIReasoningProvider{},
moonshotProvider{},
}
// resolveProvider returns the adapter handling the given provider+model, or
// baseProvider{} (Bearer auth, standard endpoint, no thinking) when none matches.
func resolveProvider(name provider.ProviderName, model string) providerAdapter {
for _, p := range providerRegistry {
if p.Name() == name && p.Matches(model) {
return p
}
}
return baseProvider{}
}
+159
View File
@@ -0,0 +1,159 @@
package chat
import (
"encoding/json"
"testing"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/types"
"github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestResolveProvider pins the provider+model routing table, including the
// sub-model matchers (reasoning models, Qwen thinking, LKEAP DeepSeek V3) and
// the baseProvider fallback for everything else.
func TestResolveProvider(t *testing.T) {
cases := []struct {
name string
prov provider.ProviderName
model string
want providerAdapter
}{
{"deepseek", provider.ProviderDeepSeek, "deepseek-chat", deepseekProvider{}},
{"lkeap v3", provider.ProviderLKEAP, "deepseek-v3.1", lkeapProvider{}},
{"lkeap r1 falls back", provider.ProviderLKEAP, "deepseek-r1", baseProvider{}},
{"qwen thinking", provider.ProviderAliyun, "qwen3-32b", qwenThinkingProvider{}},
{"generic", provider.ProviderGeneric, "anything", genericProvider{}},
{"nvidia", provider.ProviderNvidia, "anything", nvidiaProvider{}},
{"volcengine", provider.ProviderVolcengine, "doubao", volcengineProvider{}},
{"openai non-reasoning falls back", provider.ProviderOpenAI, "gpt-4o", baseProvider{}},
{"openai reasoning", provider.ProviderOpenAI, "gpt-5", openAIReasoningProvider{}},
{"azure non-reasoning", provider.ProviderAzureOpenAI, "gpt-4", azureProvider{}},
{"azure reasoning", provider.ProviderAzureOpenAI, "gpt-5-mini", azureReasoningProvider{}},
{"moonshot fixed temp", provider.ProviderMoonshot, "moonshot-v1-8k", moonshotProvider{}},
{"moonshot other falls back", provider.ProviderMoonshot, "kimi-latest", baseProvider{}},
{"weknora cloud", provider.ProviderWeKnoraCloud, "anything", weKnoraCloudProvider{}},
{"unknown falls back", provider.ProviderName("nope"), "x", baseProvider{}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.IsType(t, tc.want, resolveProvider(tc.prov, tc.model))
})
}
}
func newOutboundChat(t *testing.T, providerName, model string, extra map[string]string) *RemoteAPIChat {
t.Helper()
c, err := NewRemoteAPIChat(&ChatConfig{
Source: types.ModelSourceRemote,
ModelName: model,
APIKey: "k",
ModelID: model,
Provider: providerName,
ExtraConfig: extra,
})
require.NoError(t, err)
return c
}
// TestBuildOutbound_Thinking is the characterization suite for the merged
// thinking-control path: it asserts that buildOutbound produces the same wire
// formats the pre-refactor provider customizers did.
func TestBuildOutbound_Thinking(t *testing.T) {
msgs := []Message{{Role: "user", Content: "hi"}}
t.Run("generic explicit thinking_type overrides legacy kwargs", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderGeneric), "deepseek-v4-flash",
map[string]string{ExtraConfigThinkingControl: "thinking_type"})
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(false)}, true)
require.True(t, useRaw)
js := mustJSON(t, body)
assert.Contains(t, js, `"thinking"`)
assert.Contains(t, js, `"disabled"`)
assert.NotContains(t, js, "chat_template_kwargs")
})
t.Run("generic legacy chat_template_kwargs", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderGeneric), "qwen", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(false)}, true)
require.True(t, useRaw)
assert.Contains(t, mustJSON(t, body), "chat_template_kwargs")
})
t.Run("none keeps the standard SDK request", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderGeneric), "x",
map[string]string{ExtraConfigThinkingControl: "none"})
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(false)}, true)
assert.False(t, useRaw)
_, ok := body.(*openai.ChatCompletionRequest)
assert.True(t, ok)
})
t.Run("qwen non-stream forces disabled", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderAliyun), "qwen3-32b", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(true)}, false)
require.True(t, useRaw)
assert.Contains(t, mustJSON(t, body), `"enable_thinking":false`)
})
t.Run("qwen stream honors requested true", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderAliyun), "qwen3-32b", nil)
body, _, _ := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(true)}, true)
assert.Contains(t, mustJSON(t, body), `"enable_thinking":true`)
})
t.Run("volcengine thinking enabled", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderVolcengine), "doubao", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(true)}, true)
require.True(t, useRaw)
js := mustJSON(t, body)
assert.Contains(t, js, `"thinking"`)
assert.Contains(t, js, `"enabled"`)
})
t.Run("lkeap deepseek-v3 emits thinking type", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderLKEAP), "deepseek-v3.1", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(false)}, true)
require.True(t, useRaw)
assert.Contains(t, mustJSON(t, body), `"thinking"`)
})
t.Run("lkeap r1 left untouched", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderLKEAP), "deepseek-r1", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{Thinking: ptrBool(false)}, true)
assert.False(t, useRaw)
_, ok := body.(*openai.ChatCompletionRequest)
assert.True(t, ok)
})
}
// TestBuildOutbound_ShapeRequest covers the param-shaping providers that used
// to live inline in BuildChatCompletionRequest.
func TestBuildOutbound_ShapeRequest(t *testing.T) {
msgs := []Message{{Role: "user", Content: "hi"}}
t.Run("deepseek strips tool_choice", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderDeepSeek), "deepseek-chat", nil)
body, _, useRaw := c.buildOutbound(msgs, &ChatOptions{ToolChoice: "auto"}, false)
assert.False(t, useRaw)
req := body.(*openai.ChatCompletionRequest)
assert.Nil(t, req.ToolChoice)
})
t.Run("moonshot pins temperature to 1", func(t *testing.T) {
c := newOutboundChat(t, string(provider.ProviderMoonshot), "moonshot-v1-8k", nil)
body, _, _ := c.buildOutbound(msgs, &ChatOptions{Temperature: 0.7, TopP: 0.9}, false)
req := body.(*openai.ChatCompletionRequest)
assert.EqualValues(t, 1, req.Temperature)
assert.EqualValues(t, 0, req.TopP)
})
}
func mustJSON(t *testing.T, v any) string {
t.Helper()
b, err := json.Marshal(v)
require.NoError(t, err)
return string(b)
}
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -178,7 +178,7 @@ func TestBuildChatCompletionRequest_GPT5MaxCompletionTokens(t *testing.T) {
FrequencyPenalty: 0.1,
PresencePenalty: 0.2,
}
req := c.BuildChatCompletionRequest(messages, opts, false)
req := c.shapedRequest(messages, opts, false)
if tc.shouldRewriteMaxT {
assert.Equal(t, 0, req.MaxTokens, "MaxTokens must NOT be sent for GPT-5/o-series")
@@ -201,7 +201,7 @@ func TestBuildChatCompletionRequest_GPT5MaxCompletionTokens(t *testing.T) {
MaxTokens: 128,
MaxCompletionTokens: 2048,
}
req := c.BuildChatCompletionRequest(messages, opts, false)
req := c.shapedRequest(messages, opts, false)
assert.Equal(t, 0, req.MaxTokens)
assert.Equal(t, 2048, req.MaxCompletionTokens)
})
+36
View File
@@ -0,0 +1,36 @@
package chat
import "github.com/Tencent/WeKnora/internal/types"
// thinkingEmitter owns the "reasoning then answer" hand-off that every
// streaming Chat implementation shares: thinking chunks are forwarded as they
// arrive, and exactly one thinking-done marker is emitted before the first
// answer token (or when the stream ends without one). Centralizing the
// bookkeeping keeps the OpenAI-compatible and Ollama stream loops in sync.
type thinkingEmitter struct {
active bool
}
// emit forwards a reasoning chunk and records that a thinking-done marker is
// still owed.
func (e *thinkingEmitter) emit(ch chan types.StreamResponse, content string) {
e.active = true
ch <- types.StreamResponse{
ResponseType: types.ResponseTypeThinking,
Content: content,
Done: false,
}
}
// finish emits the single thinking-done marker if one is owed. Safe to call
// multiple times; only the first call after an emit sends anything.
func (e *thinkingEmitter) finish(ch chan types.StreamResponse) {
if !e.active {
return
}
e.active = false
ch <- types.StreamResponse{
ResponseType: types.ResponseTypeThinking,
Done: true,
}
}
+48
View File
@@ -126,6 +126,54 @@ func (d *streamPacketDumper) WritePacketRaw(raw []byte) {
_, _ = d.file.Write([]byte{'\n'})
}
// WriteError appends a terminal error record (stream read failure, API error, etc.).
func (d *streamPacketDumper) WriteError(message string) {
if d == nil || d.file == nil || strings.TrimSpace(message) == "" {
return
}
line, err := json.Marshal(map[string]any{
"type": "error",
"model": d.model,
"timestamp": time.Now().UTC().Format(time.RFC3339Nano),
"message": message,
})
if err != nil {
return
}
_ = d.writeLine(line)
}
// WriteHTTPError appends a non-2xx HTTP response before any SSE packets.
func (d *streamPacketDumper) WriteHTTPError(statusCode int, body []byte) {
if d == nil || d.file == nil {
return
}
entry := map[string]any{
"type": "http_error",
"model": d.model,
"timestamp": time.Now().UTC().Format(time.RFC3339Nano),
"status_code": statusCode,
}
if len(body) > 0 {
trimmed := bytesTrimSpace(body)
if json.Valid(trimmed) {
var parsed any
if json.Unmarshal(trimmed, &parsed) == nil {
entry["body"] = parsed
} else {
entry["body_raw"] = string(trimmed)
}
} else {
entry["body_raw"] = string(trimmed)
}
}
line, err := json.Marshal(entry)
if err != nil {
return
}
_ = d.writeLine(line)
}
// WritePacket marshals v as one JSON object per line (SDK stream Recv path).
func (d *streamPacketDumper) WritePacket(v any) {
if d == nil || v == nil {
+140
View File
@@ -0,0 +1,140 @@
package chat
import (
"strings"
"github.com/sashabaranov/go-openai"
)
// ExtraConfigThinkingControl is the model parameters.extra_config key for
// selecting how ChatOptions.Thinking is translated to provider HTTP fields.
// The accepted values mirror the strings the frontend writes (see
// ModelEditorDialog.vue): "none", "enable_thinking", "thinking_type",
// "chat_template_kwargs".
const ExtraConfigThinkingControl = "thinking_control"
// Wire-format request bodies used by providers that express extended-thinking
// through a non-standard top-level field. They embed the standard OpenAI
// request so all other fields are marshalled unchanged.
// QwenChatCompletionRequest adds Aliyun Qwen's `enable_thinking` boolean.
type QwenChatCompletionRequest struct {
openai.ChatCompletionRequest
EnableThinking *bool `json:"enable_thinking,omitempty"`
}
// ThinkingConfig is the `{ "type": "enabled"|"disabled" }` block used by
// LKEAP / Volcengine style providers.
type ThinkingConfig struct {
Type string `json:"type"`
}
// ThinkingChatCompletionRequest adds the `thinking` object for providers that
// use the `{ "thinking": { "type": ... } }` wire format.
type ThinkingChatCompletionRequest struct {
openai.ChatCompletionRequest
Thinking *ThinkingConfig `json:"thinking,omitempty"`
}
// ThinkingStrategy encodes how ChatOptions.Thinking is mapped onto a provider's
// HTTP request. Apply returns (customBody, useRawHTTP):
// - (nil, false) means "send the standard OpenAI request unchanged" (the
// caller keeps using the SDK path).
// - a non-nil customBody must be sent verbatim over raw HTTP because it
// carries fields the OpenAI SDK would strip.
//
// When opts.Thinking is nil most strategies emit nothing, deferring to the
// model's own default; the exception is enableThinking{alwaysSend: true}
// (Aliyun Qwen), which must always pin the field.
type ThinkingStrategy interface {
Apply(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (customBody any, useRawHTTP bool)
}
// noThinking sends no thinking-related fields at all.
type noThinking struct{}
func (noThinking) Apply(*openai.ChatCompletionRequest, *ChatOptions, bool) (any, bool) {
return nil, false
}
// enableThinking encodes thinking via Qwen's `enable_thinking` boolean.
//
// - alwaysSend: pin the field even when opts.Thinking is nil (Aliyun Qwen
// thinking models require it on every request; default value is false).
// - disableOnNonStream: force enable_thinking=false for non-stream requests
// (Qwen3 rejects thinking in non-stream mode).
type enableThinking struct {
alwaysSend bool
disableOnNonStream bool
}
func (s enableThinking) Apply(req *openai.ChatCompletionRequest, opts *ChatOptions, isStream bool) (any, bool) {
thinking := false
switch {
case opts != nil && opts.Thinking != nil:
thinking = *opts.Thinking
case !s.alwaysSend:
return nil, false
}
if s.disableOnNonStream && !isStream {
thinking = false
}
qwenReq := QwenChatCompletionRequest{ChatCompletionRequest: *req}
qwenReq.EnableThinking = &thinking
return qwenReq, true
}
// thinkingTypeField encodes thinking via the `{ "thinking": { "type": ... } }`
// object (LKEAP / Volcengine). Emits nothing when opts.Thinking is unset.
type thinkingTypeField struct{}
func (thinkingTypeField) Apply(req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool) (any, bool) {
if opts == nil || opts.Thinking == nil {
return nil, false
}
r := ThinkingChatCompletionRequest{ChatCompletionRequest: *req}
thinkingType := "disabled"
if *opts.Thinking {
thinkingType = "enabled"
}
r.Thinking = &ThinkingConfig{Type: thinkingType}
return r, true
}
// chatTemplateKwargs encodes thinking via the standard request's
// `chat_template_kwargs.enable_thinking` (vLLM / NVIDIA / generic local
// deployments). Emits nothing when opts.Thinking is unset.
type chatTemplateKwargs struct{}
func (chatTemplateKwargs) Apply(req *openai.ChatCompletionRequest, opts *ChatOptions, _ bool) (any, bool) {
if opts == nil || opts.Thinking == nil {
return nil, false
}
req.ChatTemplateKwargs = map[string]interface{}{
"enable_thinking": *opts.Thinking,
}
return req, true
}
// parseThinkingOverride reads extra_config.thinking_control and returns the
// strategy it selects, or nil when unset (the provider adapter's default
// strategy then applies). An unrecognized non-empty value falls back to
// chat_template_kwargs, preserving the legacy default-mode behavior.
func parseThinkingOverride(extraConfig map[string]string) ThinkingStrategy {
if extraConfig == nil {
return nil
}
switch strings.ToLower(strings.TrimSpace(extraConfig[ExtraConfigThinkingControl])) {
case "":
return nil
case "none":
return noThinking{}
case "enable_thinking":
return enableThinking{}
case "thinking_type":
return thinkingTypeField{}
default:
// "chat_template_kwargs" and any unknown non-empty value.
return chatTemplateKwargs{}
}
}
+127
View File
@@ -0,0 +1,127 @@
package chat
import (
"encoding/json"
"testing"
"github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ptrBool(b bool) *bool { return &b }
// TestThinkingStrategy_NilThinking verifies the strategies that defer to the
// model default emit nothing when ChatOptions.Thinking is unset.
func TestThinkingStrategy_NilThinking(t *testing.T) {
req := openai.ChatCompletionRequest{Model: "test"}
strategies := []ThinkingStrategy{
noThinking{},
enableThinking{}, // not alwaysSend
thinkingTypeField{},
chatTemplateKwargs{},
}
for _, s := range strategies {
custom, raw := s.Apply(&req, nil, true)
assert.Nil(t, custom, "%T", s)
assert.False(t, raw, "%T", s)
}
}
// TestEnableThinking_QwenSemantics pins the Aliyun Qwen behavior: thinking is
// always sent, defaults to false, and is forced off on non-stream requests.
func TestEnableThinking_QwenSemantics(t *testing.T) {
s := enableThinking{alwaysSend: true, disableOnNonStream: true}
req := openai.ChatCompletionRequest{Model: "qwen3-32b"}
t.Run("non-stream forces false even when requested true", func(t *testing.T) {
custom, raw := s.Apply(&req, &ChatOptions{Thinking: ptrBool(true)}, false)
require.True(t, raw)
qwen, ok := custom.(QwenChatCompletionRequest)
require.True(t, ok)
require.NotNil(t, qwen.EnableThinking)
assert.False(t, *qwen.EnableThinking)
})
t.Run("stream honors requested true", func(t *testing.T) {
custom, raw := s.Apply(&req, &ChatOptions{Thinking: ptrBool(true)}, true)
require.True(t, raw)
qwen := custom.(QwenChatCompletionRequest)
require.NotNil(t, qwen.EnableThinking)
assert.True(t, *qwen.EnableThinking)
})
t.Run("stream defaults to false when unset", func(t *testing.T) {
custom, raw := s.Apply(&req, nil, true)
require.True(t, raw)
qwen := custom.(QwenChatCompletionRequest)
require.NotNil(t, qwen.EnableThinking)
assert.False(t, *qwen.EnableThinking)
})
}
// TestEnableThinking_ExtraConfigSemantics pins the extra_config "enable_thinking"
// override: only sent when explicitly requested.
func TestEnableThinking_ExtraConfigSemantics(t *testing.T) {
s := enableThinking{}
req := openai.ChatCompletionRequest{Model: "qwen3"}
custom, raw := s.Apply(&req, &ChatOptions{Thinking: ptrBool(true)}, true)
require.True(t, raw)
qwen := custom.(QwenChatCompletionRequest)
require.NotNil(t, qwen.EnableThinking)
assert.True(t, *qwen.EnableThinking)
custom, raw = s.Apply(&req, nil, true)
assert.Nil(t, custom)
assert.False(t, raw)
}
func TestThinkingTypeField(t *testing.T) {
s := thinkingTypeField{}
req := openai.ChatCompletionRequest{Model: "ds-v3"}
custom, raw := s.Apply(&req, &ChatOptions{Thinking: ptrBool(false)}, true)
require.True(t, raw)
typed, ok := custom.(ThinkingChatCompletionRequest)
require.True(t, ok)
require.NotNil(t, typed.Thinking)
assert.Equal(t, "disabled", typed.Thinking.Type)
custom, raw = s.Apply(&req, &ChatOptions{Thinking: ptrBool(true)}, true)
require.True(t, raw)
assert.Equal(t, "enabled", custom.(ThinkingChatCompletionRequest).Thinking.Type)
}
func TestChatTemplateKwargs(t *testing.T) {
s := chatTemplateKwargs{}
req := openai.ChatCompletionRequest{Model: "vllm"}
custom, raw := s.Apply(&req, &ChatOptions{Thinking: ptrBool(true)}, true)
require.True(t, raw)
out, ok := custom.(*openai.ChatCompletionRequest)
require.True(t, ok)
assert.Equal(t, true, out.ChatTemplateKwargs["enable_thinking"])
body, err := json.Marshal(custom)
require.NoError(t, err)
assert.Contains(t, string(body), "chat_template_kwargs")
}
func TestParseThinkingOverride(t *testing.T) {
cases := map[string]ThinkingStrategy{
"none": noThinking{},
"enable_thinking": enableThinking{},
"thinking_type": thinkingTypeField{},
"chat_template_kwargs": chatTemplateKwargs{},
"something-unknown": chatTemplateKwargs{}, // legacy default-mode fallback
}
for value, want := range cases {
got := parseThinkingOverride(map[string]string{ExtraConfigThinkingControl: value})
assert.IsType(t, want, got, "value=%q", value)
}
assert.Nil(t, parseThinkingOverride(nil))
assert.Nil(t, parseThinkingOverride(map[string]string{}))
assert.Nil(t, parseThinkingOverride(map[string]string{ExtraConfigThinkingControl: ""}))
}
+59
View File
@@ -0,0 +1,59 @@
package chat
import (
"context"
"net/http"
"os"
"strconv"
"strings"
"time"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// LLM 调用超时配置。仅作为"上层未设置 deadline 时"的兜底,避免 hung 请求
// 永久阻塞 worker。如果上层 ctx 已经设置了 deadline(无论比默认更短还是更长),
// 都会原样尊重,不再叠加默认超时。可通过环境变量覆盖:
// - WEKNORA_LLM_CHAT_TIMEOUT_SECONDS 非流式调用兜底超时(默认 600s)
// - WEKNORA_LLM_STREAM_TIMEOUT_SECONDS 流式调用兜底超时(默认 1800s)
var (
defaultChatTimeout = envDurationSeconds("WEKNORA_LLM_CHAT_TIMEOUT_SECONDS", 300*time.Second)
defaultStreamTimeout = envDurationSeconds("WEKNORA_LLM_STREAM_TIMEOUT_SECONDS", 600*time.Second)
)
// envDurationSeconds 读取以"秒"为单位的环境变量,解析失败或非正值时回退到 fallback。
func envDurationSeconds(key string, fallback time.Duration) time.Duration {
v := strings.TrimSpace(os.Getenv(key))
if v == "" {
return fallback
}
n, err := strconv.Atoi(v)
if err != nil || n <= 0 {
return fallback
}
return time.Duration(n) * time.Second
}
// withLLMTimeout 仅在上层 ctx 没有 deadline 时附加一个兜底超时;
// 如果上层已显式设置 deadline(无论更短或更长),则原样返回,
// 让调用方对自己的超时策略拥有最终决定权。
func withLLMTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc) {
if _, ok := ctx.Deadline(); ok {
return ctx, func() {}
}
return context.WithTimeout(ctx, d)
}
// rawHTTPClient is a shared HTTP client for raw HTTP LLM calls with connection-level timeouts.
// Per-request timeout is enforced via context deadline (see defaultChatTimeout / defaultStreamTimeout)
// rather than http.Client.Timeout, so streaming calls are not prematurely terminated.
// Uses SSRFSafeDialContext to prevent DNS rebinding attacks at the connection layer.
var rawHTTPClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: secutils.SSRFSafeDialContext,
TLSHandshakeTimeout: 10 * time.Second,
IdleConnTimeout: 90 * time.Second,
MaxIdleConnsPerHost: 5,
},
}
+20
View File
@@ -0,0 +1,20 @@
package chat
import (
"context"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
)
// logUsage emits the standard "[LLM Usage]" line shared by every Chat
// implementation. It is a no-op when usage is nil so callers can pass through
// optional usage blocks without guarding at each call site.
func logUsage(ctx context.Context, model string, u *types.TokenUsage) {
if u == nil {
return
}
logger.Infof(ctx,
"[LLM Usage] model=%s, prompt_tokens=%d, completion_tokens=%d, total_tokens=%d, cached_tokens=%d",
model, u.PromptTokens, u.CompletionTokens, u.TotalTokens, u.CachedTokens)
}