mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 09:16:36 +08:00
feat:gemini file
This commit is contained in:
parent
a529eab39e
commit
974331a028
23
common/file/file.go
Normal file
23
common/file/file.go
Normal 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
|
||||
}
|
@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"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/image"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
@ -118,11 +119,12 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
parts = append(parts, Part{
|
||||
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{
|
||||
FileData: &FileData{
|
||||
MimeType: "application/pdf",
|
||||
FileUri: part.ImageURL.Url,
|
||||
InlineData: &InlineData{
|
||||
MimeType: mimeType,
|
||||
Data: data,
|
||||
},
|
||||
})
|
||||
} else if part.Type == model.ContentTypeImageURL {
|
||||
|
@ -4,5 +4,5 @@ const (
|
||||
ContentTypeText = "text"
|
||||
ContentTypeImageURL = "image_url"
|
||||
ContentTypeInputAudio = "input_audio"
|
||||
ContentTypeInputPdf = "application/pdf"
|
||||
ContentTypeInputFile = "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
|
||||
@ -137,4 +146,10 @@ type MessageContent struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Text string `json:"text"`
|
||||
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"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user