Skip to content

Commit

Permalink
Fix expend cost.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 27, 2023
1 parent 279c5ca commit 7bd32ff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/api/publication.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (a *Publication) Create(ctx *gear.Context) error {
CID: src.CID,
Language: *input.ToLanguage,
Version: src.Version,
Model: input.Model,
Price: bll.Pricing(input.Model),
Tokens: teOutput.Tokens,
}
Expand Down
6 changes: 3 additions & 3 deletions src/bll/walletbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func Pricing(model string) float64 {
return pricing[model]
}

func CostWEN(model string, tokens uint32) int64 {
f := pricing[model] * float64(tokens) / 1000
func CostWEN(price float64, tokens uint32) int64 {
f := price * float64(tokens) / 1000
c := int64(f)
if f > float64(c) {
c += 1
Expand Down Expand Up @@ -106,7 +106,7 @@ func (b *Walletbase) Expend(ctx context.Context, uid util.ID, input *ExpendPaylo

ex := ExpendInput{
UID: uid,
Amount: CostWEN(input.Model, input.Tokens),
Amount: CostWEN(input.Price, input.Tokens),
Description: "Create Publication",
Payload: data,
}
Expand Down
12 changes: 7 additions & 5 deletions src/bll/walletbase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func TestModel(t *testing.T) {
assert.Equal(float64(10.0), Pricing("gpt4"))
assert.Equal(float64(0), Pricing("davinci"))

assert.Equal(int64(1), CostWEN(DefaultModel, 100))
assert.Equal(int64(2), CostWEN(DefaultModel, 1600))
assert.Equal(int64(3), CostWEN(DefaultModel, 2100))
assert.Equal(int64(1), CostWEN("gpt4", 100))
assert.Equal(int64(15), CostWEN("gpt4", 1500))
assert.Equal(int64(1), CostWEN(Pricing(DefaultModel), 100))
assert.Equal(int64(2), CostWEN(Pricing(DefaultModel), 1600))
assert.Equal(int64(3), CostWEN(Pricing(DefaultModel), 2100))
assert.Equal(int64(3), CostWEN(Pricing(DefaultModel), 2597))
assert.Equal(int64(1), CostWEN(Pricing("gpt4"), 100))
assert.Equal(int64(15), CostWEN(Pricing("gpt4"), 1500))
assert.Equal(int64(26), CostWEN(Pricing("gpt4"), 2597))
}

0 comments on commit 7bd32ff

Please sign in to comment.