mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: support Mistral's models now (close #1051)
This commit is contained in:
		@@ -67,6 +67,7 @@ _✨ 通过标准的 OpenAI API 格式访问所有的大模型,开箱即用 
 | 
			
		||||
   + [x] [OpenAI ChatGPT 系列模型](https://platform.openai.com/docs/guides/gpt/chat-completions-api)(支持 [Azure OpenAI API](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference))
 | 
			
		||||
   + [x] [Anthropic Claude 系列模型](https://anthropic.com)
 | 
			
		||||
   + [x] [Google PaLM2/Gemini 系列模型](https://developers.generativeai.google)
 | 
			
		||||
   + [x] [Mistral 系列模型](https://mistral.ai/)
 | 
			
		||||
   + [x] [百度文心一言系列模型](https://cloud.baidu.com/doc/WENXINWORKSHOP/index.html)
 | 
			
		||||
   + [x] [阿里通义千问系列模型](https://help.aliyun.com/document_detail/2400395.html)
 | 
			
		||||
   + [x] [讯飞星火认知大模型](https://www.xfyun.cn/doc/spark/Web.html)
 | 
			
		||||
 
 | 
			
		||||
@@ -66,6 +66,7 @@ const (
 | 
			
		||||
	ChannelTypeMoonshot       = 25
 | 
			
		||||
	ChannelTypeBaichuan       = 26
 | 
			
		||||
	ChannelTypeMinimax        = 27
 | 
			
		||||
	ChannelTypeMistral        = 28
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ChannelBaseURLs = []string{
 | 
			
		||||
@@ -97,6 +98,7 @@ var ChannelBaseURLs = []string{
 | 
			
		||||
	"https://api.moonshot.cn",                   // 25
 | 
			
		||||
	"https://api.baichuan-ai.com",               // 26
 | 
			
		||||
	"https://api.minimax.chat",                  // 27
 | 
			
		||||
	"https://api.mistral.ai",                    // 28
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,6 @@ const (
 | 
			
		||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
 | 
			
		||||
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Blfmc9dlf
 | 
			
		||||
// https://openai.com/pricing
 | 
			
		||||
// TODO: when a new api is enabled, check the pricing here
 | 
			
		||||
// 1 === $0.002 / 1K tokens
 | 
			
		||||
// 1 === ¥0.014 / 1k tokens
 | 
			
		||||
var ModelRatio = map[string]float64{
 | 
			
		||||
@@ -116,15 +115,29 @@ var ModelRatio = map[string]float64{
 | 
			
		||||
	"abab6-chat":    0.1 * RMB,
 | 
			
		||||
	"abab5.5-chat":  0.015 * RMB,
 | 
			
		||||
	"abab5.5s-chat": 0.005 * RMB,
 | 
			
		||||
	// https://docs.mistral.ai/platform/pricing/
 | 
			
		||||
	"open-mistral-7b":       0.25 / 1000 * USD,
 | 
			
		||||
	"open-mixtral-8x7b":     0.7 / 1000 * USD,
 | 
			
		||||
	"mistral-small-latest":  2.0 / 1000 * USD,
 | 
			
		||||
	"mistral-medium-latest": 2.7 / 1000 * USD,
 | 
			
		||||
	"mistral-large-latest":  8.0 / 1000 * USD,
 | 
			
		||||
	"mistral-embed":         0.1 / 1000 * USD,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var CompletionRatio = map[string]float64{}
 | 
			
		||||
 | 
			
		||||
var DefaultModelRatio map[string]float64
 | 
			
		||||
var DefaultCompletionRatio map[string]float64
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	DefaultModelRatio = make(map[string]float64)
 | 
			
		||||
	for k, v := range ModelRatio {
 | 
			
		||||
		DefaultModelRatio[k] = v
 | 
			
		||||
	}
 | 
			
		||||
	DefaultCompletionRatio = make(map[string]float64)
 | 
			
		||||
	for k, v := range CompletionRatio {
 | 
			
		||||
		DefaultCompletionRatio[k] = v
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ModelRatio2JSONString() string {
 | 
			
		||||
@@ -155,8 +168,6 @@ func GetModelRatio(name string) float64 {
 | 
			
		||||
	return ratio
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var CompletionRatio = map[string]float64{}
 | 
			
		||||
 | 
			
		||||
func CompletionRatio2JSONString() string {
 | 
			
		||||
	jsonBytes, err := json.Marshal(CompletionRatio)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -174,6 +185,9 @@ func GetCompletionRatio(name string) float64 {
 | 
			
		||||
	if ratio, ok := CompletionRatio[name]; ok {
 | 
			
		||||
		return ratio
 | 
			
		||||
	}
 | 
			
		||||
	if ratio, ok := DefaultCompletionRatio[name]; ok {
 | 
			
		||||
		return ratio
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(name, "gpt-3.5") {
 | 
			
		||||
		if strings.HasSuffix(name, "0125") {
 | 
			
		||||
			// https://openai.com/blog/new-embedding-models-and-api-updates
 | 
			
		||||
@@ -206,5 +220,8 @@ func GetCompletionRatio(name string) float64 {
 | 
			
		||||
	if strings.HasPrefix(name, "claude-2") {
 | 
			
		||||
		return 2.965517
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(name, "mistral-") {
 | 
			
		||||
		return 3
 | 
			
		||||
	}
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ import (
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/ai360"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/baichuan"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/minimax"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/mistral"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/moonshot"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/constant"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/helper"
 | 
			
		||||
@@ -122,6 +123,17 @@ func init() {
 | 
			
		||||
			Parent:     nil,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	for _, modelName := range mistral.ModelList {
 | 
			
		||||
		openAIModels = append(openAIModels, OpenAIModels{
 | 
			
		||||
			Id:         modelName,
 | 
			
		||||
			Object:     "model",
 | 
			
		||||
			Created:    1626777600,
 | 
			
		||||
			OwnedBy:    "mistralai",
 | 
			
		||||
			Permission: permission,
 | 
			
		||||
			Root:       modelName,
 | 
			
		||||
			Parent:     nil,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	openAIModelsMap = make(map[string]OpenAIModels)
 | 
			
		||||
	for _, model := range openAIModels {
 | 
			
		||||
		openAIModelsMap[model.Id] = model
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								relay/channel/mistral/constants.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								relay/channel/mistral/constants.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
package mistral
 | 
			
		||||
 | 
			
		||||
var ModelList = []string{
 | 
			
		||||
	"open-mistral-7b",
 | 
			
		||||
	"open-mixtral-8x7b",
 | 
			
		||||
	"mistral-small-latest",
 | 
			
		||||
	"mistral-medium-latest",
 | 
			
		||||
	"mistral-large-latest",
 | 
			
		||||
	"mistral-embed",
 | 
			
		||||
}
 | 
			
		||||
@@ -9,6 +9,7 @@ import (
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/ai360"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/baichuan"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/minimax"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/mistral"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/channel/moonshot"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/model"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/util"
 | 
			
		||||
@@ -94,6 +95,8 @@ func (a *Adaptor) GetModelList() []string {
 | 
			
		||||
		return baichuan.ModelList
 | 
			
		||||
	case common.ChannelTypeMinimax:
 | 
			
		||||
		return minimax.ModelList
 | 
			
		||||
	case common.ChannelTypeMistral:
 | 
			
		||||
		return mistral.ModelList
 | 
			
		||||
	default:
 | 
			
		||||
		return ModelList
 | 
			
		||||
	}
 | 
			
		||||
@@ -111,6 +114,8 @@ func (a *Adaptor) GetChannelName() string {
 | 
			
		||||
		return "baichuan"
 | 
			
		||||
	case common.ChannelTypeMinimax:
 | 
			
		||||
		return "minimax"
 | 
			
		||||
	case common.ChannelTypeMistral:
 | 
			
		||||
		return "mistralai"
 | 
			
		||||
	default:
 | 
			
		||||
		return "openai"
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -172,10 +172,8 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *util.R
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Error(ctx, "error update user quota cache: "+err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	if quota != 0 {
 | 
			
		||||
		logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
 | 
			
		||||
		model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent)
 | 
			
		||||
		model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota)
 | 
			
		||||
		model.UpdateChannelUsedQuota(meta.ChannelId, quota)
 | 
			
		||||
	}
 | 
			
		||||
	logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
 | 
			
		||||
	model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent)
 | 
			
		||||
	model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota)
 | 
			
		||||
	model.UpdateChannelUsedQuota(meta.ChannelId, quota)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,12 @@ export const CHANNEL_OPTIONS = {
 | 
			
		||||
    value: 24,
 | 
			
		||||
    color: 'orange'
 | 
			
		||||
  },
 | 
			
		||||
  28: {
 | 
			
		||||
    key: 28,
 | 
			
		||||
    text: 'Mistral AI',
 | 
			
		||||
    value: 28,
 | 
			
		||||
    color: 'orange'
 | 
			
		||||
  },
 | 
			
		||||
  15: {
 | 
			
		||||
    key: 15,
 | 
			
		||||
    text: '百度文心千帆',
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ export const CHANNEL_OPTIONS = [
 | 
			
		||||
  { key: 3, text: 'Azure OpenAI', value: 3, color: 'olive' },
 | 
			
		||||
  { key: 11, text: 'Google PaLM2', value: 11, color: 'orange' },
 | 
			
		||||
  { key: 24, text: 'Google Gemini', value: 24, color: 'orange' },
 | 
			
		||||
  { key: 28, text: 'Mistral AI', value: 28, color: 'orange' },
 | 
			
		||||
  { key: 15, text: '百度文心千帆', value: 15, color: 'blue' },
 | 
			
		||||
  { key: 17, text: '阿里通义千问', value: 17, color: 'orange' },
 | 
			
		||||
  { key: 18, text: '讯飞星火认知', value: 18, color: 'blue' },
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user