fix(agent): stricter max_tokens calculation for response length control
- Reduce tokensPerChar from 2 to 1.8 for more accurate Chinese token estimation - Use min() instead of max() to enforce upper limits on token counts - CHAT: max 200 tokens (was min 256) - SIMPLE_QUERY: max 600 tokens (was min 512) - CLARIFICATION: max 300 tokens (was min 256) - CONFIRMATION: max 400 tokens (was min 384) - DEEP_CONSULTATION: 800-1600 tokens (was 1024-4096) - ACTION_NEEDED: 500-1000 tokens (was 768-2048) This should result in more concise AI responses that better match the intent classifier's suggested length limits. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
d9b4c72894
commit
c768e2aa53
|
|
@ -85,29 +85,29 @@ export class ClaudeAgentService implements OnModuleInit {
|
|||
|
||||
/**
|
||||
* Calculate max tokens based on intent classification
|
||||
* 中文平均每字符约 1.5 tokens,加上一定余量
|
||||
* 严格控制回复长度,中文约 1.5 tokens/字符
|
||||
*/
|
||||
private calculateMaxTokens(intent: IntentResult): number {
|
||||
// 字符数转 tokens(中文约 1.5 tokens/字符,取 2 以留余量)
|
||||
const tokensPerChar = 2;
|
||||
const baseTokens = intent.maxResponseLength * tokensPerChar;
|
||||
// 中文约 1.5 tokens/字符,稍加余量取 1.8
|
||||
const tokensPerChar = 1.8;
|
||||
const baseTokens = Math.round(intent.maxResponseLength * tokensPerChar);
|
||||
|
||||
// 根据意图类型调整
|
||||
// 根据意图类型调整,严格限制上限
|
||||
switch (intent.type) {
|
||||
case IntentType.CHAT:
|
||||
return Math.max(256, baseTokens); // 闲聊最少 256 tokens
|
||||
return Math.min(200, baseTokens); // 闲聊严格限制 200 tokens
|
||||
case IntentType.SIMPLE_QUERY:
|
||||
return Math.max(512, baseTokens); // 简单查询最少 512 tokens
|
||||
return Math.min(600, baseTokens); // 简单查询限制 600 tokens (~300字)
|
||||
case IntentType.CLARIFICATION:
|
||||
return Math.max(256, baseTokens); // 澄清最少 256 tokens
|
||||
return Math.min(300, baseTokens); // 澄清限制 300 tokens
|
||||
case IntentType.CONFIRMATION:
|
||||
return Math.max(384, baseTokens); // 确认最少 384 tokens
|
||||
return Math.min(400, baseTokens); // 确认限制 400 tokens
|
||||
case IntentType.DEEP_CONSULTATION:
|
||||
return Math.min(4096, Math.max(1024, baseTokens)); // 深度咨询 1024-4096
|
||||
return Math.min(1600, Math.max(800, baseTokens)); // 深度咨询 800-1600
|
||||
case IntentType.ACTION_NEEDED:
|
||||
return Math.min(2048, Math.max(768, baseTokens)); // 需要行动 768-2048
|
||||
return Math.min(1000, Math.max(500, baseTokens)); // 需要行动 500-1000
|
||||
default:
|
||||
return 2048; // 默认 2048
|
||||
return 1024; // 默认 1024
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue