fix(wiki): set MaxTokens for generateWithTemplate LLM calls

Combined wiki extraction can require well over 8k completion tokens.
When MaxTokens is unset, OpenAI-compatible clients omit max_tokens and
providers such as DeepSeek default to 8192, truncating mid-JSON and
failing postprocess.wiki.extract with unexpected end of JSON input.

Set a 32768 completion budget for all generateWithTemplate calls so
large extracts can finish while short replies still stop early.

Fixes #2604
main
jiahao6635 2026-08-07 22:26:39 +08:00 committed by lyingbug
parent d4a16cc8f3
commit eeef89db3b
2 changed files with 35 additions and 1 deletions

View File

@ -148,6 +148,17 @@ const (
// materially prolonging task runtime when the remote is genuinely down.
wikiLLMMaxAttempts = 3
// wikiLLMMaxTokens is the completion-token budget for every LLM call
// routed through generateWithTemplate. Combined wiki extraction emits a
// single large JSON document (entities + concepts + details). When
// MaxTokens is left at 0, OpenAI-compatible clients omit max_tokens and
// providers such as DeepSeek apply a default of 8192 — long extracts are
// truncated mid-JSON with finish_reason=length, and parse fails with
// "unexpected end of JSON input" (EXTRACT_FAILED). Raising the budget to
// 32768 matches verified complete outputs for large Chinese policy docs;
// shorter replies still stop early via finish_reason=stop. See #2604.
wikiLLMMaxTokens = 32768
// wikiLLMBackoffBase is the base delay for the exponential backoff
// between retry attempts. The nth retry waits base << (n-1) — so with
// a 2s base we wait 2s, 4s, 8s between attempts.
@ -2467,7 +2478,7 @@ func (s *wikiIngestService) generateWithTemplate(ctx context.Context, chatModel
)
}
thinking := false
opts := &chat.ChatOptions{Temperature: 0.3, Thinking: &thinking}
opts := &chat.ChatOptions{Temperature: 0.3, Thinking: &thinking, MaxTokens: wikiLLMMaxTokens}
prefixFingerprint := chat.PromptPrefixFingerprint(messages, opts)
warmupKey := ""
if promptTpl == agent.WikiPageModifyUserPrompt {

View File

@ -611,3 +611,26 @@ func (r *wikiPendingRepoForCleanupTest) DeleteByDedupKey(
) error {
return nil
}
// TestGenerateWithTemplateSetsMaxTokens is a regression for #2604: without an
// explicit MaxTokens, DeepSeek-class providers default to 8192 completion
// tokens and truncate combined wiki extraction JSON mid-field.
func TestGenerateWithTemplateSetsMaxTokens(t *testing.T) {
model := &templateCaptureChatModel{response: `{"entities":[],"concepts":[]}`}
service := &wikiIngestService{}
_, err := service.generateWithTemplate(
context.Background(),
model,
`Content={{.Content}}`,
map[string]string{"Content": "hello"},
)
if err != nil {
t.Fatalf("generateWithTemplate() error = %v", err)
}
if model.options.MaxTokens != wikiLLMMaxTokens {
t.Fatalf("MaxTokens = %d, want %d", model.options.MaxTokens, wikiLLMMaxTokens)
}
if model.options.Thinking == nil || *model.options.Thinking {
t.Fatalf("Thinking should be non-nil false, got %#v", model.options.Thinking)
}
}