From 5061a205f535d72137a43d21405929b5d85b8159 Mon Sep 17 00:00:00 2001 From: Xun Cai Date: Fri, 24 Mar 2023 18:51:16 +1100 Subject: [PATCH] support an optional setting `max_conversation_history_per_session` for chatgpt model --- README.md | 1 + model/openai/chatgpt_model.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 51f14a4..9843ea0 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ cp config-template.json config.json + `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。 + default: ["#清除记忆"] ++ `max_history_per_session`[optional]: 对话最大记忆长度,超过该长度则清理前面的记忆。 ## 二、选择模型 diff --git a/model/openai/chatgpt_model.py b/model/openai/chatgpt_model.py index 9a8f3d6..7cf6cfc 100644 --- a/model/openai/chatgpt_model.py +++ b/model/openai/chatgpt_model.py @@ -196,6 +196,7 @@ class Session(object): @staticmethod def save_session(query, answer, user_id, used_tokens=0): max_tokens = model_conf(const.OPEN_AI).get('conversation_max_tokens') + max_history_per_session = model_conf(const.OPEN_AI).get('max_history_per_session', None) if not max_tokens or max_tokens > 4000: # default value max_tokens = 1000 @@ -210,6 +211,10 @@ class Session(object): session.pop(1) session.pop(1) + if max_history_per_session is not None: + while len(session) > max_history_per_session + 1: + session.pop(1) + @staticmethod def clear_session(user_id): user_session[user_id] = []