mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 01:06:37 +08:00
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
This commit is contained in:
parent
6916debf66
commit
fef7ae048b
@ -5,9 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/songquanpeng/one-api/common/config"
|
||||||
"github.com/songquanpeng/one-api/common/helper"
|
"github.com/songquanpeng/one-api/common/helper"
|
||||||
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
|
channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
|
||||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||||
@ -20,17 +21,12 @@ type Adaptor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) Init(meta *meta.Meta) {
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||||
var defaultVersion string
|
defaultVersion := config.GeminiVersion
|
||||||
switch meta.ActualModelName {
|
if strings.Contains(meta.ActualModelName, "gemini-2.0") ||
|
||||||
case "gemini-2.0-flash-exp",
|
strings.Contains(meta.ActualModelName, "gemini-1.5") {
|
||||||
"gemini-2.0-flash-thinking-exp",
|
|
||||||
"gemini-2.0-flash-thinking-exp-01-21":
|
|
||||||
defaultVersion = "v1beta"
|
|
||||||
default:
|
|
||||||
defaultVersion = "v1beta"
|
defaultVersion = "v1beta"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,38 @@ package gemini
|
|||||||
|
|
||||||
var ModelList = []string{
|
var ModelList = []string{
|
||||||
"gemini-pro", "gemini-1.0-pro",
|
"gemini-pro", "gemini-1.0-pro",
|
||||||
"gemini-1.5-flash", "gemini-1.5-pro",
|
// "gemma-2-2b-it", "gemma-2-9b-it", "gemma-2-27b-it",
|
||||||
|
"gemini-1.5-flash", "gemini-1.5-flash-8b",
|
||||||
|
"gemini-1.5-pro", "gemini-1.5-pro-experimental",
|
||||||
"text-embedding-004", "aqa",
|
"text-embedding-004", "aqa",
|
||||||
"gemini-2.0-flash-exp",
|
"gemini-2.0-flash", "gemini-2.0-flash-exp",
|
||||||
"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
|
"gemini-2.0-flash-lite-preview-02-05",
|
||||||
|
"gemini-2.0-flash-thinking-exp-01-21",
|
||||||
|
"gemini-2.0-pro-exp-02-05",
|
||||||
|
}
|
||||||
|
|
||||||
|
// ModelsSupportSystemInstruction is the list of models that support system instruction.
|
||||||
|
//
|
||||||
|
// https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/system-instructions
|
||||||
|
var ModelsSupportSystemInstruction = []string{
|
||||||
|
// "gemini-1.0-pro-002",
|
||||||
|
// "gemini-1.5-flash", "gemini-1.5-flash-001", "gemini-1.5-flash-002",
|
||||||
|
// "gemini-1.5-flash-8b",
|
||||||
|
// "gemini-1.5-pro", "gemini-1.5-pro-001", "gemini-1.5-pro-002",
|
||||||
|
// "gemini-1.5-pro-experimental",
|
||||||
|
"gemini-2.0-flash", "gemini-2.0-flash-exp",
|
||||||
|
"gemini-2.0-flash-thinking-exp-01-21",
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsModelSupportSystemInstruction check if the model support system instruction.
|
||||||
|
//
|
||||||
|
// Because the main version of Go is 1.20, slice.Contains cannot be used
|
||||||
|
func IsModelSupportSystemInstruction(model string) bool {
|
||||||
|
for _, m := range ModelsSupportSystemInstruction {
|
||||||
|
if m == model {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
@ -132,9 +132,16 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *ChatRequest {
|
|||||||
}
|
}
|
||||||
// Converting system prompt to prompt from user for the same reason
|
// Converting system prompt to prompt from user for the same reason
|
||||||
if content.Role == "system" {
|
if content.Role == "system" {
|
||||||
content.Role = "user"
|
|
||||||
shouldAddDummyModelMessage = true
|
shouldAddDummyModelMessage = true
|
||||||
|
if IsModelSupportSystemInstruction(textRequest.Model) {
|
||||||
|
geminiRequest.SystemInstruction = &content
|
||||||
|
geminiRequest.SystemInstruction.Role = ""
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
content.Role = "user"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
geminiRequest.Contents = append(geminiRequest.Contents, content)
|
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 a system message is the last message, we need to add a dummy model message to make gemini happy
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
package gemini
|
package gemini
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
Contents []ChatContent `json:"contents"`
|
Contents []ChatContent `json:"contents"`
|
||||||
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
|
SafetySettings []ChatSafetySettings `json:"safety_settings,omitempty"`
|
||||||
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
|
GenerationConfig ChatGenerationConfig `json:"generation_config,omitempty"`
|
||||||
Tools []ChatTools `json:"tools,omitempty"`
|
Tools []ChatTools `json:"tools,omitempty"`
|
||||||
|
SystemInstruction *ChatContent `json:"system_instruction,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmbeddingRequest struct {
|
type EmbeddingRequest struct {
|
||||||
|
@ -16,10 +16,12 @@ import (
|
|||||||
|
|
||||||
var ModelList = []string{
|
var ModelList = []string{
|
||||||
"gemini-pro", "gemini-pro-vision",
|
"gemini-pro", "gemini-pro-vision",
|
||||||
"gemini-1.5-pro-001", "gemini-1.5-flash-001",
|
"gemini-exp-1206",
|
||||||
"gemini-1.5-pro-002", "gemini-1.5-flash-002",
|
"gemini-1.5-pro-001", "gemini-1.5-pro-002",
|
||||||
"gemini-2.0-flash-exp",
|
"gemini-1.5-flash-001", "gemini-1.5-flash-002",
|
||||||
"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
|
"gemini-2.0-flash-exp", "gemini-2.0-flash-001",
|
||||||
|
"gemini-2.0-flash-lite-preview-02-05",
|
||||||
|
"gemini-2.0-flash-thinking-exp-01-21",
|
||||||
}
|
}
|
||||||
|
|
||||||
type Adaptor struct {
|
type Adaptor struct {
|
||||||
|
@ -115,15 +115,24 @@ var ModelRatio = map[string]float64{
|
|||||||
"bge-large-en": 0.002 * RMB,
|
"bge-large-en": 0.002 * RMB,
|
||||||
"tao-8k": 0.002 * RMB,
|
"tao-8k": 0.002 * RMB,
|
||||||
// https://ai.google.dev/pricing
|
// https://ai.google.dev/pricing
|
||||||
"gemini-pro": 1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
|
// https://cloud.google.com/vertex-ai/generative-ai/pricing
|
||||||
"gemini-1.0-pro": 1,
|
// "gemma-2-2b-it": 0,
|
||||||
"gemini-1.5-pro": 1,
|
// "gemma-2-9b-it": 0,
|
||||||
"gemini-1.5-pro-001": 1,
|
// "gemma-2-27b-it": 0,
|
||||||
"gemini-1.5-flash": 1,
|
"gemini-pro": 0.25 * MILLI_USD, // $0.00025 / 1k characters -> $0.001 / 1k tokens
|
||||||
"gemini-1.5-flash-001": 1,
|
"gemini-1.0-pro": 0.125 * MILLI_USD,
|
||||||
"gemini-2.0-flash-exp": 1,
|
"gemini-1.5-pro": 1.25 * MILLI_USD,
|
||||||
"gemini-2.0-flash-thinking-exp": 1,
|
"gemini-1.5-pro-001": 1.25 * MILLI_USD,
|
||||||
"gemini-2.0-flash-thinking-exp-01-21": 1,
|
"gemini-1.5-pro-experimental": 1.25 * MILLI_USD,
|
||||||
|
"gemini-1.5-flash": 0.075 * MILLI_USD,
|
||||||
|
"gemini-1.5-flash-001": 0.075 * MILLI_USD,
|
||||||
|
"gemini-1.5-flash-8b": 0.0375 * MILLI_USD,
|
||||||
|
"gemini-2.0-flash-exp": 0.075 * MILLI_USD,
|
||||||
|
"gemini-2.0-flash": 0.15 * MILLI_USD,
|
||||||
|
"gemini-2.0-flash-001": 0.15 * MILLI_USD,
|
||||||
|
"gemini-2.0-flash-lite-preview-02-05": 0.075 * MILLI_USD,
|
||||||
|
"gemini-2.0-flash-thinking-exp-01-21": 0.075 * MILLI_USD,
|
||||||
|
"gemini-2.0-pro-exp-02-05": 1.25 * MILLI_USD,
|
||||||
"aqa": 1,
|
"aqa": 1,
|
||||||
// https://open.bigmodel.cn/pricing
|
// https://open.bigmodel.cn/pricing
|
||||||
"glm-zero-preview": 0.01 * RMB,
|
"glm-zero-preview": 0.01 * RMB,
|
||||||
|
@ -224,4 +224,4 @@ export function getChannelModels(type) {
|
|||||||
return channelModels[type];
|
return channelModels[type];
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user