mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: support gpt-4o mini (#1665)
* feat: support gpt-4o mini * feat: fix gpt-4o mini image price
This commit is contained in:
		@@ -8,6 +8,7 @@ var ModelList = []string{
 | 
			
		||||
	"gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-0613",
 | 
			
		||||
	"gpt-4-turbo-preview", "gpt-4-turbo", "gpt-4-turbo-2024-04-09",
 | 
			
		||||
	"gpt-4o", "gpt-4o-2024-05-13",
 | 
			
		||||
	"gpt-4o-mini", "gpt-4o-mini-2024-07-18",
 | 
			
		||||
	"gpt-4-vision-preview",
 | 
			
		||||
	"text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large",
 | 
			
		||||
	"text-curie-001", "text-babbage-001", "text-ada-001", "text-davinci-002", "text-davinci-003",
 | 
			
		||||
 
 | 
			
		||||
@@ -110,7 +110,7 @@ func CountTokenMessages(messages []model.Message, model string) int {
 | 
			
		||||
						if imageUrl["detail"] != nil {
 | 
			
		||||
							detail = imageUrl["detail"].(string)
 | 
			
		||||
						}
 | 
			
		||||
						imageTokens, err := countImageTokens(url, detail)
 | 
			
		||||
						imageTokens, err := countImageTokens(url, detail, model)
 | 
			
		||||
						if err != nil {
 | 
			
		||||
							logger.SysError("error counting image tokens: " + err.Error())
 | 
			
		||||
						} else {
 | 
			
		||||
@@ -134,11 +134,15 @@ const (
 | 
			
		||||
	lowDetailCost         = 85
 | 
			
		||||
	highDetailCostPerTile = 170
 | 
			
		||||
	additionalCost        = 85
 | 
			
		||||
	// gpt-4o-mini cost higher than other model
 | 
			
		||||
	gpt4oMiniLowDetailCost  = 2833
 | 
			
		||||
	gpt4oMiniHighDetailCost = 5667
 | 
			
		||||
	gpt4oMiniAdditionalCost = 2833
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// https://platform.openai.com/docs/guides/vision/calculating-costs
 | 
			
		||||
// https://github.com/openai/openai-cookbook/blob/05e3f9be4c7a2ae7ecf029a7c32065b024730ebe/examples/How_to_count_tokens_with_tiktoken.ipynb
 | 
			
		||||
func countImageTokens(url string, detail string) (_ int, err error) {
 | 
			
		||||
func countImageTokens(url string, detail string, model string) (_ int, err error) {
 | 
			
		||||
	var fetchSize = true
 | 
			
		||||
	var width, height int
 | 
			
		||||
	// Reference: https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding
 | 
			
		||||
@@ -172,6 +176,9 @@ func countImageTokens(url string, detail string) (_ int, err error) {
 | 
			
		||||
	}
 | 
			
		||||
	switch detail {
 | 
			
		||||
	case "low":
 | 
			
		||||
		if strings.HasPrefix(model, "gpt-4o-mini") {
 | 
			
		||||
			return gpt4oMiniLowDetailCost, nil
 | 
			
		||||
		}
 | 
			
		||||
		return lowDetailCost, nil
 | 
			
		||||
	case "high":
 | 
			
		||||
		if fetchSize {
 | 
			
		||||
@@ -191,6 +198,9 @@ func countImageTokens(url string, detail string) (_ int, err error) {
 | 
			
		||||
			height = int(float64(height) * ratio)
 | 
			
		||||
		}
 | 
			
		||||
		numSquares := int(math.Ceil(float64(width)/512) * math.Ceil(float64(height)/512))
 | 
			
		||||
		if strings.HasPrefix(model, "gpt-4o-mini") {
 | 
			
		||||
			return numSquares*gpt4oMiniHighDetailCost + gpt4oMiniAdditionalCost, nil
 | 
			
		||||
		}
 | 
			
		||||
		result := numSquares*highDetailCostPerTile + additionalCost
 | 
			
		||||
		return result, nil
 | 
			
		||||
	default:
 | 
			
		||||
 
 | 
			
		||||
@@ -35,6 +35,8 @@ var ModelRatio = map[string]float64{
 | 
			
		||||
	"gpt-4-turbo-2024-04-09":  5,     // $0.01 / 1K tokens
 | 
			
		||||
	"gpt-4o":                  2.5,   // $0.005 / 1K tokens
 | 
			
		||||
	"gpt-4o-2024-05-13":       2.5,   // $0.005 / 1K tokens
 | 
			
		||||
	"gpt-4o-mini":             0.075, // $0.00015 / 1K tokens
 | 
			
		||||
	"gpt-4o-mini-2024-07-18":  0.075, // $0.00015 / 1K tokens
 | 
			
		||||
	"gpt-4-vision-preview":    5,     // $0.01 / 1K tokens
 | 
			
		||||
	"gpt-3.5-turbo":           0.25,  // $0.0005 / 1K tokens
 | 
			
		||||
	"gpt-3.5-turbo-0301":      0.75,
 | 
			
		||||
@@ -308,6 +310,9 @@ func GetCompletionRatio(name string, channelType int) float64 {
 | 
			
		||||
		return 4.0 / 3.0
 | 
			
		||||
	}
 | 
			
		||||
	if strings.HasPrefix(name, "gpt-4") {
 | 
			
		||||
		if strings.HasPrefix(name, "gpt-4o-mini") {
 | 
			
		||||
			return 4
 | 
			
		||||
		}
 | 
			
		||||
		if strings.HasPrefix(name, "gpt-4-turbo") ||
 | 
			
		||||
			strings.HasPrefix(name, "gpt-4o") ||
 | 
			
		||||
			strings.HasSuffix(name, "preview") {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user