fix(chat): enforce retrieved image output in answers

When retrieved context or tool results contain Markdown images, dynamically
append output requirements to system prompts and final-answer instructions.
Also normalize fullwidth image parentheses in the frontend renderer so
localized model output still renders correctly.
This commit is contained in:
wizardchen
2026-07-15 18:09:17 +08:00
committed by lyingbug
parent 951f95903b
commit fbc9db85d1
11 changed files with 264 additions and 5 deletions
@@ -11,12 +11,31 @@ import (
"github.com/Tencent/WeKnora/internal/common"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
var regThinkTags = regexp.MustCompile(`(?s)<think>.*?</think>`)
const retrievedImageOutputRequirement = `
## Retrieved Image Output Requirement
The retrieved context for this turn contains Markdown images. Images attached to retrieved passages should be treated as relevant by default.
- Unless the user explicitly requests text-only output, or every retrieved image is clearly unrelated to the answer, the final answer MUST include at least one relevant Markdown image copied from the retrieved context.
- Copy the complete Markdown image syntax and its URL verbatim. Never invent, shorten, normalize, or replace the URL.
- Use ASCII half-width parentheses in image Markdown exactly as ![alt](url). Never use full-width ( or ).
- Place each image immediately after the paragraph it supports, rather than collecting images at the end.
- When multiple retrieved images support different sections of a multi-section answer, include them in their corresponding sections instead of stopping after the first image.
- Before finishing, silently verify that the answer contains a Markdown image whenever this requirement applies.`
func appendRetrievedImageOutputRequirement(systemPrompt, renderedContexts string) string {
if !searchutil.MarkdownImageRegex.MatchString(renderedContexts) {
return systemPrompt
}
return strings.TrimRight(systemPrompt, " \t\r\n") + retrievedImageOutputRequirement
}
// pipelineInfo logs pipeline info level entries.
func pipelineInfo(ctx context.Context, stage, action string, fields map[string]interface{}) {
common.PipelineInfo(ctx, stage, action, fields)
@@ -75,6 +94,7 @@ func prepareMessagesWithHistory(chatManage *types.ChatManage) []chat.Message {
"language": chatManage.Language,
"contexts": chatManage.RenderedContexts,
})
systemPrompt = appendRetrievedImageOutputRequirement(systemPrompt, chatManage.RenderedContexts)
chatMessages := []chat.Message{
{Role: "system", Content: systemPrompt},