fix: 修复temperature参数冲突问题

This commit is contained in:
shaw
2025-10-12 13:25:43 +08:00
parent a58d67940c
commit b7cd143b4c

View File

@@ -977,6 +977,16 @@ class DroidRelayService {
// Anthropic 端点:处理 thinking 字段 // Anthropic 端点:处理 thinking 字段
if (endpointType === 'anthropic') { if (endpointType === 'anthropic') {
const hasTemperatureField = Object.prototype.hasOwnProperty.call(processedBody, 'temperature')
const hasValidTemperature =
processedBody.temperature !== undefined && processedBody.temperature !== null
const hasValidTopP = processedBody.top_p !== undefined && processedBody.top_p !== null
if (hasValidTemperature && hasValidTopP) {
// Claude API 仅允许 temperature 或 top_p 其一,同时优先保留 temperature
delete processedBody.top_p
}
if (this.systemPrompt) { if (this.systemPrompt) {
const promptBlock = { type: 'text', text: this.systemPrompt } const promptBlock = { type: 'text', text: this.systemPrompt }
if (Array.isArray(processedBody.system)) { if (Array.isArray(processedBody.system)) {
@@ -1010,6 +1020,24 @@ class DroidRelayService {
if ('thinking' in processedBody) { if ('thinking' in processedBody) {
delete processedBody.thinking delete processedBody.thinking
} }
} else if (
processedBody.thinking &&
processedBody.thinking.type === 'enabled' &&
hasTemperatureField
) {
const parsedTemperature =
typeof processedBody.temperature === 'string'
? parseFloat(processedBody.temperature)
: processedBody.temperature
if (typeof parsedTemperature === 'number' && !Number.isNaN(parsedTemperature)) {
if (parsedTemperature <= 0) {
// 当开启 thinking 时temperature 不允许为 0
processedBody.temperature = 1
}
} else {
delete processedBody.temperature
}
} }
} }