mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-27 13:46:38 +08:00
feat: update Gemini adaptor to support custom response format
This commit is contained in:
parent
fdd7bf41c0
commit
6e55a741a1
@ -4,11 +4,12 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/songquanpeng/one-api/common/render"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/songquanpeng/one-api/common/render"
|
||||||
|
|
||||||
"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/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
@ -28,6 +29,11 @@ const (
|
|||||||
VisionMaxImageNum = 16
|
VisionMaxImageNum = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mimeTypeMap = map[string]string{
|
||||||
|
"json_object": "application/json",
|
||||||
|
"text": "text/plain",
|
||||||
|
}
|
||||||
|
|
||||||
// Setting safety to the lowest possible values since Gemini is already powerless enough
|
// Setting safety to the lowest possible values since Gemini is already powerless enough
|
||||||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||||
geminiRequest := ChatRequest{
|
geminiRequest := ChatRequest{
|
||||||
@ -56,6 +62,15 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
|||||||
MaxOutputTokens: textRequest.MaxTokens,
|
MaxOutputTokens: textRequest.MaxTokens,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if textRequest.ResponseFormat != nil {
|
||||||
|
if mimeType, ok := mimeTypeMap[textRequest.ResponseFormat.Type]; ok {
|
||||||
|
geminiRequest.GenerationConfig.ResponseMimeType = mimeType
|
||||||
|
}
|
||||||
|
if textRequest.ResponseFormat.JsonSchema != nil {
|
||||||
|
geminiRequest.GenerationConfig.ResponseSchema = textRequest.ResponseFormat.JsonSchema.Schema
|
||||||
|
geminiRequest.GenerationConfig.ResponseMimeType = mimeTypeMap["json_object"]
|
||||||
|
}
|
||||||
|
}
|
||||||
if textRequest.Tools != nil {
|
if textRequest.Tools != nil {
|
||||||
functions := make([]model.Function, 0, len(textRequest.Tools))
|
functions := make([]model.Function, 0, len(textRequest.Tools))
|
||||||
for _, tool := range textRequest.Tools {
|
for _, tool := range textRequest.Tools {
|
||||||
|
@ -65,10 +65,12 @@ type ChatTools struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ChatGenerationConfig struct {
|
type ChatGenerationConfig struct {
|
||||||
Temperature float64 `json:"temperature,omitempty"`
|
ResponseMimeType string `json:"responseMimeType,omitempty"`
|
||||||
TopP float64 `json:"topP,omitempty"`
|
ResponseSchema any `json:"responseSchema,omitempty"`
|
||||||
TopK float64 `json:"topK,omitempty"`
|
Temperature float64 `json:"temperature,omitempty"`
|
||||||
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
|
TopP float64 `json:"topP,omitempty"`
|
||||||
CandidateCount int `json:"candidateCount,omitempty"`
|
TopK float64 `json:"topK,omitempty"`
|
||||||
StopSequences []string `json:"stopSequences,omitempty"`
|
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
|
||||||
|
CandidateCount int `json:"candidateCount,omitempty"`
|
||||||
|
StopSequences []string `json:"stopSequences,omitempty"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user