mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 02:43:44 +08:00
feat: add tool choice configuration and update steam handling in Gemini
update ToolConfig to use pointer type in ChatRequest
This commit is contained in:
@@ -34,6 +34,12 @@ var mimeTypeMap = map[string]string{
|
||||
"text": "text/plain",
|
||||
}
|
||||
|
||||
var toolChoiceTypeMap = map[string]string{
|
||||
"none": "NONE",
|
||||
"auto": "AUTO",
|
||||
"required": "ANY",
|
||||
}
|
||||
|
||||
// Setting safety to the lowest possible values since Gemini is already powerless enough
|
||||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
geminiRequest := ChatRequest{
|
||||
@@ -92,6 +98,24 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
||||
},
|
||||
}
|
||||
}
|
||||
if textRequest.ToolChoice != nil {
|
||||
geminiRequest.ToolConfig = &ToolConfig{
|
||||
FunctionCallingConfig: FunctionCallingConfig{
|
||||
Mode: "auto",
|
||||
},
|
||||
}
|
||||
switch mode := textRequest.ToolChoice.(type) {
|
||||
case string:
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.Mode = toolChoiceTypeMap[mode]
|
||||
case map[string]interface{}:
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.Mode = "ANY"
|
||||
if fn, ok := mode["function"].(map[string]interface{}); ok {
|
||||
if name, ok := fn["name"].(string); ok {
|
||||
geminiRequest.ToolConfig.FunctionCallingConfig.AllowedFunctionNames = []string{name}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
shouldAddDummyModelMessage := false
|
||||
for _, message := range textRequest.Messages {
|
||||
content := ChatContent{
|
||||
@@ -186,10 +210,16 @@ func (g *ChatResponse) GetResponseText() string {
|
||||
if g == nil {
|
||||
return ""
|
||||
}
|
||||
if len(g.Candidates) > 0 && len(g.Candidates[0].Content.Parts) > 0 {
|
||||
return g.Candidates[0].Content.Parts[0].Text
|
||||
var builder strings.Builder
|
||||
for _, candidate := range g.Candidates {
|
||||
for idx, part := range candidate.Content.Parts {
|
||||
if idx > 0 {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString(part.Text)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
type ChatCandidate struct {
|
||||
@@ -252,8 +282,8 @@ func responseGeminiChat2OpenAI(response *ChatResponse) *openai.TextResponse {
|
||||
choice.Message.ToolCalls = getToolCalls(&candidate)
|
||||
} else {
|
||||
var builder strings.Builder
|
||||
for _, part := range candidate.Content.Parts {
|
||||
if i > 0 {
|
||||
for idx, part := range candidate.Content.Parts {
|
||||
if idx > 0 {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString(part.Text)
|
||||
|
||||
Reference in New Issue
Block a user