From 0c96f2bf6b98d4a2c473df1f1e215a5bad1d9aa3 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 8 Apr 2023 16:03:59 +1200 Subject: [PATCH] api: support a MaxTokens parameter --- api.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 45e1980..7265ac8 100644 --- a/api.go +++ b/api.go @@ -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 }