38 lines
1.2 KiB
Go
38 lines
1.2 KiB
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/cloudwego/eino/schema"
|
|
)
|
|
|
|
type ImageInput struct {
|
|
URLs []string
|
|
}
|
|
|
|
func BuildVisionMessages(systemPrompt string, userText string, images ImageInput) ([]*schema.Message, error) {
|
|
if len(images.URLs) == 0 {
|
|
return nil, errors.New("vision requires at least one image url")
|
|
}
|
|
parts := make([]schema.MessageInputPart, 0, 1+len(images.URLs))
|
|
if strings.TrimSpace(userText) != "" {
|
|
parts = append(parts, schema.MessageInputPart{Type: schema.ChatMessagePartTypeText, Text: userText})
|
|
}
|
|
for _, u := range images.URLs {
|
|
if u == "" {
|
|
continue
|
|
}
|
|
if !strings.HasPrefix(u, "http://") && !strings.HasPrefix(u, "https://") {
|
|
continue
|
|
}
|
|
parts = append(parts, schema.MessageInputPart{Type: schema.ChatMessagePartTypeImageURL, Image: &schema.MessageInputImage{MessagePartCommon: schema.MessagePartCommon{URL: &u}, Detail: schema.ImageURLDetailHigh}})
|
|
}
|
|
if len(parts) == 0 {
|
|
return nil, errors.New("vision inputs invalid: no text or valid image urls")
|
|
}
|
|
msgs := []*schema.Message{schema.SystemMessage(systemPrompt)}
|
|
msgs = append(msgs, &schema.Message{Role: schema.User, UserInputMultiContent: parts})
|
|
return msgs, nil
|
|
}
|