mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +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:
		@@ -5,9 +5,10 @@ import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/config"
 | 
			
		||||
	"github.com/songquanpeng/one-api/common/helper"
 | 
			
		||||
	channelhelper "github.com/songquanpeng/one-api/relay/adaptor"
 | 
			
		||||
	"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) GetRequestURL(meta *meta.Meta) (string, error) {
 | 
			
		||||
	var defaultVersion string
 | 
			
		||||
	switch meta.ActualModelName {
 | 
			
		||||
	case "gemini-2.0-flash-exp",
 | 
			
		||||
		"gemini-2.0-flash-thinking-exp",
 | 
			
		||||
		"gemini-2.0-flash-thinking-exp-01-21":
 | 
			
		||||
		defaultVersion = "v1beta"
 | 
			
		||||
	default:
 | 
			
		||||
	defaultVersion := config.GeminiVersion
 | 
			
		||||
	if strings.Contains(meta.ActualModelName, "gemini-2.0") ||
 | 
			
		||||
		strings.Contains(meta.ActualModelName, "gemini-1.5") {
 | 
			
		||||
		defaultVersion = "v1beta"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,38 @@ package gemini
 | 
			
		||||
 | 
			
		||||
var ModelList = []string{
 | 
			
		||||
	"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",
 | 
			
		||||
	"gemini-2.0-flash-exp",
 | 
			
		||||
	"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
 | 
			
		||||
	"gemini-2.0-flash", "gemini-2.0-flash-exp",
 | 
			
		||||
	"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
 | 
			
		||||
		if content.Role == "system" {
 | 
			
		||||
			content.Role = "user"
 | 
			
		||||
			shouldAddDummyModelMessage = true
 | 
			
		||||
			if IsModelSupportSystemInstruction(textRequest.Model) {
 | 
			
		||||
				geminiRequest.SystemInstruction = &content
 | 
			
		||||
				geminiRequest.SystemInstruction.Role = ""
 | 
			
		||||
				continue
 | 
			
		||||
			} else {
 | 
			
		||||
				content.Role = "user"
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,11 @@
 | 
			
		||||
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"`
 | 
			
		||||
	SafetySettings    []ChatSafetySettings `json:"safety_settings,omitempty"`
 | 
			
		||||
	GenerationConfig  ChatGenerationConfig `json:"generation_config,omitempty"`
 | 
			
		||||
	Tools             []ChatTools          `json:"tools,omitempty"`
 | 
			
		||||
	SystemInstruction *ChatContent         `json:"system_instruction,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type EmbeddingRequest struct {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,10 +16,12 @@ import (
 | 
			
		||||
 | 
			
		||||
var ModelList = []string{
 | 
			
		||||
	"gemini-pro", "gemini-pro-vision",
 | 
			
		||||
	"gemini-1.5-pro-001", "gemini-1.5-flash-001",
 | 
			
		||||
	"gemini-1.5-pro-002", "gemini-1.5-flash-002",
 | 
			
		||||
	"gemini-2.0-flash-exp",
 | 
			
		||||
	"gemini-2.0-flash-thinking-exp", "gemini-2.0-flash-thinking-exp-01-21",
 | 
			
		||||
	"gemini-exp-1206",
 | 
			
		||||
	"gemini-1.5-pro-001", "gemini-1.5-pro-002",
 | 
			
		||||
	"gemini-1.5-flash-001", "gemini-1.5-flash-002",
 | 
			
		||||
	"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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -115,15 +115,24 @@ var ModelRatio = map[string]float64{
 | 
			
		||||
	"bge-large-en":       0.002 * RMB,
 | 
			
		||||
	"tao-8k":             0.002 * RMB,
 | 
			
		||||
	// https://ai.google.dev/pricing
 | 
			
		||||
	"gemini-pro":                          1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
 | 
			
		||||
	"gemini-1.0-pro":                      1,
 | 
			
		||||
	"gemini-1.5-pro":                      1,
 | 
			
		||||
	"gemini-1.5-pro-001":                  1,
 | 
			
		||||
	"gemini-1.5-flash":                    1,
 | 
			
		||||
	"gemini-1.5-flash-001":                1,
 | 
			
		||||
	"gemini-2.0-flash-exp":                1,
 | 
			
		||||
	"gemini-2.0-flash-thinking-exp":       1,
 | 
			
		||||
	"gemini-2.0-flash-thinking-exp-01-21": 1,
 | 
			
		||||
	// https://cloud.google.com/vertex-ai/generative-ai/pricing
 | 
			
		||||
	// "gemma-2-2b-it":                       0,
 | 
			
		||||
	// "gemma-2-9b-it":                       0,
 | 
			
		||||
	// "gemma-2-27b-it":                      0,
 | 
			
		||||
	"gemini-pro":                          0.25 * MILLI_USD, // $0.00025 / 1k characters -> $0.001 / 1k tokens
 | 
			
		||||
	"gemini-1.0-pro":                      0.125 * MILLI_USD,
 | 
			
		||||
	"gemini-1.5-pro":                      1.25 * MILLI_USD,
 | 
			
		||||
	"gemini-1.5-pro-001":                  1.25 * MILLI_USD,
 | 
			
		||||
	"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,
 | 
			
		||||
	// https://open.bigmodel.cn/pricing
 | 
			
		||||
	"glm-zero-preview": 0.01 * RMB,
 | 
			
		||||
 
 | 
			
		||||
@@ -224,4 +224,4 @@ export function getChannelModels(type) {
 | 
			
		||||
    return channelModels[type];
 | 
			
		||||
  }
 | 
			
		||||
  return [];
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user