Skip to content

Commit

Permalink
Add extra_rate config
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh authored and lyricat committed Mar 23, 2023
1 parent 672daec commit 0ae6ecc
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/botastic
/test.db
/config.yaml
/scripts/deploy.sh
4 changes: 3 additions & 1 deletion cmd/httpd/httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func NewCmdHttpd() *cobra.Command {
}
indexes := index.New(ctx, milvusClient)

userz := userServ.New(userServ.Config{}, client, users)
userz := userServ.New(userServ.Config{
ExtraRate: cfg.Sys.ExtraRate,
}, client, users)
indexService := indexServ.NewService(ctx, gptHandler, indexes, userz)

middlewarez := middlewareServ.New(middlewareServ.Config{}, indexService)
Expand Down
2 changes: 0 additions & 2 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/pandodao/botastic/cmd/gen"
"github.com/pandodao/botastic/cmd/httpd"
"github.com/pandodao/botastic/cmd/migrate"
"github.com/pandodao/botastic/cmd/worker"
"github.com/pandodao/botastic/cmdutil"
"github.com/pandodao/botastic/config"
"github.com/pandodao/botastic/session"
Expand Down Expand Up @@ -70,7 +69,6 @@ func NewCmdRoot(version string) *cobra.Command {
cmd.AddCommand(migrate.NewCmdMigrate())
cmd.AddCommand(gen.NewCmdGen())
cmd.AddCommand(app.NewCmdApp())
cmd.AddCommand(worker.NewCmdWorker())

return cmd
}
132 changes: 0 additions & 132 deletions cmd/worker/worker.go

This file was deleted.

6 changes: 6 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ openai:

sys:
secret_key: ""
extra_rate: 0.1

milvus:
address: "localhost:19530"
Expand All @@ -18,6 +19,11 @@ mixpay:
settlement_asset_id: "31d2ea9c-95eb-3355-b65b-ba096853bc18"
callback_url: "$API_HOST/api/callback/mixpay"

order_syncer:
interval: 1s
check_interval: 10s
cancel_interval: 2h

auth:
jwt_secret: "abc123"
mixin_client_secret: "...."
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type DBConfig struct {
}

type System struct {
SecretKey string `yaml:"secret_key"`
ExtraRate float64 `yaml:"extra_rate"`
SecretKey string `yaml:"secret_key"`
}

type Auth struct {
Expand Down
2 changes: 1 addition & 1 deletion service/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *serviceImpl) createEmbeddingsWithLimit(ctx context.Context, req gogpt.E

resp, err := s.gptHandler.CreateEmbeddings(ctx, req)
if err == nil {
if err := s.userz.ConsumeCreditsByModel(ctx, userID, "text-embedding-ada-002", uint64(resp.Usage.TotalTokens)); err != nil {
if err := s.userz.ConsumeCreditsByModel(ctx, userID, gogpt.AdaEmbeddingV2.String(), uint64(resp.Usage.TotalTokens)); err != nil {
log.Printf("ConsumeCredits error: %v\n", err)
}
}
Expand Down
12 changes: 8 additions & 4 deletions service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pandodao/botastic/core"
gogpt "github.com/sashabaranov/go-gpt3"
"github.com/shopspring/decimal"
"gorm.io/gorm"

Expand All @@ -28,7 +29,7 @@ func New(
}

type Config struct {
MixinClientSecret string
ExtraRate float64
}

type UserService struct {
Expand Down Expand Up @@ -149,20 +150,23 @@ func (s *UserService) Topup(ctx context.Context, user *core.User, amount decimal
func (s *UserService) ConsumeCreditsByModel(ctx context.Context, userID uint64, model string, tokenCount uint64) error {
price := decimal.Zero
switch model {
case "gpt-3.5-turbo":
case gogpt.GPT3Dot5Turbo:
// $0.002 per 1000 tokens
price = decimal.NewFromFloat(0.000002)
case "text-davinci-003":
case gogpt.GPT3TextDavinci003:
// $0.02 per 1000 tokens
price = decimal.NewFromFloat(0.00002)
case "text-embedding-ada-002":
case gogpt.AdaEmbeddingV2.String():
// $0.0004 per 1000 tokens
price = decimal.NewFromFloat(0.0000004)
default:
return core.ErrInvalidModel
}

credits := price.Mul(decimal.NewFromInt(int64(tokenCount)))
if s.cfg.ExtraRate > 0 {
credits = credits.Mul(decimal.NewFromFloat(1 + s.cfg.ExtraRate))
}
fmt.Printf("model: %v, price: $%s, token: %d, credits: $%s\n", model, price.StringFixed(8), tokenCount, credits.StringFixed(8))
return s.ConsumeCredits(ctx, userID, credits)
}
Expand Down
7 changes: 5 additions & 2 deletions worker/rotater/rotater.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (w *Worker) run(ctx context.Context) error {
}

switch bot.Model {
case "gpt-3.5-turbo", "gpt-3.5-turbo-0301":
case gogpt.GPT3Dot5Turbo:
// chat completion
request := gogpt.ChatCompletionRequest{
Model: bot.Model,
Expand All @@ -156,7 +156,7 @@ func (w *Worker) run(ctx context.Context) error {
}

turnReq.ChatRequest = &request
default:
case gogpt.GPT3TextDavinci003:
// text completion
prompt := bot.GetPrompt(conv, turn.Request)
request := gogpt.CompletionRequest{
Expand All @@ -167,6 +167,9 @@ func (w *Worker) run(ctx context.Context) error {
User: conv.GetKey(),
}
turnReq.Request = &request
default:
w.UpdateConvTurnAsError(ctx, turn.ID, fmt.Errorf("unsupported model: %s", bot.Model).Error())
continue
}

if turn.Status == core.ConvTurnStatusInit {
Expand Down

0 comments on commit 0ae6ecc

Please sign in to comment.