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:
@@ -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 . 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},
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package chatpipeline
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppendRetrievedImageOutputRequirement(t *testing.T) {
|
||||
base := "Answer from retrieved evidence."
|
||||
withImage := appendRetrievedImageOutputRequirement(
|
||||
base,
|
||||
"context\n",
|
||||
)
|
||||
for _, required := range []string{
|
||||
base,
|
||||
"MUST include at least one relevant Markdown image",
|
||||
"Copy the complete Markdown image syntax and its URL verbatim",
|
||||
"ASCII half-width parentheses",
|
||||
"immediately after the paragraph it supports",
|
||||
} {
|
||||
if !strings.Contains(withImage, required) {
|
||||
t.Fatalf("expected %q in dynamic image requirement:\n%s", required, withImage)
|
||||
}
|
||||
}
|
||||
|
||||
withoutImage := appendRetrievedImageOutputRequirement(base, "text-only retrieved context")
|
||||
if withoutImage != base {
|
||||
t.Fatalf("text-only context should not change the system prompt: %q", withoutImage)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user