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:
hailin 2026-01-23 08:02:34 -08:00
parent d9b4c72894
commit c768e2aa53
1 changed files with 12 additions and 12 deletions

View File

@ -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
}
}