support an optional setting max_conversation_history_per_session for chatgpt model

This commit is contained in:
Xun Cai
2023-03-24 18:51:16 +11:00
parent dc213d6af6
commit 5061a205f5
2 changed files with 6 additions and 0 deletions

View File

@@ -78,6 +78,7 @@ cp config-template.json config.json
+ `clear_memory_commands`: 对话内指令,主动清空前文记忆,字符串数组可自定义指令别名。
+ default: ["#清除记忆"]
+ `max_history_per_session`[optional]: 对话最大记忆长度,超过该长度则清理前面的记忆。
## 二、选择模型

View File

@@ -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] = []