Skip to content

Commit fef7ae0

Browse files
authored
feat: support gemini-2.0-flash (#2055)
* feat: support gemini-2.0-flash - Enhance model support by adding new entries and refining checks for system instruction compatibility. - Update logging display behavior and adjust default quotas for better user experience. - Revamp pricing structures in the billing system to reflect current model values and deprecate outdated entries. - Streamline code by replacing hardcoded values with configurations for maintainability. * feat: add new Gemini 2.0 flash models to adapter and billing ratio * fix: update GetRequestURL to support gemini-1.5 model in versioning
1 parent 6916deb commit fef7ae0

File tree

7 files changed

+76
-31
lines changed

7 files changed

+76
-31
lines changed

relay/adaptor/gemini/adaptor.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strings"
89

910
"github.com/gin-gonic/gin"
10-
11+
"github.com/songquanpeng/one-api/common/config"
1112
"github.com/songquanpeng/one-api/common/helper"
1213
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
1314
"github.com/songquanpeng/one-api/relay/adaptor/openai"
@@ -20,17 +21,12 @@ type Adaptor struct {
2021
}
2122

2223
func (a *Adaptor) Init(meta *meta.Meta) {
23-
2424
}
2525

2626
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
27-
var defaultVersion string
28-
switch meta.ActualModelName {
29-
case "gemini-2.0-flash-exp",
30-
"gemini-2.0-flash-thinking-exp",
31-
"gemini-2.0-flash-thinking-exp-01-21":
32-
defaultVersion = "v1beta"
33-
default:
27+
defaultVersion := config.GeminiVersion
28+
if strings.Contains(meta.ActualModelName, "gemini-2.0") ||
29+
strings.Contains(meta.ActualModelName, "gemini-1.5") {
3430
defaultVersion = "v1beta"
3531
}
3632

relay/adaptor/gemini/constants.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,38 @@ package gemini
44

55
var ModelList = []string{
66
"gemini-pro", "gemini-1.0-pro",
7-
"gemini-1.5-flash", "gemini-1.5-pro",
7+
// "gemma-2-2b-it", "gemma-2-9b-it", "gemma-2-27b-it",
8+
"gemini-1.5-flash", "gemini-1.5-flash-8b",
9+
"gemini-1.5-pro", "gemini-1.5-pro-experimental",
810
"text-embedding-004", "aqa",
9-
"gemini-2.0-flash-exp",
10-
"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
11+
"gemini-2.0-flash", "gemini-2.0-flash-exp",
12+
"gemini-2.0-flash-lite-preview-02-05",
13+
"gemini-2.0-flash-thinking-exp-01-21",
14+
"gemini-2.0-pro-exp-02-05",
15+
}
16+
17+
// ModelsSupportSystemInstruction is the list of models that support system instruction.
18+
//
19+
// https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions
20+
var ModelsSupportSystemInstruction = []string{
21+
// "gemini-1.0-pro-002",
22+
// "gemini-1.5-flash", "gemini-1.5-flash-001", "gemini-1.5-flash-002",
23+
// "gemini-1.5-flash-8b",
24+
// "gemini-1.5-pro", "gemini-1.5-pro-001", "gemini-1.5-pro-002",
25+
// "gemini-1.5-pro-experimental",
26+
"gemini-2.0-flash", "gemini-2.0-flash-exp",
27+
"gemini-2.0-flash-thinking-exp-01-21",
28+
}
29+
30+
// IsModelSupportSystemInstruction check if the model support system instruction.
31+
//
32+
// Because the main version of Go is 1.20, slice.Contains cannot be used
33+
func IsModelSupportSystemInstruction(model string) bool {
34+
for _, m := range ModelsSupportSystemInstruction {
35+
if m == model {
36+
return true
37+
}
38+
}
39+
40+
return false
1141
}

relay/adaptor/gemini/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
132132
}
133133
// Converting system prompt to prompt from user for the same reason
134134
if content.Role == "system" {
135-
content.Role = "user"
136135
shouldAddDummyModelMessage = true
136+
if IsModelSupportSystemInstruction(textRequest.Model) {
137+
geminiRequest.SystemInstruction = &content
138+
geminiRequest.SystemInstruction.Role = ""
139+
continue
140+
} else {
141+
content.Role = "user"
142+
}
137143
}
144+
138145
geminiRequest.Contents = append(geminiRequest.Contents, content)
139146

140147
// If a system message is the last message, we need to add a dummy model message to make gemini happy

relay/adaptor/gemini/model.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 ChatRequest struct {
4-
Contents []ChatContent `json:"contents"`
5-
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
6-
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
7-
Tools []ChatTools `json:"tools,omitempty"`
4+
Contents []ChatContent `json:"contents"`
5+
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
6+
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
7+
Tools []ChatTools `json:"tools,omitempty"`
8+
SystemInstruction *ChatContent `json:"system_instruction,omitempty"`
89
}
910

1011
type EmbeddingRequest struct {

relay/adaptor/vertexai/gemini/adapter.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616

1717
var ModelList = []string{
1818
"gemini-pro", "gemini-pro-vision",
19-
"gemini-1.5-pro-001", "gemini-1.5-flash-001",
20-
"gemini-1.5-pro-002", "gemini-1.5-flash-002",
21-
"gemini-2.0-flash-exp",
22-
"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
19+
"gemini-exp-1206",
20+
"gemini-1.5-pro-001", "gemini-1.5-pro-002",
21+
"gemini-1.5-flash-001", "gemini-1.5-flash-002",
22+
"gemini-2.0-flash-exp", "gemini-2.0-flash-001",
23+
"gemini-2.0-flash-lite-preview-02-05",
24+
"gemini-2.0-flash-thinking-exp-01-21",
2325
}
2426

2527
type Adaptor struct {

relay/billing/ratio/model.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,24 @@ var ModelRatio = map[string]float64{
115115
"bge-large-en": 0.002 * RMB,
116116
"tao-8k": 0.002 * RMB,
117117
// https://ai.google.dev/pricing
118-
"gemini-pro": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
119-
"gemini-1.0-pro": 1,
120-
"gemini-1.5-pro": 1,
121-
"gemini-1.5-pro-001": 1,
122-
"gemini-1.5-flash": 1,
123-
"gemini-1.5-flash-001": 1,
124-
"gemini-2.0-flash-exp": 1,
125-
"gemini-2.0-flash-thinking-exp": 1,
126-
"gemini-2.0-flash-thinking-exp-01-21": 1,
118+
// https://cloud.google.com/vertex-ai/generative-ai/pricing
119+
// "gemma-2-2b-it": 0,
120+
// "gemma-2-9b-it": 0,
121+
// "gemma-2-27b-it": 0,
122+
"gemini-pro": 0.25 * MILLI_USD, // $0.00025 / 1k characters -> $0.001 / 1k tokens
123+
"gemini-1.0-pro": 0.125 * MILLI_USD,
124+
"gemini-1.5-pro": 1.25 * MILLI_USD,
125+
"gemini-1.5-pro-001": 1.25 * MILLI_USD,
126+
"gemini-1.5-pro-experimental": 1.25 * MILLI_USD,
127+
"gemini-1.5-flash": 0.075 * MILLI_USD,
128+
"gemini-1.5-flash-001": 0.075 * MILLI_USD,
129+
"gemini-1.5-flash-8b": 0.0375 * MILLI_USD,
130+
"gemini-2.0-flash-exp": 0.075 * MILLI_USD,
131+
"gemini-2.0-flash": 0.15 * MILLI_USD,
132+
"gemini-2.0-flash-001": 0.15 * MILLI_USD,
133+
"gemini-2.0-flash-lite-preview-02-05": 0.075 * MILLI_USD,
134+
"gemini-2.0-flash-thinking-exp-01-21": 0.075 * MILLI_USD,
135+
"gemini-2.0-pro-exp-02-05": 1.25 * MILLI_USD,
127136
"aqa": 1,
128137
// https://open.bigmodel.cn/pricing
129138
"glm-zero-preview": 0.01 * RMB,

web/default/src/helpers/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,4 @@ export function getChannelModels(type) {
224224
return channelModels[type];
225225
}
226226
return [];
227-
}
227+
}

0 commit comments

Comments
 (0)