From 2fa3f77ab8d88d43487b91a362e6737421b37614 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Mon, 6 Feb 2023 00:14:12 +0800 Subject: [PATCH] fix: prompt build bug --- README.md | 2 +- bot/openai/open_ai_bot.py | 11 ++++++++--- config-template.json | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e1f33ca..1c24a75 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ cp config-template.json config.json + 对于图像生成,在满足个人或群组触发条件外,还需要额外的关键词前缀来触发,对应配置 `image_create_prefix ` + 关于OpenAI对话及图片接口的参数配置(内容自由度、回复字数限制、图片大小等),可以参考 [对话接口](https://beta.openai.com/docs/api-reference/completions) 和 [图像接口](https://beta.openai.com/docs/api-reference/completions) 文档直接在 [代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/bot/openai/open_ai_bot.py) `bot/openai/open_ai_bot.py` 中进行调整。 + `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话) -+ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的人格设定,你可以为他塑造任何形象 ++ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的人格设定 (需要注意的是,有了人设后知识面也会相对变弱,) ## 运行 diff --git a/bot/openai/open_ai_bot.py b/bot/openai/open_ai_bot.py index 0aedc4d..d0faacc 100644 --- a/bot/openai/open_ai_bot.py +++ b/bot/openai/open_ai_bot.py @@ -4,6 +4,7 @@ from bot.bot import Bot from config import conf from common.log import logger import openai +from datetime import date user_session = dict() @@ -25,14 +26,15 @@ class OpenAIBot(Bot): new_query = Session.build_session_query(query, from_user_id) logger.debug("[OPEN_AI] session query={}".format(new_query)) - reply_content = self.reply_text(new_query, query) + reply_content = self.reply_text(new_query, from_user_id) + logger.debug("[OPEN_AI] new_query={}, user={}".format(new_query, from_user_id)) Session.save_session(query, reply_content, from_user_id) return reply_content elif context.get('type', None) == 'IMAGE_CREATE': return self.create_img(query) - def reply_text(self, query, origin_query): + def reply_text(self, query, user_id): try: response = openai.Completion.create( model="text-davinci-003", # 对话模型的名称 @@ -47,6 +49,7 @@ class OpenAIBot(Bot): res_content = response.choices[0]["text"].strip().rstrip("<|im_end|>") except Exception as e: logger.exception(e) + Session.clear_session(user_id) return None logger.info("[OPEN_AI] reply={}".format(res_content)) return res_content @@ -124,7 +127,7 @@ class Session(object): if session: for conversation in session: prompt += "Q: " + conversation["question"] + "\n\n\nA: " + conversation["answer"] + "<|im_end|>\n" - prompt += "Q: " + query + "\nA: " + prompt += "Q: " + query + "\nA: " return prompt else: return prompt + "Q: " + query + "\nA: " @@ -139,6 +142,8 @@ class Session(object): conversation["question"] = query conversation["answer"] = answer session = user_session.get(user_id) + logger.debug(conversation) + logger.debug(session) if session: # append conversation session.append(conversation) diff --git a/config-template.json b/config-template.json index aaececb..9e3bc48 100644 --- a/config-template.json +++ b/config-template.json @@ -6,5 +6,5 @@ "group_name_white_list": ["ChatGPT测试群", "ChatGPT测试群2"], "image_create_prefix": ["画", "看", "找"], "conversation_max_tokens": 1000, - "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你乐于回答人们的各种问题" + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你乐于回答人们的任何问题" }