From 8f4456c4eff1127038ccc79cae27e2bf2453609e Mon Sep 17 00:00:00 2001 From: mibody2 Date: Sun, 19 May 2024 22:22:03 +0800 Subject: [PATCH] feat: jb can set the locale for chat --- config.json.example | 3 ++- main.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/config.json.example b/config.json.example index 1712968..26c77b0 100644 --- a/config.json.example +++ b/config.json.example @@ -12,5 +12,6 @@ "chat_api_project": "", "chat_max_tokens": 4096, "chat_model_default": "gpt-4o", - "chat_model_map": {} + "chat_model_map": {}, + "chat_locale": "zh_CN" } \ No newline at end of file diff --git a/main.go b/main.go index f77175f..1a36a42 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ type config struct { ChatMaxTokens int `json:"chat_max_tokens"` ChatModelDefault string `json:"chat_model_default"` ChatModelMap map[string]string `json:"chat_model_map"` + ChatLocale string `json:"chat_locale"` } func readConfig() *config { @@ -171,6 +172,19 @@ func (s *ProxyService) completions(c *gin.Context) { model = s.cfg.ChatModelDefault } body, _ = sjson.SetBytes(body, "model", model) + + if !gjson.GetBytes(body, "function_call").Exists() { + messages := gjson.GetBytes(body, "messages").Array() + lastIndex := len(messages) - 1 + if !strings.Contains(messages[lastIndex].Get("content").String(), "Respond in the following locale") { + locale := s.cfg.ChatLocale + if locale == "" { + locale = "zh_CN" + } + body, _ = sjson.SetBytes(body, "messages."+strconv.Itoa(lastIndex)+".content", messages[lastIndex].Get("content").String()+"Respond in the following locale: "+locale+".") + } + } + body, _ = sjson.DeleteBytes(body, "intent") body, _ = sjson.DeleteBytes(body, "intent_threshold") body, _ = sjson.DeleteBytes(body, "intent_content")