mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 03:32:33 +08:00
feat: 部分日志 #3
This commit is contained in:
parent
b39bc076c9
commit
7b17b316a3
|
@ -46,7 +46,7 @@ git clone https://github.com/RockChinQ/QChatGPT
|
|||
2. 安装依赖
|
||||
|
||||
```bash
|
||||
pip install pymysql yiri-mirai openai
|
||||
pip install pymysql yiri-mirai openai colorlog
|
||||
```
|
||||
|
||||
3. 运行一次主程序,生成配置文件
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import logging
|
||||
|
||||
# Mirai的配置
|
||||
# 请到配置mirai的步骤中的教程查看每个字段的信息
|
||||
# host: 运行mirai的主机地址
|
||||
|
@ -35,18 +37,22 @@ openai_config = {
|
|||
# 不了解的话请不要修改,具体请查看OpenAI的文档
|
||||
completion_api_params = {
|
||||
"model": "text-davinci-003",
|
||||
"temperature": 0.9,
|
||||
"max_tokens": 1024,
|
||||
"temperature": 0.6,
|
||||
"max_tokens": 256,
|
||||
"top_p": 1,
|
||||
"frequency_penalty": 0.4,
|
||||
"presence_penalty": 0.3,
|
||||
}
|
||||
|
||||
# 消息处理的超时时间
|
||||
process_message_timeout = 45
|
||||
process_message_timeout = 20
|
||||
|
||||
# 消息处理超时重试次数
|
||||
retry_times = 3
|
||||
|
||||
# 每个会话的过期时间
|
||||
session_expire_time = 60 * 60 * 24 * 7
|
||||
# 默认值20分钟
|
||||
session_expire_time = 60 * 20
|
||||
|
||||
# 日志级别
|
||||
logging_level = logging.DEBUG
|
||||
|
|
32
main.py
32
main.py
|
@ -9,6 +9,17 @@ import pkg.database.manager
|
|||
import pkg.openai.session
|
||||
import pkg.qqbot.manager
|
||||
|
||||
import logging
|
||||
import colorlog
|
||||
|
||||
|
||||
log_colors_config = {
|
||||
'DEBUG': 'green', # cyan white
|
||||
'INFO': 'white',
|
||||
'WARNING': 'yellow',
|
||||
'ERROR': 'red',
|
||||
'CRITICAL': 'bold_red',
|
||||
}
|
||||
|
||||
def init_db():
|
||||
import config
|
||||
|
@ -27,6 +38,24 @@ def main():
|
|||
assert os.path.exists('config.py')
|
||||
import config
|
||||
|
||||
logging.basicConfig(level=config.logging_level, # 设置日志输出格式
|
||||
filename='qchatgpt.log', # log日志输出的文件位置和文件名
|
||||
format="[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : %(message)s",
|
||||
# 日志输出的格式
|
||||
# -8表示占位符,让输出左对齐,输出长度都为8位
|
||||
datefmt="%Y-%m-%d %H:%M:%S" # 时间输出的格式
|
||||
)
|
||||
sh = logging.StreamHandler()
|
||||
sh.setLevel(config.logging_level)
|
||||
sh.setFormatter(colorlog.ColoredFormatter(
|
||||
fmt="%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s (%(lineno)d) - [%(levelname)s] : "
|
||||
"%(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
log_colors=log_colors_config
|
||||
))
|
||||
logging.getLogger().addHandler(sh)
|
||||
|
||||
|
||||
# 主启动流程
|
||||
openai_interact = pkg.openai.manager.OpenAIInteract(config.openai_config['api_key'], config.completion_api_params)
|
||||
|
||||
|
@ -49,12 +78,15 @@ if __name__ == '__main__':
|
|||
sys.exit(0)
|
||||
main()
|
||||
|
||||
logging.info('程序启动完成')
|
||||
|
||||
while True:
|
||||
try:
|
||||
time.sleep(86400)
|
||||
except KeyboardInterrupt:
|
||||
try:
|
||||
for session in pkg.openai.session.sessions:
|
||||
logging.info('持久化session: %s', session)
|
||||
pkg.openai.session.sessions[session].persistence()
|
||||
except Exception as e:
|
||||
if not isinstance(e, KeyboardInterrupt):
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import logging
|
||||
import time
|
||||
|
||||
import pkg.openai.manager
|
||||
|
@ -19,6 +20,8 @@ def load_sessions():
|
|||
session_data = db_inst.load_valid_sessions()
|
||||
|
||||
for session_name in session_data:
|
||||
logging.info('加载session: {}'.format(session_name))
|
||||
|
||||
temp_session = Session(session_name)
|
||||
temp_session.name = session_name
|
||||
temp_session.create_timestamp = session_data[session_name]['create_timestamp']
|
||||
|
|
|
@ -4,11 +4,16 @@ from func_timeout import func_set_timeout, FunctionTimedOut
|
|||
import datetime
|
||||
import logging
|
||||
|
||||
help_text = """帮助信息:
|
||||
help_text = """此机器人通过调用OpenAI的GPT-3大型语言模型生成回复,不具有情感。
|
||||
你可以用自然语言与其交流,回复的消息中[GPT]开头的为模型生成的语言,[bot]开头的为程序提示。
|
||||
你可以通过QQ 1010553892 联系作者
|
||||
欢迎到github.com/RockChinQ/QChatGPT 给个star
|
||||
|
||||
帮助信息:
|
||||
!help - 显示帮助
|
||||
!reset - 重置会话
|
||||
!last - 切换到上一次的对话
|
||||
!next - 切换到下一次的对话
|
||||
!last - 切换到前一次的对话
|
||||
!next - 切换到后一次的对话
|
||||
!prompt - 显示当前对话所有内容
|
||||
!list - 列出所有历史会话"""
|
||||
|
||||
|
@ -61,6 +66,9 @@ class QQBotManager:
|
|||
session_name = "{}_{}".format(launcher_type, launcher_id)
|
||||
|
||||
if text_message.startswith('!') or text_message.startswith("!"): # 指令
|
||||
|
||||
logging.info("[{}]发起指令:{}".format(session_name, text_message[:min(20, len(text_message))]+("..." if len(text_message) > 20 else "")))
|
||||
|
||||
cmd = text_message[1:].strip().split(' ')[0]
|
||||
|
||||
params = text_message[1:].strip().split(' ')[1:]
|
||||
|
@ -72,18 +80,18 @@ class QQBotManager:
|
|||
elif cmd == 'last':
|
||||
result = pkg.openai.session.get_session(session_name).last_session()
|
||||
if result is None:
|
||||
reply = "[bot]没有上一次的对话"
|
||||
reply = "[bot]没有前一次的对话"
|
||||
else:
|
||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
reply = "[bot]已切换到上一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
reply = "[bot]已切换到前一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
elif cmd == 'next':
|
||||
result = pkg.openai.session.get_session(session_name).next_session()
|
||||
if result is None:
|
||||
reply = "[bot]没有下一次的对话"
|
||||
reply = "[bot]没有后一次的对话"
|
||||
else:
|
||||
datetime_str = datetime.datetime.fromtimestamp(result.create_timestamp).strftime('%Y-%m-%d %H:%M:%S')
|
||||
reply = "[bot]已切换到下一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
reply = "[bot]已切换到后一次的对话:\n创建时间:{}\n".format(datetime_str) + result.prompt[:min(100, len(result.prompt))] + \
|
||||
("..." if len(result.prompt) > 100 else "#END#")
|
||||
elif cmd == 'prompt':
|
||||
reply = "[bot]当前对话所有内容:\n" + pkg.openai.session.get_session(session_name).prompt
|
||||
|
@ -111,19 +119,24 @@ class QQBotManager:
|
|||
if results[i]['create_timestamp'] == pkg.openai.session.get_session(session_name).create_timestamp:
|
||||
current = i + page * 10
|
||||
|
||||
reply += "以上信息倒序排列"
|
||||
reply += "\n以上信息倒序排列"
|
||||
if current != -1:
|
||||
reply += ",当前会话是 #{}\n".format(current)
|
||||
else:
|
||||
reply += ",当前处于全新会话或不在此页"
|
||||
|
||||
else: # 消息
|
||||
logging.info("[{}]发送消息:{}".format(session_name, text_message[:min(20, len(text_message))]+("..." if len(text_message) > 20 else "")))
|
||||
|
||||
session = pkg.openai.session.get_session(session_name)
|
||||
try:
|
||||
reply = "[GPT]" + session.append(text_message)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
reply = "[bot]err:{}".format(e)
|
||||
|
||||
logging.info("回复[{}]消息:{}".format(session_name, reply[:min(100, len(reply))]+("..." if len(reply) > 100 else "")))
|
||||
|
||||
return reply
|
||||
|
||||
async def on_person_message(self, event: MessageEvent):
|
||||
|
|
Loading…
Reference in New Issue
Block a user