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:
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
@@ -75,7 +76,22 @@ func (c *RemoteAPIChat) ConvertMessages(messages []Message) []openai.ChatComplet
|
||||
Role: msg.Role,
|
||||
}
|
||||
|
||||
if msg.Content != "" {
|
||||
if len(msg.Images) > 0 && msg.Role == "user" {
|
||||
parts := []openai.ChatMessagePart{
|
||||
{Type: openai.ChatMessagePartTypeText, Text: msg.Content},
|
||||
}
|
||||
for _, imgURL := range msg.Images {
|
||||
resolved := resolveImageURLForLLM(imgURL)
|
||||
parts = append(parts, openai.ChatMessagePart{
|
||||
Type: openai.ChatMessagePartTypeImageURL,
|
||||
ImageURL: &openai.ChatMessageImageURL{
|
||||
URL: resolved,
|
||||
Detail: openai.ImageURLDetailAuto,
|
||||
},
|
||||
})
|
||||
}
|
||||
openaiMsg.MultiContent = parts
|
||||
} else if msg.Content != "" {
|
||||
openaiMsg.Content = msg.Content
|
||||
}
|
||||
|
||||
@@ -180,7 +196,7 @@ func (c *RemoteAPIChat) BuildChatCompletionRequest(messages []Message, opts *Cha
|
||||
// logRequest 记录请求日志
|
||||
func (c *RemoteAPIChat) logRequest(ctx context.Context, req any, isStream bool) {
|
||||
if jsonData, err := json.MarshalIndent(req, "", " "); err == nil {
|
||||
logger.Infof(ctx, "[LLM Request] model=%s, stream=%v, request:\n%s", c.modelName, isStream, string(jsonData))
|
||||
logger.Infof(ctx, "[LLM Request] model=%s, stream=%v, request:\n%s", c.modelName, isStream, secutils.CompactImageDataURLForLog(string(jsonData)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,7 +224,15 @@ func (c *RemoteAPIChat) Chat(ctx context.Context, messages []Message, opts *Chat
|
||||
|
||||
resp, err := c.client.CreateChatCompletion(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create chat completion: %w", err)
|
||||
if isMultimodalNotSupportedError(err) {
|
||||
logger.Warnf(ctx, "[LLM Request] Model %s does not support multimodal, retrying without images", c.modelName)
|
||||
cleaned := stripImagesFromMessages(messages)
|
||||
req = c.BuildChatCompletionRequest(cleaned, opts, false)
|
||||
resp, err = c.client.CreateChatCompletion(ctx, req)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create chat completion: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return c.parseCompletionResponse(&resp)
|
||||
@@ -344,8 +368,16 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context, messages []Message, opts
|
||||
|
||||
stream, err := c.client.CreateChatCompletionStream(ctx, req)
|
||||
if err != nil {
|
||||
close(streamChan)
|
||||
return nil, fmt.Errorf("create chat completion stream: %w", err)
|
||||
if isMultimodalNotSupportedError(err) {
|
||||
logger.Warnf(ctx, "[LLM Stream] Model %s does not support multimodal, retrying without images", c.modelName)
|
||||
cleaned := stripImagesFromMessages(messages)
|
||||
req = c.BuildChatCompletionRequest(cleaned, opts, true)
|
||||
stream, err = c.client.CreateChatCompletionStream(ctx, req)
|
||||
}
|
||||
if err != nil {
|
||||
close(streamChan)
|
||||
return nil, fmt.Errorf("create chat completion stream: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
go c.processStream(ctx, stream, streamChan)
|
||||
|
||||
Reference in New Issue
Block a user