Files
Enterprise-Knowledge-Base/internal/agent/image_requirement.go
T
wizardchen fbc9db85d1 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.
2026-07-15 18:10:31 +08:00

47 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agent
import (
"strings"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/types"
)
const agentRetrievedImageRequirementMarker = "## Retrieved Image Output Requirement"
const agentRetrievedImageSystemRequirement = `
## Retrieved Image Output Requirement
Retrieved tool results for this turn contain Markdown images. Treat images attached to retrieved passages as relevant by default.
- Unless the user explicitly requests text-only output, or every retrieved image is clearly unrelated, the final answer MUST include at least one relevant Markdown image copied verbatim from the tool results.
- Preserve the complete Markdown image syntax and URL exactly; never invent, shorten, normalize, or replace the URL.
- Use ASCII half-width parentheses exactly as ![alt](url); never use full-width ( or ).
- Place each image immediately after the paragraph it supports.
- When multiple retrieved images support different sections, distribute them across those sections instead of stopping after the first image.
- Before finishing, silently verify that the answer contains a Markdown image whenever this requirement applies.`
func stepContainsMarkdownImage(step types.AgentStep) bool {
for _, toolCall := range step.ToolCalls {
if toolCall.Result != nil &&
toolCall.Result.Success &&
searchutil.MarkdownImageRegex.MatchString(toolCall.Result.Output) {
return true
}
}
return false
}
func appendAgentRetrievedImageRequirement(messages []chat.Message) []chat.Message {
for i := range messages {
if messages[i].Role != "system" {
continue
}
if !strings.Contains(messages[i].Content, agentRetrievedImageRequirementMarker) {
messages[i].Content = strings.TrimRight(messages[i].Content, " \t\r\n") + agentRetrievedImageSystemRequirement
}
break
}
return messages
}