WeKnora/internal/handler/initialization_extract_rela...

41 lines
1.4 KiB
Go
Raw Normal View History

fix(chat_pipeline): treat non-JSON model refusals as skipped graph extraction (#3606) * fix(chat_pipeline): treat non-JSON model refusals as skipped graph extraction When a chunk carries nothing extractable (e.g. a table-of-contents page), the LLM correctly answers with a prose refusal such as "抱歉…". parseOutput then fed that prose to json.Unmarshal, failing with a confusing "invalid character 'æ'" (the first byte of a CJK character), marking the chunk task GRAPH_EXTRACT_FAILED and letting asynq replay the identical doomed LLM call up to the retry limit. Classify responses that contain no JSON structure at all (no '{' or '[') as errModelDeclined and complete the extraction with an empty graph so the task succeeds as an explicit skip. Malformed or truncated JSON (structure present) still fails and retries as before. Fixes #3600 * fix(chat_pipeline): keep empty model responses retriable in graph extraction An empty response is usually transient (gateway returning 200 with no content, a reasoning model spending its whole budget on reasoning, content filtering), and a retry often succeeds. Only non-empty prose with no JSON structure is treated as a declined extraction. * fix(graph): skip prose refusals only in the async chunk task, keep truncation retriable ParseGraph no longer turns a prose answer into an empty graph. It returns an error wrapping ErrModelDeclined, and ChunkExtractService.Handle is the only caller that treats it as a skip (span output skipped=model_declined). The /initialization "try extract" endpoint and query-side entity extraction report the refusal as a failure again, so the graph settings form no longer gets its few-shot examples replaced with an empty result. Extractor.Extract drops the ErrModelDeclined marker when finish_reason is "length": prose with no JSON because the output budget ran out is truncation, and asynq should retry it. * fix(graph): retry every truncation spelling, not only finish_reason=length Extract kept a prose answer retriable only when FinishReason was exactly "length". max_tokens / max_output_tokens (and case/space variants that IsLengthFinishReason already accepts), a stream that broke before the stop event ("incomplete") and an adapter that reports no reason were still read as a refusal, so the async chunk task skipped the chunk for good. Only a reason that proves the model stopped on its own now counts as a refusal. Ollama's non-streaming Chat also forwards done_reason, so a num_predict cut-off is reported as "length" instead of an empty string. * fix(graph): only treat a response with no JSON anywhere as a refusal The refusal check looked at the first extracted fence, so a prose fence followed by a JSON fence was skipped as a refusal instead of failing as a retriable parse error. --------- Co-authored-by: LuckVd <LuckVd@users.noreply.github.com> Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-10-08 06:57:11 +00:00
package handler
import (
"context"
"errors"
"testing"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
)
type proseReplyChat struct{}
func (proseReplyChat) Chat(context.Context, []chat.Message, *chat.ChatOptions) (*types.ChatResponse, error) {
return &types.ChatResponse{Content: "抱歉,这段文本没有可抽取的实体和关系。", FinishReason: "stop"}, nil
}
func (proseReplyChat) ChatStream(
context.Context, []chat.Message, *chat.ChatOptions,
) (<-chan types.StreamResponse, error) {
return nil, errors.New("not implemented")
}
func (proseReplyChat) GetModelName() string { return "prose" }
func (proseReplyChat) GetModelID() string { return "prose" }
// The graph-settings "try extract" button must report a prose refusal as a
// failure. Returning an empty graph would make the UI clear the user's
// few-shot nodes/relations and show "success" (#3606 review).
func TestExtractRelationsFromTextReportsModelRefusal(t *testing.T) {
h := &InitializationHandler{config: &config.Config{
ExtractManager: &config.ExtractManagerConfig{
ExtractGraph: &types.PromptTemplateStructured{Description: "extract"},
},
}}
res, err := h.extractRelationsFromText(context.Background(), "目录", nil, proseReplyChat{})
if err == nil {
t.Fatalf("expected an error for a prose refusal, got %+v", res)
}
}