feat(dingtalk): support image questions with vision models
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
type staticImageDownloader struct {
|
||||
body string
|
||||
fileName string
|
||||
}
|
||||
|
||||
func (d staticImageDownloader) DownloadFile(context.Context, *IncomingMessage) (io.ReadCloser, string, error) {
|
||||
return io.NopCloser(strings.NewReader(d.body)), d.fileName, nil
|
||||
}
|
||||
|
||||
func TestPrepareIMImageForQAAndBuildRequest(t *testing.T) {
|
||||
png := string([]byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}) + strings.Repeat("x", 600)
|
||||
msg := &IncomingMessage{
|
||||
Platform: PlatformDingtalk,
|
||||
MessageType: MessageTypeImage,
|
||||
MessageID: "picture-message-1",
|
||||
FileKey: "temporary-download-code",
|
||||
}
|
||||
|
||||
if err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: png, fileName: "question.png"}); err != nil {
|
||||
t.Fatalf("prepareIMImageForQA: %v", err)
|
||||
}
|
||||
if len(msg.ImageURLs) != 1 || !strings.HasPrefix(msg.ImageURLs[0], "data:image/png;base64,") {
|
||||
t.Fatalf("ImageURLs = %#v", msg.ImageURLs)
|
||||
}
|
||||
if len(msg.ImageReferences) != 1 {
|
||||
t.Fatalf("ImageReferences = %#v", msg.ImageReferences)
|
||||
}
|
||||
if got := msg.ImageReferences[0]; got.URL != "dingtalk://message/picture-message-1" || got.Caption != "question.png" {
|
||||
t.Fatalf("image reference = %#v", got)
|
||||
}
|
||||
if msg.Content == "" {
|
||||
t.Fatal("image-only message should receive a default visual question")
|
||||
}
|
||||
|
||||
req := buildIMQARequest(&types.Session{ID: "session-1"}, msg.Content, "assistant-1", "user-1", nil, nil, nil, msg.ImageURLs)
|
||||
if len(req.ImageURLs) != 1 || req.ImageURLs[0] != msg.ImageURLs[0] {
|
||||
t.Fatalf("request ImageURLs = %#v", req.ImageURLs)
|
||||
}
|
||||
|
||||
audit := newIMMessageAuditContext(msg, "channel-1", nil)
|
||||
userMessage := createIMUserMessagePayload("session-1", msg.Content, "request-1", audit)
|
||||
assistantMessage := createIMAssistantMessagePayload("session-1", "request-1", audit)
|
||||
if len(userMessage.Images) != 1 || userMessage.Images[0].URL != "dingtalk://message/picture-message-1" {
|
||||
t.Fatalf("user message Images = %#v", userMessage.Images)
|
||||
}
|
||||
if len(assistantMessage.Images) != 0 {
|
||||
t.Fatalf("assistant message should not duplicate input image: %#v", assistantMessage.Images)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareIMImageForQARejectsInvalidOrOversizedPayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
want string
|
||||
}{
|
||||
{name: "not an image", body: strings.Repeat("plain text", 80), want: "unsupported image content type"},
|
||||
{name: "oversized", body: strings.Repeat("x", maxIMImageBytes+1), want: "exceeds 10 MiB limit"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
msg := &IncomingMessage{Platform: PlatformDingtalk, MessageType: MessageTypeImage, MessageID: "image-1"}
|
||||
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: tt.body, fileName: "payload.bin"})
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("error = %v, want containing %q", err, tt.want)
|
||||
}
|
||||
if len(msg.ImageURLs) != 0 {
|
||||
t.Fatal("rejected image must not reach the model")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user