Skip to content

Commit cd6b329

Browse files
committed
feat: support gemini SystemInstructions QuantumNous#408
1 parent c3d4de6 commit cd6b329

3 files changed

Lines changed: 36 additions & 29 deletions

File tree

relay/channel/gemini/constant.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const (
55
)
66

77
var ModelList = []string{
8-
"gemini-1.5-pro-latest", "gemini-1.5-flash-latest", "gemini-ultra",
8+
"gemini-1.5-pro-latest", "gemini-1.5-flash-latest",
99
"gemini-1.5-pro-exp-0827", "gemini-1.5-flash-exp-0827",
1010
"gemini-exp-1114", "gemini-exp-1206",
1111
"gemini-2.0-flash-exp",

relay/channel/gemini/dto.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package gemini
22

33
type GeminiChatRequest struct {
4-
Contents []GeminiChatContent `json:"contents"`
5-
SafetySettings []GeminiChatSafetySettings `json:"safety_settings,omitempty"`
6-
GenerationConfig GeminiChatGenerationConfig `json:"generation_config,omitempty"`
7-
Tools []GeminiChatTools `json:"tools,omitempty"`
4+
Contents []GeminiChatContent `json:"contents"`
5+
SafetySettings []GeminiChatSafetySettings `json:"safety_settings,omitempty"`
6+
GenerationConfig GeminiChatGenerationConfig `json:"generation_config,omitempty"`
7+
Tools []GeminiChatTools `json:"tools,omitempty"`
8+
SystemInstructions *GeminiPart `json:"system_instructions,omitempty"`
89
}
910

1011
type GeminiInlineData struct {

relay/channel/gemini/relay-gemini.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,27 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
7272
},
7373
}
7474
}
75-
shouldAddDummyModelMessage := false
75+
//shouldAddDummyModelMessage := false
7676
for _, message := range textRequest.Messages {
77+
78+
if message.Role == "system" {
79+
geminiRequest.SystemInstructions = &GeminiPart{
80+
Text: message.StringContent(),
81+
}
82+
continue
83+
}
7784
content := GeminiChatContent{
7885
Role: message.Role,
79-
Parts: []GeminiPart{
80-
{
81-
Text: message.StringContent(),
82-
},
83-
},
86+
//Parts: []GeminiPart{
87+
// {
88+
// Text: message.StringContent(),
89+
// },
90+
//},
8491
}
8592
openaiContent := message.ParseContent()
8693
var parts []GeminiPart
8794
imageNum := 0
8895
for _, part := range openaiContent {
89-
9096
if part.Type == dto.ContentTypeText {
9197
parts = append(parts, GeminiPart{
9298
Text: part.Text,
@@ -127,24 +133,24 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
127133
content.Role = "model"
128134
}
129135
// Converting system prompt to prompt from user for the same reason
130-
if content.Role == "system" {
131-
content.Role = "user"
132-
shouldAddDummyModelMessage = true
133-
}
136+
//if content.Role == "system" {
137+
// content.Role = "user"
138+
// shouldAddDummyModelMessage = true
139+
//}
134140
geminiRequest.Contents = append(geminiRequest.Contents, content)
135-
136-
// If a system message is the last message, we need to add a dummy model message to make gemini happy
137-
if shouldAddDummyModelMessage {
138-
geminiRequest.Contents = append(geminiRequest.Contents, GeminiChatContent{
139-
Role: "model",
140-
Parts: []GeminiPart{
141-
{
142-
Text: "Okay",
143-
},
144-
},
145-
})
146-
shouldAddDummyModelMessage = false
147-
}
141+
//
142+
//// If a system message is the last message, we need to add a dummy model message to make gemini happy
143+
//if shouldAddDummyModelMessage {
144+
// geminiRequest.Contents = append(geminiRequest.Contents, GeminiChatContent{
145+
// Role: "model",
146+
// Parts: []GeminiPart{
147+
// {
148+
// Text: "Okay",
149+
// },
150+
// },
151+
// })
152+
// shouldAddDummyModelMessage = false
153+
//}
148154
}
149155
return &geminiRequest
150156
}

0 commit comments

Comments
 (0)