feat(piagent): give image generator inline reference placement context
Derive an ordered map of each inline reference image to the surrounding prompt text describing its role, and feed it into the decision, research, task-plan, conversation and image-prompt builders. The image prompt now carries this placement so reference roles are not swapped. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -284,6 +284,8 @@ func (a *DesignAgent) generatePlannedImagesIncremental(ctx context.Context, prom
|
||||
|
||||
func (a *DesignAgent) generateImageWithFallback(ctx context.Context, req design.AgentRequest, prompt string, fallback string, index int) (GeneratedImage, error) {
|
||||
referenceImages := referenceImageURLs(req)
|
||||
prompt = withReferenceImageContext(prompt, req, len(referenceImages))
|
||||
fallback = withReferenceImageContext(fallback, req, len(referenceImages))
|
||||
prompt = withReferenceImageInstruction(prompt, len(referenceImages))
|
||||
fallback = withReferenceImageInstruction(fallback, len(referenceImages))
|
||||
prompt = prepareGPTImagePrompt(prompt, imageGenerationPromptMaxRunes)
|
||||
@@ -345,7 +347,7 @@ func referenceImageURLs(req design.AgentRequest) []string {
|
||||
urls := make([]string, 0)
|
||||
seen := make(map[string]struct{})
|
||||
add := func(value string) {
|
||||
value = strings.TrimSpace(strings.TrimRight(value, ",。,."))
|
||||
value = sanitizeReferenceURL(value)
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
@@ -407,6 +409,24 @@ func withReferenceImageInstruction(prompt string, imageCount int) string {
|
||||
return "已随请求附带参考图;只按用户原话说明的用途使用每张图,例如商品/源图、Logo、风格、色彩、构图或编辑对象。需要编辑或延续源商品时保持其身份,不要仅凭图片内容臆测未说明的角色。\n" + prompt
|
||||
}
|
||||
|
||||
func withReferenceImageContext(prompt string, req design.AgentRequest, imageCount int) string {
|
||||
prompt = strings.TrimSpace(prompt)
|
||||
if prompt == "" || imageCount <= 0 {
|
||||
return prompt
|
||||
}
|
||||
if strings.Contains(prompt, "Inline reference placement:") || strings.Contains(prompt, "参考图位置关系") {
|
||||
return prompt
|
||||
}
|
||||
context := inlineReferencePlacementContext(req.Prompt, req.Messages)
|
||||
if context == "" || context == "none" {
|
||||
return prompt
|
||||
}
|
||||
if prefersEnglish(prompt + "\n" + req.Prompt) {
|
||||
return "Inline reference placement (attached image inputs follow this order; nearby text before or after each image defines its role, so do not swap reference roles):\n" + context + "\n\n" + prompt
|
||||
}
|
||||
return "参考图位置关系(附带图片按此顺序输入;每张图前后的相邻文字代表它的用途/角色,不要调换参考图角色):\n" + context + "\n\n" + prompt
|
||||
}
|
||||
|
||||
func imagePromptTooLarge(prompt string) bool {
|
||||
return utf8.RuneCountInString(strings.TrimSpace(prompt)) > imageGenerationPromptTooLargeRunes
|
||||
}
|
||||
@@ -476,7 +496,7 @@ var explicitPixelSizePattern = regexp.MustCompile(`(?i)([1-9]\d{2,4})\s*(?:x|×|
|
||||
var explicitAspectRatioPattern = regexp.MustCompile(`(?i)(?:^|[^\d])((?:1|2|3|4|9|16)\s*:\s*(?:1|2|3|4|9|16))(?:[^\d]|$)`)
|
||||
|
||||
var imagePromptURLPattern = regexp.MustCompile(`https?://\S+`)
|
||||
var referenceImageDirectiveURLPattern = regexp.MustCompile(`(?i)(?:参考图|reference image)\s*\d*\s*(?:[((][^))\n]+[))])?\s*[::]\s*(https?://[^\s\]]+|data:image/[^\s\]]+|/[^\s\]]+)`)
|
||||
var referenceImageDirectiveURLPattern = regexp.MustCompile(`(?i)(?:参考图|reference image)\s*\d*\s*(?:[((][^))\n]+[))])?\s*[::]\s*(https?://[^\s\],。]+|data:image/[^\s\],。]+|/[^\s\],。]+)`)
|
||||
var promptImageTokenURLPattern = regexp.MustCompile(`\[@image:#\d+:[^:\]]+:(?:\[[^\]]+\]\(([^)]+)\)|([^\]]+))\]`)
|
||||
|
||||
func fullFrameImagePrompt(prompt string) string {
|
||||
|
||||
Reference in New Issue
Block a user