mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-12 03:13:41 +08:00
feat:gemini file
This commit is contained in:
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"
|
||||||
"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 {
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user