This commit is contained in:
Sakurasan
2023-04-26 03:56:29 +08:00
parent a34e579aa8
commit a100d75c00
2 changed files with 11 additions and 7 deletions

View File

@@ -401,7 +401,8 @@ func HandleProy(c *gin.Context) {
if resp.StatusCode == 200 && localuser { if resp.StatusCode == 200 && localuser {
if isStream { if isStream {
chatlog.CompletionCount = NumTokensFromStr(<-fetchResponseContent(resbuf, reader), chatreq.Model) chatdata := <-fetchResponseContent(resbuf, reader)
chatlog.CompletionCount = NumTokensFromStr(chatdata, chatreq.Model)
chatlog.TotalTokens = chatlog.PromptCount + chatlog.CompletionCount chatlog.TotalTokens = chatlog.PromptCount + chatlog.CompletionCount
} else { } else {
reader.WriteTo(resbuf) reader.WriteTo(resbuf)
@@ -410,7 +411,7 @@ func HandleProy(c *gin.Context) {
chatlog.CompletionCount = chatres.Usage.CompletionTokens chatlog.CompletionCount = chatres.Usage.CompletionTokens
chatlog.TotalTokens = chatres.Usage.TotalTokens chatlog.TotalTokens = chatres.Usage.TotalTokens
} }
chatlog.Cost = Cost(chatlog.Model, chatlog.PromptCount, chatlog.CompletionCount) chatlog.Cost = fmt.Sprintf("%.6f", Cost(chatlog.Model, chatlog.PromptCount, chatlog.CompletionCount))
if err := store.Record(&chatlog); err != nil { if err := store.Record(&chatlog); err != nil {
log.Println(err) log.Println(err)
} }
@@ -484,14 +485,17 @@ func HandleReverseProxy(c *gin.Context) {
} }
func Cost(model string, promptCount, completionCount int) float64 { func Cost(model string, promptCount, completionCount int) float64 {
var cost float64 var cost, prompt, completion float64
prompt = float64(promptCount)
completion = float64(completionCount)
switch model { switch model {
case "gpt-3.5-turbo", "gpt-3.5-turbo-0301": case "gpt-3.5-turbo", "gpt-3.5-turbo-0301":
cost = 0.002 * float64((promptCount+completionCount)/1000) cost = 0.002 * float64((prompt+completion)/1000)
case "gpt-4", "gpt-4-0314": case "gpt-4", "gpt-4-0314":
cost = 0.03*float64(promptCount/1000) + 0.06*float64(completionCount/1000) cost = 0.03*float64(prompt/1000) + 0.06*float64(completion/1000)
case "gpt-4-32k", "gpt-4-32k-0314": case "gpt-4-32k", "gpt-4-32k-0314":
cost = 0.06*float64(promptCount/1000) + 0.12*float64(completionCount/1000) cost = 0.06*float64(prompt/1000) + 0.12*float64(completion/1000)
} }
return cost return cost
} }

View File

@@ -73,7 +73,7 @@ type Tokens struct {
PromptCount int PromptCount int
CompletionCount int CompletionCount int
TotalTokens int TotalTokens int
Cost float64 Cost string
Model string Model string
PromptHash string PromptHash string
} }