feat:gemini file

This commit is contained in:
suziheng
2025-09-15 17:16:50 +08:00
parent a529eab39e
commit 974331a028
4 changed files with 45 additions and 5 deletions

23
common/file/file.go Normal file
View File

@@ -0,0 +1,23 @@
package file
import (
"bytes"
"encoding/base64"
"net/http"
)
func GetFileFromUrl(url string) (mimeType string, data string, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
buffer := bytes.NewBuffer(nil)
_, err = buffer.ReadFrom(resp.Body)
if err != nil {
return
}
mimeType = resp.Header.Get("Content-Type")
data = base64.StdEncoding.EncodeToString(buffer.Bytes())
return
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/file"
"github.com/songquanpeng/one-api/common/helper" "github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/image" "github.com/songquanpeng/one-api/common/image"
"github.com/songquanpeng/one-api/common/logger" "github.com/songquanpeng/one-api/common/logger"
@@ -118,11 +119,12 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
parts = append(parts, Part{ parts = append(parts, Part{
Text: part.Text, Text: part.Text,
}) })
} else if part.Type == model.ContentTypeInputPdf { } else if part.Type == model.ContentTypeInputFile {
mimeType, data, _ := file.GetFileFromUrl(part.File.FileData)
parts = append(parts, Part{ parts = append(parts, Part{
FileData: &FileData{ InlineData: &InlineData{
MimeType: "application/pdf", MimeType: mimeType,
FileUri: part.ImageURL.Url, Data: data,
}, },
}) })
} else if part.Type == model.ContentTypeImageURL { } else if part.Type == model.ContentTypeImageURL {

View File

@@ -4,5 +4,5 @@ const (
ContentTypeText = "text" ContentTypeText = "text"
ContentTypeImageURL = "image_url" ContentTypeImageURL = "image_url"
ContentTypeInputAudio = "input_audio" ContentTypeInputAudio = "input_audio"
ContentTypeInputPdf = "application/pdf" ContentTypeInputFile = "file"
) )

View File

@@ -121,6 +121,15 @@ func (m Message) ParseContent() []MessageContent {
}, },
}) })
} }
case ContentTypeInputFile:
if subObj, ok := contentMap["file"].(map[string]any); ok {
contentList = append(contentList, MessageContent{
Type: ContentTypeInputFile,
File: &File{
FileData: subObj["file_data"].(string),
},
})
}
} }
} }
return contentList return contentList
@@ -137,4 +146,10 @@ type MessageContent struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Text string `json:"text"` Text string `json:"text"`
ImageURL *ImageURL `json:"image_url,omitempty"` ImageURL *ImageURL `json:"image_url,omitempty"`
File *File `json:"file,omitempty"`
}
type File struct {
FileData string `json:"file_data,omitempty"`
FileName string `json:"filename,omitempty"`
} }