From 733f9d1f10c68aff49067b9aee300d41a81217fe Mon Sep 17 00:00:00 2001 From: ubuntu Date: Mon, 6 Mar 2023 00:51:53 +0800 Subject: [PATCH] fix: tokens limit optimization --- bot/chatgpt/chat_gpt_bot.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 9909f74..22b13df 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -66,8 +66,7 @@ class ChatGPTBot(Bot): frequency_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 presence_penalty=0.0, # [-2,2]之间,该值越大则更倾向于产生不同的内容 ) - # res_content = response.choices[0]['text'].strip().replace('<|endoftext|>', '') - logger.info(response.choices[0]['message']['content']) + logger.info("[ChatGPT] reply={}, total_tokens={}".format(response.choices[0]['message']['content'], response["usage"]["total_tokens"])) return {"total_tokens": response["usage"]["total_tokens"], "completion_tokens": response["usage"]["completion_tokens"], "content": response.choices[0]['message']['content']} @@ -159,18 +158,20 @@ class Session(object): # discard exceed limit conversation Session.discard_exceed_conversation(session, max_tokens, total_tokens) + @staticmethod def discard_exceed_conversation(session, max_tokens, total_tokens): - dec_tokens=int(total_tokens) + dec_tokens = int(total_tokens) # logger.info("prompt tokens used={},max_tokens={}".format(used_tokens,max_tokens)) while dec_tokens > max_tokens: # pop first conversation - if len(session) > 0: - session.pop(0) + if len(session) > 3: + session.pop(1) + session.pop(1) else: break - dec_tokens=dec_tokens-max_tokens + dec_tokens = dec_tokens - max_tokens @staticmethod def clear_session(user_id): @@ -178,4 +179,4 @@ class Session(object): @staticmethod def clear_all_session(): - user_session.clear() \ No newline at end of file + user_session.clear()