Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tool choice configuration and update steam handling in Gemini #1996

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions relay/adaptor/gemini/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
"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{
Expand Down Expand Up @@ -92,7 +98,24 @@
},
}
}
shouldAddDummyModelMessage := false
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}
}

Check warning on line 115 in relay/adaptor/gemini/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/gemini/main.go#L101-L115

Added lines #L101 - L115 were not covered by tests
}
}
}
for _, message := range textRequest.Messages {
content := ChatContent{
Role: message.Role,
Expand Down Expand Up @@ -130,25 +153,12 @@
if content.Role == "assistant" {
content.Role = "model"
}
// Converting system prompt to prompt from user for the same reason
// Converting system prompt to SystemInstructions
if content.Role == "system" {
content.Role = "user"
shouldAddDummyModelMessage = true
geminiRequest.SystemInstruction = &content
continue

Check warning on line 159 in relay/adaptor/gemini/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/gemini/main.go#L158-L159

Added lines #L158 - L159 were not covered by tests
}
geminiRequest.Contents = append(geminiRequest.Contents, content)

// If a system message is the last message, we need to add a dummy model message to make gemini happy
if shouldAddDummyModelMessage {
geminiRequest.Contents = append(geminiRequest.Contents, ChatContent{
Role: "model",
Parts: []Part{
{
Text: "Okay",
},
},
})
shouldAddDummyModelMessage = false
}
}

return &geminiRequest
Expand Down Expand Up @@ -186,10 +196,16 @@
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)

Check warning on line 205 in relay/adaptor/gemini/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/gemini/main.go#L199-L205

Added lines #L199 - L205 were not covered by tests
}
}
return ""
return builder.String()

Check warning on line 208 in relay/adaptor/gemini/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/gemini/main.go#L208

Added line #L208 was not covered by tests
}

type ChatCandidate struct {
Expand Down Expand Up @@ -252,8 +268,8 @@
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 {

Check warning on line 272 in relay/adaptor/gemini/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/gemini/main.go#L271-L272

Added lines #L271 - L272 were not covered by tests
builder.WriteString("\n")
}
builder.WriteString(part.Text)
Expand Down
19 changes: 15 additions & 4 deletions relay/adaptor/gemini/model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gemini

type ChatRequest struct {
Contents []ChatContent `json:"contents"`
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
Tools []ChatTools `json:"tools,omitempty"`
Contents []ChatContent `json:"contents"`
SystemInstruction *ChatContent `json:"system_instruction,omitempty"`
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
Tools []ChatTools `json:"tools,omitempty"`
ToolConfig *ToolConfig `json:"tool_config,omitempty"`
}

type EmbeddingRequest struct {
Expand Down Expand Up @@ -74,3 +76,12 @@ type ChatGenerationConfig struct {
CandidateCount int `json:"candidateCount,omitempty"`
StopSequences []string `json:"stopSequences,omitempty"`
}

type FunctionCallingConfig struct {
Mode string `json:"mode,omitempty"`
AllowedFunctionNames []string `json:"allowed_function_names,omitempty"`
}

type ToolConfig struct {
FunctionCallingConfig FunctionCallingConfig `json:"function_calling_config"`
}
Loading