Skip to content

Commit

Permalink
fix: now the input field can be array type now (close lobehub#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Jun 12, 2023
1 parent 8b2ef66 commit 7c7eb6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions controller/relay-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func countTokenMessages(messages []Message, model string) int {
return tokenNum
}

func countTokenInput(input any, model string) int {
switch input.(type) {
case string:
return countTokenText(input.(string), model)
case []string:
text := ""
for _, s := range input.([]string) {
text += s
}
return countTokenText(text, model)
}
return 0
}

func countTokenText(text string, model string) int {
tokenEncoder := getTokenEncoder(model)
token := tokenEncoder.Encode(text, nil, nil)
Expand Down
4 changes: 2 additions & 2 deletions controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type GeneralOpenAIRequest struct {
Temperature float64 `json:"temperature"`
TopP float64 `json:"top_p"`
N int `json:"n"`
Input string `json:"input"`
Input any `json:"input"`
}

type ChatRequest struct {
Expand Down Expand Up @@ -189,7 +189,7 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
case RelayModeCompletions:
promptTokens = countTokenText(textRequest.Prompt, textRequest.Model)
case RelayModeModeration:
promptTokens = countTokenText(textRequest.Input, textRequest.Model)
promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
}
preConsumedTokens := common.PreConsumedQuota
if textRequest.MaxTokens != 0 {
Expand Down

0 comments on commit 7c7eb6b

Please sign in to comment.