From eeef89db3b9684102033634e3b4fcb3f90c15768 Mon Sep 17 00:00:00 2001 From: jiahao6635 Date: Fri, 7 Aug 2026 22:26:39 +0800 Subject: [PATCH] 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 --- internal/application/service/wiki_ingest.go | 13 ++++++++++- .../application/service/wiki_ingest_test.go | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index 4b5d7caf..40f6e0c1 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -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 { diff --git a/internal/application/service/wiki_ingest_test.go b/internal/application/service/wiki_ingest_test.go index 71712e7a..091927df 100644 --- a/internal/application/service/wiki_ingest_test.go +++ b/internal/application/service/wiki_ingest_test.go @@ -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) + } +}