-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add model store; add support for openai's gpt-4
- Loading branch information
Showing
12 changed files
with
226 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/shopspring/decimal" | ||
) | ||
|
||
const ( | ||
ModelProviderOpenAI = "openai" | ||
) | ||
|
||
const ( | ||
ModelOpenAIGPT4 = "openai:gpt-4" | ||
ModelOpenAIGPT3Dot5Turbo = "openai:gpt-3.5-turbo" | ||
ModelOpenAIGPT3TextDavinci003 = "openai:text-davinci-003" | ||
ModelOpenAIAdaEmbeddingV2 = "openai:text-embedding-ada-002" | ||
) | ||
|
||
type ( | ||
Model struct { | ||
Provider string `yaml:"provider"` | ||
ProviderModel string `yaml:"provider_model"` | ||
MaxToken int `yaml:"max_token"` | ||
PromptPriceUSD decimal.Decimal `yaml:"prompt_price_usd"` | ||
CompletionPriceUSD decimal.Decimal `yaml:"completion_price_usd"` | ||
PriceUSD decimal.Decimal `yaml:"price_usd"` | ||
|
||
Props struct { | ||
IsOpenAIChatModel bool `yaml:"is_openai_chat_model"` | ||
IsOpenAICompletionModel bool `yaml:"is_openai_completion_model"` | ||
IsOpenAIEmbeddingModel bool `yaml:"is_openai_embedding_model"` | ||
} | ||
} | ||
|
||
ModelStore interface { | ||
GetModel(ctx context.Context, name string) (*Model, error) | ||
} | ||
) | ||
|
||
func (m *Model) CalculateTokenCost(promptCount, completionCount int64) decimal.Decimal { | ||
pc := decimal.NewFromInt(promptCount) | ||
cc := decimal.NewFromInt(completionCount) | ||
|
||
if m.PriceUSD.IsPositive() { | ||
return m.PriceUSD.Mul(pc.Add(cc)) | ||
} | ||
if m.PromptPriceUSD.IsPositive() && m.CompletionPriceUSD.IsPositive() { | ||
return m.PromptPriceUSD.Mul(pc).Add(m.CompletionPriceUSD.Mul(cc)) | ||
} | ||
return decimal.Zero | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package model | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"fmt" | ||
|
||
"github.com/pandodao/botastic/core" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type ( | ||
store struct { | ||
modelMap map[string]*core.Model | ||
} | ||
) | ||
|
||
//go:embed models.yaml | ||
var modelsConfig string | ||
|
||
func New() *store { | ||
modelMap, err := LoadModels() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return &store{ | ||
modelMap: modelMap, | ||
} | ||
} | ||
|
||
func LoadModels() (map[string]*core.Model, error) { | ||
models := []*core.Model{} | ||
modelMap := make(map[string]*core.Model) | ||
|
||
if err := yaml.Unmarshal([]byte(modelsConfig), &models); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, m := range models { | ||
key := fmt.Sprintf("%s:%s", m.Provider, m.ProviderModel) | ||
modelMap[key] = m | ||
|
||
if m.Provider == core.ModelProviderOpenAI { | ||
switch m.ProviderModel { | ||
case "gpt-4", "gpt-4-32k", "gpt-4-0314", "gpt-4-32k-0314", "gpt-3.5-turbo", "gpt-3.5-turbo-0301": | ||
m.Props.IsOpenAIChatModel = true | ||
case "text-davinci-003": | ||
m.Props.IsOpenAICompletionModel = true | ||
case "text-embedding-ada-002": | ||
m.Props.IsOpenAIEmbeddingModel = true | ||
} | ||
} | ||
|
||
// backward compatibility | ||
if m.Provider == core.ModelProviderOpenAI { | ||
modelMap[m.ProviderModel] = m | ||
} | ||
} | ||
|
||
return modelMap, nil | ||
} | ||
|
||
func (s *store) GetModel(ctx context.Context, name string) (*core.Model, error) { | ||
if model, ok := s.modelMap[name]; ok { | ||
return model, nil | ||
} | ||
return nil, core.ErrInvalidModel | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
- provider: openai | ||
provider_model: gpt-4 | ||
prompt_price_usd: 0.00003 # $0.03 / 1K tokens | ||
completion_price_usd: 0.00006 # $0.06 / 1K tokens | ||
max_token: 8192 | ||
- provider: openai | ||
provider_model: gpt-4-0314 | ||
prompt_price_usd: 0.00003 # $0.03 / 1K tokens | ||
completion_price_usd: 0.00006 # $0.06 / 1K tokens | ||
max_token: 8192 | ||
- provider: openai | ||
provider_model: gpt-4-32k | ||
prompt_price_usd: 0.00006 # $0.06 / 1K tokens | ||
completion_price_usd: 0.00012 # $0.12 / 1K tokens | ||
max_token: 32768 | ||
- provider: openai | ||
provider_model: gpt-4-32k-0314 | ||
prompt_price_usd: 0.00006 # $0.06 / 1K tokens | ||
completion_price_usd: 0.00012 # $0.12 / 1K tokens | ||
max_token: 32768 | ||
- provider: openai | ||
provider_model: gpt-3.5-turbo | ||
price_usd: 0.000002 # $0.002 per 1000 tokens | ||
max_token: 4096 | ||
- provider: openai | ||
provider_model: gpt-3.5-turbo-0301 | ||
price_usd: 0.000002 # $0.002 per 1000 tokens | ||
max_token: 4096 | ||
- provider: openai | ||
provider_model: text-davinci-003 | ||
price_usd: 0.00002 # $0.02 per 1000 tokens | ||
max_token: 4097 | ||
- provider: openai | ||
provider_model: text-embedding-ada-002 | ||
price_usd: 0.0000004 # 0.0004 per 1000 tokens | ||
max_token: 2049 |
Oops, something went wrong.