mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-18 17:46:37 +08:00
fix: Add image support to Gemini relay
- Add support for getting base64-encoded images via openAI's image_url. - Add `context` as a parameter for the function `LogError`. - Handle the error from `image.GetImageFromUrl` by logging it. - Convert the role to `user` if it is `system` and add a dummy model message to make Gemini happy.
This commit is contained in:
parent
75cbfd7bb6
commit
671fe78e44
@ -44,6 +44,11 @@ func GetImageSizeFromUrl(url string) (width int, height int, err error) {
|
||||
}
|
||||
|
||||
func GetImageFromUrl(url string) (mimeType string, data string, err error) {
|
||||
// openai's image_url support base64 encoded image
|
||||
if strings.HasPrefix(url, "data:image/jpeg;base64,") {
|
||||
return "image/jpeg", strings.TrimPrefix(url, "data:image/jpeg;base64,"), nil
|
||||
}
|
||||
|
||||
isImage, err := IsImageUrl(url)
|
||||
if !isImage {
|
||||
return
|
||||
|
@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -117,7 +118,12 @@ func requestOpenAI2Gemini(textRequest GeneralOpenAIRequest) *GeminiChatRequest {
|
||||
if imageNum > GeminiVisionMaxImageNum {
|
||||
continue
|
||||
}
|
||||
mimeType, data, _ := image.GetImageFromUrl(part.ImageURL.Url)
|
||||
|
||||
mimeType, data, err := image.GetImageFromUrl(part.ImageURL.Url)
|
||||
if err != nil {
|
||||
common.LogError(context.TODO(),
|
||||
fmt.Sprintf("get image from url %s got %+v", part.ImageURL.Url, err))
|
||||
}
|
||||
parts = append(parts, GeminiPart{
|
||||
InlineData: &GeminiInlineData{
|
||||
MimeType: mimeType,
|
||||
|
@ -262,7 +262,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
||||
requestBody = c.Request.Body
|
||||
}
|
||||
|
||||
common.LogInfo(c.Request.Context(), fmt.Sprintf("convert to apitype %d", apiType))
|
||||
common.LogInfo(c.Request.Context(), fmt.Sprintf(
|
||||
"convert to apitype %d, channel_type %d, channel_id %d",
|
||||
apiType, channelType, channelId))
|
||||
switch apiType {
|
||||
case APITypeClaude:
|
||||
claudeRequest := requestOpenAI2Claude(textRequest)
|
||||
@ -300,6 +302,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
||||
return errorWrapper(err, "marshal_text_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
requestBody = bytes.NewBuffer(jsonStr)
|
||||
fmt.Println(">> convert request body to gemini: " + string(jsonStr)) // FIXME
|
||||
// case APITypeZhipu:
|
||||
// zhipuRequest := requestOpenAI2Zhipu(textRequest)
|
||||
// jsonStr, err := json.Marshal(zhipuRequest)
|
||||
@ -431,7 +434,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
||||
}
|
||||
|
||||
{ // more error info
|
||||
if reqdata, err := json.Marshal(textRequest); err != nil {
|
||||
if reqdata, err := json.Marshal(req.Body); err != nil {
|
||||
fmt.Printf("[ERROR] marshal relay text error: %s\n", err.Error())
|
||||
} else {
|
||||
if respdata, err := io.ReadAll(resp.Body); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user