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:
@@ -10,9 +10,19 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"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"
|
||||
)
|
||||
|
||||
func finalAnswerImageRequirement(hasRetrievedImage bool) string {
|
||||
if !hasRetrievedImage {
|
||||
return ""
|
||||
}
|
||||
return `
|
||||
6. Retrieved tool results contain Markdown images. Unless the user explicitly requested text-only output or every image is clearly unrelated, the final answer MUST include at least one relevant Markdown image copied verbatim from the tool results. Preserve its complete URL exactly. Use ASCII half-width parentheses exactly as  and never use full-width ( or ). Place the image immediately after the paragraph it supports. When multiple images support different sections, distribute them across those sections instead of stopping after the first image.
|
||||
7. Before finishing, silently verify that the answer contains a Markdown image when requirement 6 applies.`
|
||||
}
|
||||
|
||||
// streamFinalAnswerToEventBus streams the final answer generation through EventBus
|
||||
func (e *AgentEngine) streamFinalAnswerToEventBus(
|
||||
ctx context.Context,
|
||||
@@ -41,9 +51,13 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
|
||||
|
||||
// Add all tool call results as context
|
||||
toolResultCount := 0
|
||||
hasRetrievedImage := false
|
||||
for stepIdx, step := range state.RoundSteps {
|
||||
for toolIdx, toolCall := range step.ToolCalls {
|
||||
toolResultCount++
|
||||
if searchutil.MarkdownImageRegex.MatchString(toolCall.Result.Output) {
|
||||
hasRetrievedImage = true
|
||||
}
|
||||
messages = append(messages, chat.Message{
|
||||
Role: "user",
|
||||
Content: fmt.Sprintf("Tool %s returned: %s", toolCall.Name, toolCall.Result.Output),
|
||||
@@ -56,6 +70,8 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
|
||||
logger.Debugf(ctx, "[Agent][FinalAnswer] Built context: %d messages, %d tool results",
|
||||
len(messages), toolResultCount)
|
||||
|
||||
imageRequirement := finalAnswerImageRequirement(hasRetrievedImage)
|
||||
|
||||
// Add final answer prompt
|
||||
finalPrompt := fmt.Sprintf(`Based on the above tool call results, generate a complete answer for the user's question.
|
||||
|
||||
@@ -67,8 +83,9 @@ Requirements:
|
||||
3. Organize the answer in a structured format
|
||||
4. If information is insufficient, honestly state so
|
||||
5. IMPORTANT: Respond in the same language as the user's question
|
||||
%s
|
||||
|
||||
Now generate the final answer:`, query)
|
||||
Now generate the final answer:`, query, imageRequirement)
|
||||
|
||||
messages = append(messages, chat.Message{
|
||||
Role: "user",
|
||||
|
||||
Reference in New Issue
Block a user