mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: add gemini-2.0-flash-exp and fix race condition in processChannelRelayError (#1983)
Co-authored-by: JustSong <39998050+songquanpeng@users.noreply.github.com>
This commit is contained in:
		@@ -60,7 +60,7 @@ func Relay(c *gin.Context) {
 | 
				
			|||||||
	channelName := c.GetString(ctxkey.ChannelName)
 | 
						channelName := c.GetString(ctxkey.ChannelName)
 | 
				
			||||||
	group := c.GetString(ctxkey.Group)
 | 
						group := c.GetString(ctxkey.Group)
 | 
				
			||||||
	originalModel := c.GetString(ctxkey.OriginalModel)
 | 
						originalModel := c.GetString(ctxkey.OriginalModel)
 | 
				
			||||||
	go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
 | 
						go processChannelRelayError(ctx, userId, channelId, channelName, *bizErr)
 | 
				
			||||||
	requestId := c.GetString(helper.RequestIdKey)
 | 
						requestId := c.GetString(helper.RequestIdKey)
 | 
				
			||||||
	retryTimes := config.RetryTimes
 | 
						retryTimes := config.RetryTimes
 | 
				
			||||||
	if !shouldRetry(c, bizErr.StatusCode) {
 | 
						if !shouldRetry(c, bizErr.StatusCode) {
 | 
				
			||||||
@@ -87,8 +87,7 @@ func Relay(c *gin.Context) {
 | 
				
			|||||||
		channelId := c.GetInt(ctxkey.ChannelId)
 | 
							channelId := c.GetInt(ctxkey.ChannelId)
 | 
				
			||||||
		lastFailedChannelId = channelId
 | 
							lastFailedChannelId = channelId
 | 
				
			||||||
		channelName := c.GetString(ctxkey.ChannelName)
 | 
							channelName := c.GetString(ctxkey.ChannelName)
 | 
				
			||||||
		// BUG: bizErr is in race condition
 | 
							go processChannelRelayError(ctx, userId, channelId, channelName, *bizErr)
 | 
				
			||||||
		go processChannelRelayError(ctx, userId, channelId, channelName, bizErr)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if bizErr != nil {
 | 
						if bizErr != nil {
 | 
				
			||||||
		if bizErr.StatusCode == http.StatusTooManyRequests {
 | 
							if bizErr.StatusCode == http.StatusTooManyRequests {
 | 
				
			||||||
@@ -122,7 +121,7 @@ func shouldRetry(c *gin.Context, statusCode int) bool {
 | 
				
			|||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func processChannelRelayError(ctx context.Context, userId int, channelId int, channelName string, err *model.ErrorWithStatusCode) {
 | 
					func processChannelRelayError(ctx context.Context, userId int, channelId int, channelName string, err model.ErrorWithStatusCode) {
 | 
				
			||||||
	logger.Errorf(ctx, "relay error (channel id %d, user id: %d): %s", channelId, userId, err.Message)
 | 
						logger.Errorf(ctx, "relay error (channel id %d, user id: %d): %s", channelId, userId, err.Message)
 | 
				
			||||||
	// https://platform.openai.com/docs/guides/error-codes/api-errors
 | 
						// https://platform.openai.com/docs/guides/error-codes/api-errors
 | 
				
			||||||
	if monitor.ShouldDisableChannel(&err.Error, err.StatusCode) {
 | 
						if monitor.ShouldDisableChannel(&err.Error, err.StatusCode) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,7 +24,12 @@ 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) {
 | 
				
			||||||
	version := helper.AssignOrDefault(meta.Config.APIVersion, config.GeminiVersion)
 | 
						defaultVersion := config.GeminiVersion
 | 
				
			||||||
 | 
						if meta.ActualModelName == "gemini-2.0-flash-exp" {
 | 
				
			||||||
 | 
							defaultVersion = "v1beta"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						version := helper.AssignOrDefault(meta.Config.APIVersion, defaultVersion)
 | 
				
			||||||
	action := ""
 | 
						action := ""
 | 
				
			||||||
	switch meta.Mode {
 | 
						switch meta.Mode {
 | 
				
			||||||
	case relaymode.Embeddings:
 | 
						case relaymode.Embeddings:
 | 
				
			||||||
@@ -36,6 +41,7 @@ func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
 | 
				
			|||||||
	if meta.IsStream {
 | 
						if meta.IsStream {
 | 
				
			||||||
		action = "streamGenerateContent?alt=sse"
 | 
							action = "streamGenerateContent?alt=sse"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return fmt.Sprintf("%s/%s/models/%s:%s", meta.BaseURL, version, meta.ActualModelName, action), nil
 | 
						return fmt.Sprintf("%s/%s/models/%s:%s", meta.BaseURL, version, meta.ActualModelName, action), nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,5 +3,8 @@ package gemini
 | 
				
			|||||||
// https://ai.google.dev/models/gemini
 | 
					// https://ai.google.dev/models/gemini
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var ModelList = []string{
 | 
					var ModelList = []string{
 | 
				
			||||||
	"gemini-pro", "gemini-1.0-pro", "gemini-1.5-flash", "gemini-1.5-pro", "text-embedding-004", "aqa",
 | 
						"gemini-pro", "gemini-1.0-pro",
 | 
				
			||||||
 | 
						"gemini-1.5-flash", "gemini-1.5-pro",
 | 
				
			||||||
 | 
						"text-embedding-004", "aqa",
 | 
				
			||||||
 | 
						"gemini-2.0-flash-exp",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,10 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var ModelList = []string{
 | 
					var ModelList = []string{
 | 
				
			||||||
	"gemini-1.5-pro-001", "gemini-1.5-flash-001", "gemini-pro", "gemini-pro-vision", "gemini-1.5-pro-002", "gemini-1.5-flash-002",
 | 
						"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",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Adaptor struct {
 | 
					type Adaptor struct {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -108,11 +108,14 @@ 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
 | 
						"gemini-pro":           1, // $0.00025 / 1k characters -> $0.001 / 1k tokens
 | 
				
			||||||
	"gemini-1.0-pro":   1,
 | 
						"gemini-1.0-pro":       1,
 | 
				
			||||||
	"gemini-1.5-flash": 1,
 | 
						"gemini-1.5-pro":       1,
 | 
				
			||||||
	"gemini-1.5-pro":   1,
 | 
						"gemini-1.5-pro-001":   1,
 | 
				
			||||||
	"aqa":              1,
 | 
						"gemini-1.5-flash":     1,
 | 
				
			||||||
 | 
						"gemini-1.5-flash-001": 1,
 | 
				
			||||||
 | 
						"gemini-2.0-flash-exp": 1,
 | 
				
			||||||
 | 
						"aqa":                  1,
 | 
				
			||||||
	// https://open.bigmodel.cn/pricing
 | 
						// https://open.bigmodel.cn/pricing
 | 
				
			||||||
	"glm-4":         0.1 * RMB,
 | 
						"glm-4":         0.1 * RMB,
 | 
				
			||||||
	"glm-4v":        0.1 * RMB,
 | 
						"glm-4v":        0.1 * RMB,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user