From 075342606f4e42a7a51d013efe7e2fcdcc6fe7a8 Mon Sep 17 00:00:00 2001 From: wozulong <> Date: Sat, 18 May 2024 19:55:51 +0800 Subject: [PATCH] reduce output tokens Signed-off-by: wozulong <> --- config.json.example | 1 + main.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/config.json.example b/config.json.example index 0d315b1..6c2a5d2 100644 --- a/config.json.example +++ b/config.json.example @@ -11,6 +11,7 @@ "chat_api_key": "sk-xxx", "chat_api_organization": "", "chat_api_project": "", + "chat_max_tokens": 4096, "chat_model_default": "gpt-4o", "chat_model_map": {} } \ No newline at end of file diff --git a/main.go b/main.go index e387ac2..4f55082 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ type config struct { ChatApiKey string `json:"chat_api_key"` ChatApiOrganization string `json:"chat_api_organization"` ChatApiProject string `json:"chat_api_project"` + ChatMaxTokens int `json:"chat_max_tokens"` ChatModelDefault string `json:"chat_model_default"` ChatModelMap map[string]string `json:"chat_model_map"` } @@ -183,6 +184,10 @@ func (s *ProxyService) completions(c *gin.Context) { body, _ = sjson.DeleteBytes(body, "intent_threshold") body, _ = sjson.DeleteBytes(body, "intent_content") + if int(gjson.GetBytes(body, "max_tokens").Int()) > s.cfg.ChatMaxTokens { + body, _ = sjson.SetBytes(body, "max_tokens", s.cfg.ChatMaxTokens) + } + proxyUrl := s.cfg.ChatApiBase + "/chat/completions" req, err := http.NewRequestWithContext(ctx, http.MethodPost, proxyUrl, io.NopCloser(bytes.NewBuffer(body))) if nil != err {