feat: implement image upload and multimodal support
- Added functionality for image uploads in chat, allowing users to attach images for multimodal Q&A. - Enhanced the input field to handle image selection via drag-and-drop and paste, with validation for file types and sizes. - Updated the backend to process images alongside text queries, including support for image analysis. - Introduced new UI components for image previews and management, improving user interaction with uploaded content. - Added localization strings for image upload features and error messages, enhancing accessibility for users in multiple languages. These changes significantly improve the chat experience by enabling users to incorporate images into their queries, facilitating richer interactions and responses.
This commit is contained in:
@@ -3,6 +3,7 @@ package session
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
@@ -12,6 +13,47 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// convertImageAttachments converts ImageAttachment slice to types.MessageImages
|
||||
func convertImageAttachments(items []ImageAttachment) types.MessageImages {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
result := make(types.MessageImages, len(items))
|
||||
for i, item := range items {
|
||||
result[i] = types.MessageImage{
|
||||
URL: item.URL,
|
||||
Caption: item.Caption,
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// extractImageURLsAndOCRText extracts image references and concatenated analysis text.
|
||||
// For LLM consumption it prefers the raw Data (data URI) when available so that
|
||||
// image_resolve can skip the disk round-trip; falls back to the storage URL otherwise.
|
||||
func extractImageURLsAndOCRText(images []ImageAttachment) (urls []string, ocrText string) {
|
||||
if len(images) == 0 {
|
||||
return nil, ""
|
||||
}
|
||||
urls = make([]string, 0, len(images))
|
||||
var parts []string
|
||||
for _, img := range images {
|
||||
switch {
|
||||
case img.Data != "":
|
||||
urls = append(urls, img.Data)
|
||||
case img.URL != "":
|
||||
urls = append(urls, img.URL)
|
||||
}
|
||||
if img.Caption != "" {
|
||||
parts = append(parts, img.Caption)
|
||||
}
|
||||
}
|
||||
if len(parts) > 0 {
|
||||
ocrText = strings.Join(parts, "\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// convertMentionedItems converts MentionedItemRequest slice to types.MentionedItems
|
||||
func convertMentionedItems(items []MentionedItemRequest) types.MentionedItems {
|
||||
if len(items) == 0 {
|
||||
@@ -123,7 +165,7 @@ func createAgentQueryEvent(sessionID, assistantMessageID string) interfaces.Stre
|
||||
}
|
||||
|
||||
// createUserMessage creates a user message
|
||||
func (h *Handler) createUserMessage(ctx context.Context, sessionID, query, requestID string, mentionedItems types.MentionedItems) error {
|
||||
func (h *Handler) createUserMessage(ctx context.Context, sessionID, query, requestID string, mentionedItems types.MentionedItems, images types.MessageImages) error {
|
||||
_, err := h.messageService.CreateMessage(ctx, &types.Message{
|
||||
SessionID: sessionID,
|
||||
Role: "user",
|
||||
@@ -132,6 +174,7 @@ func (h *Handler) createUserMessage(ctx context.Context, sessionID, query, reque
|
||||
CreatedAt: time.Now(),
|
||||
IsCompleted: true,
|
||||
MentionedItems: mentionedItems,
|
||||
Images: images,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user