api: support a MaxTokens parameter

This commit is contained in:
mappu 2023-04-08 16:03:59 +12:00
parent 6c6a5c602e
commit 0c96f2bf6b
1 changed files with 12 additions and 1 deletions

13
api.go
View File

@ -36,6 +36,7 @@ func (this *Application) POST_Chat(w http.ResponseWriter, r *http.Request) {
ConversationID string
APIKey string
Content string
MaxTokens int
}
var apiParams requestBody
err := json.NewDecoder(r.Body).Decode(&apiParams)
@ -44,6 +45,11 @@ func (this *Application) POST_Chat(w http.ResponseWriter, r *http.Request) {
return
}
if apiParams.MaxTokens < 0 {
http.Error(w, "MaxTokens should be 0 or positive", 400)
return
}
// Verify API key
// TODO
@ -94,7 +100,12 @@ func (this *Application) POST_Chat(w http.ResponseWriter, r *http.Request) {
// We've consumed all the input.
for i := int(llast_n_tokens_used_size); i < ParamContextSize; i += 1 {
EndPos := ParamContextSize
if apiParams.MaxTokens != 0 && (int(llast_n_tokens_used_size)+apiParams.MaxTokens) < ParamContextSize {
EndPos = int(llast_n_tokens_used_size) + apiParams.MaxTokens
}
for i := int(llast_n_tokens_used_size); i < EndPos; i += 1 {
if err := r.Context().Err(); err != nil {
return
}