Skip to content

Commit

Permalink
feat: correct price calculations for missing OpenAI models and delete…
Browse files Browse the repository at this point in the history
… the legacy models (casibase#756)

* fix: Add price calculations for missing OpenAI models and delete the legacy models

* Update go.mod

---------

Co-authored-by: Eric Luo <hsluoyz@qq.com>
  • Loading branch information
Infinityay and hsluoyz committed Mar 7, 2024
1 parent 9b2808d commit b1dd078
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 37 deletions.
68 changes: 54 additions & 14 deletions model/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,65 @@ Image models:
`
}

// calculatePrice calculates the total price for using a specific AI model based on the input and output token counts.
// This function supports various models with different pricing strategies as outlined below:
//
// GPT-3.5 Turbo Models:
// - "gpt-3.5-turbo-16k" and variants: $0.003 per 1,000 input tokens, $0.004 per 1,000 output tokens.
// - "gpt-3.5-turbo-instruct": $0.0015 per 1,000 input tokens, $0.002 per 1,000 output tokens.
// - "gpt-3.5-turbo-1106": $0.001 per 1,000 input tokens, $0.002 per 1,000 output tokens.
// - Other GPT-3.5 Turbo models (default pricing): $0.0005 per 1,000 input tokens, $0.0015 per 1,000 output tokens.
//
// GPT-4.0 Models:
// - Models with "preview" in their name: $0.01 per 1,000 input tokens, $0.03 per 1,000 output tokens.
// - "gpt-4-32k" and variants: $0.06 per 1,000 input tokens, $0.12 per 1,000 output tokens.
// - Other GPT-4 models (default pricing): $0.03 per 1,000 input tokens, $0.06 per 1,000 output tokens.
//
// DALL-E Models:
// - "dall-e-3": Flat rate of $0.08 per image generated, regardless of token count.
//
// The function dynamically calculates the total price based on the specific model and the number of input/output tokens or images.
// Prices are calculated in USD.
//
// Parameters:
// - modelResult: A pointer to a ModelResult struct, which contains model details, including the token count and the number of images (if applicable).
//
// Returns:
// - error: Returns an error if the model type is unknown, otherwise nil.
func (p *LocalModelProvider) calculatePrice(modelResult *ModelResult) error {
model := p.subType
var inputPricePerThousandTokens, outputPricePerThousandTokens float64
switch {
case strings.Contains(model, "gpt-3.5-turbo-instruct"):
inputPricePerThousandTokens = 0.0015
outputPricePerThousandTokens = 0.002
case strings.Contains(model, "gpt-3.5-turbo"):
inputPricePerThousandTokens = 0.0005
outputPricePerThousandTokens = 0.0015
case strings.Contains(model, "gpt-4-turbo"), strings.Contains(model, "gpt-4-vision"):
inputPricePerThousandTokens = 0.01
outputPricePerThousandTokens = 0.03
case strings.Contains(model, "gpt-4") && strings.Contains(model, "32k"):
inputPricePerThousandTokens = 0.06
outputPricePerThousandTokens = 0.12
// gpt 3.5 turbo model Support:
case strings.Contains(model, "gpt-3.5"):
if strings.Contains(model, "16k") {
inputPricePerThousandTokens = 0.003
outputPricePerThousandTokens = 0.004
} else if strings.Contains(model, "instruct") {
inputPricePerThousandTokens = 0.0015
outputPricePerThousandTokens = 0.002
} else if strings.Contains(model, "1106") {
inputPricePerThousandTokens = 0.001
outputPricePerThousandTokens = 0.002
} else {
inputPricePerThousandTokens = 0.0005
outputPricePerThousandTokens = 0.0015
}

// gpt 4.0 model
case strings.Contains(model, "gpt-4"):
inputPricePerThousandTokens = 0.03
outputPricePerThousandTokens = 0.06
if strings.Contains(model, "preview") {
inputPricePerThousandTokens = 0.01
outputPricePerThousandTokens = 0.03
} else if strings.Contains(model, "32k") {
inputPricePerThousandTokens = 0.06
outputPricePerThousandTokens = 0.12
} else {
inputPricePerThousandTokens = 0.03
outputPricePerThousandTokens = 0.06
}

// dall-e model
case strings.Contains(model, "dall-e-3"):
modelResult.TotalPrice = float64(modelResult.ImageCount) * 0.08
return nil
Expand Down
36 changes: 13 additions & 23 deletions web/src/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,31 +672,21 @@ export function getProviderTypeOptions(category) {

const openaiModels = [
{id: "dall-e-3", name: "dall-e-3"},
{id: "gpt-3.5-turbo-0125", name: "gpt-3.5-turbo-0125"},
{id: "gpt-3.5-turbo", name: "gpt-3.5-turbo"},
{id: "gpt-3.5-turbo-1106", name: "gpt-3.5-turbo-1106"},
{id: "gpt-3.5-turbo-instruct", name: "gpt-3.5-turbo-instruct"},
{id: "gpt-3.5-turbo-16k-0613", name: "gpt-3.5-turbo-16k-0613"},
{id: "gpt-3.5-turbo-16k", name: "gpt-3.5-turbo-16k"},
{id: "gpt-4-0125-preview", name: "gpt-4-0125-preview"},
{id: "gpt-4-1106-preview", name: "gpt-4-1106-preview"},
{id: "gpt-4-turbo-preview", name: "gpt-4-turbo-preview"},
{id: "gpt-4-vision-preview", name: "gpt-4-vision-preview"},
{id: "gpt-4-vision-dall-e-3", name: "gpt-4-vision-dall-e-3"},
{id: "gpt-4-32k-0613", name: "gpt-4-32k-0613"},
{id: "gpt-4-32k-0314", name: "gpt-4-32k-0314"},
{id: "gpt-4-32k", name: "gpt-4-32k"},
{id: "gpt-4-0613", name: "gpt-4-0613"},
{id: "gpt-4-0314", name: "gpt-4-0314"},
{id: "gpt-4-1106-vision-preview", name: "gpt-4-1106-vision-preview"},
{id: "gpt-4", name: "gpt-4"},
{id: "gpt-3.5-turbo-0613", name: "gpt-3.5-turbo-0613"},
{id: "gpt-3.5-turbo-0301", name: "gpt-3.5-turbo-0301"},
{id: "gpt-3.5-turbo-16k", name: "gpt-3.5-turbo-16k"},
{id: "gpt-3.5-turbo-16k-0613", name: "gpt-3.5-turbo-16k-0613"},
{id: "gpt-3.5-turbo", name: "gpt-3.5-turbo"},
{id: "text-davinci-003", name: "text-davinci-003"},
{id: "text-davinci-002", name: "text-davinci-002"},
{id: "text-curie-001", name: "text-curie-001"},
{id: "text-babbage-001", name: "text-babbage-001"},
{id: "text-ada-001", name: "text-ada-001"},
{id: "text-davinci-001", name: "text-davinci-001"},
{id: "davinci-instruct-beta", name: "davinci-instruct-beta"},
{id: "davinci", name: "davinci"},
{id: "curie-instruct-beta", name: "curie-instruct-beta"},
{id: "curie", name: "curie"},
{id: "ada", name: "ada"},
{id: "babbage", name: "babbage"},
{id: "gpt-4-0613", name: "gpt-4-0613"},
{id: "gpt-4-32k", name: "gpt-4-32k"},
{id: "gpt-4-32k-0613", name: "gpt-4-32k-0613"},
];

const openaiEmbeddings = [
Expand Down

0 comments on commit b1dd078

Please sign in to comment.