Compare commits

...

178 Commits

Author SHA1 Message Date
Soulter
19e3390083 Update README.md 2023-06-13 17:04:29 +08:00
Soulter
3015b90e12 bugfixes 2023-06-13 11:59:16 +08:00
Soulter
aa419f3ef9 perf: 去帮助中心部分指令显示 2023-06-13 11:54:44 +08:00
Soulter
954236c284 fix: 修复markdown宽度计算异常的问题 2023-06-13 11:54:20 +08:00
Soulter
72d6b3886b perf: markdown render 增大 fontsize 2023-06-13 11:44:34 +08:00
Soulter
a95046ecaf Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-06-13 10:09:33 +08:00
Soulter
ccdb11575b remove chore 2023-06-13 10:09:28 +08:00
Soulter
7e68b2f2be Update requirements.txt 2023-06-13 10:05:57 +08:00
Soulter
39efab1081 perf: enhanced markdown image render regex 2023-06-12 18:41:04 +08:00
Soulter
cc6707c8ce Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-06-12 18:26:16 +08:00
Soulter
09080adf84 perf: markdown渲染器支持渲染图片 2023-06-12 18:26:11 +08:00
Soulter
4cc72030c0 Update README.md 2023-06-12 08:32:05 +08:00
Soulter
a395902184 Update README.md 2023-06-12 08:30:58 +08:00
Soulter
5156f0584a Update README.md 2023-06-12 08:30:03 +08:00
Soulter
be171fe0d7 Update README.md 2023-06-12 08:14:55 +08:00
Soulter
ad4bf5e654 perf: update command add "update latest r" 2023-06-11 09:53:49 +08:00
Soulter
da7429ad62 perf: add markdown minheight 2023-06-11 09:51:53 +08:00
Soulter
b5f20ee282 chore: change fonts 2023-06-11 09:16:16 +08:00
Soulter
a9023d6f3a perf: 支持markdown渲染 2023-06-10 13:10:32 +00:00
Soulter
628b661a18 fix markdown 2023-06-10 13:05:22 +00:00
Soulter
638fe466f8 perf markdown 2023-06-10 12:51:34 +00:00
Soulter
a90adcf15c chore: change some markdown parameters 2023-06-10 12:24:37 +00:00
Soulter
7896066db6 perf: markdown perf 2023-06-10 12:22:32 +00:00
Soulter
b1314bcc31 perf: \t -> 4 blanks 2023-06-10 12:13:50 +00:00
Soulter
b1ecc929f2 perf: markdown render perf 2023-06-10 12:10:20 +00:00
Soulter
3aad42a886 perf: markdown render perf 2023-06-10 12:08:32 +00:00
Soulter
b6e87d3d31 perf: markdown render perf 2023-06-10 10:54:26 +00:00
Soulter
461eb4b9c7 perf: markdown render perf 2023-06-10 10:48:36 +00:00
Soulter
a89e92d5cc perf: markdown render perf 2023-06-10 10:33:14 +00:00
Soulter
6e69e88e91 perf: markdown render perf 2023-06-10 10:30:17 +00:00
Soulter
ae732c1dac perf: markdown render perf 2023-06-10 10:03:03 +00:00
Soulter
8e4a72c97b perf: markdown render perf 2023-06-10 10:02:23 +00:00
Soulter
bf0d82fe67 perf: markdown render perf 2023-06-10 10:01:23 +00:00
Soulter
987383f957 perf: markdown render perf 2023-06-10 10:00:22 +00:00
Soulter
c2cacf3281 perf: markdown render perf 2023-06-10 09:56:58 +00:00
Soulter
72878477dc perf: qq pic mode support markdown 2023-06-10 09:47:02 +00:00
Soulter
ad0d14420a feat: markdown render support 2023-06-10 09:39:37 +00:00
Soulter
5a7c60c81e fix: markdown render support 2023-06-10 09:38:19 +00:00
Soulter
6011840d1f feat: markdown render support 2023-06-10 09:32:49 +00:00
Soulter
9a2dffe299 feat: markdown render support 2023-06-10 09:27:02 +00:00
Soulter
e6770d2b12 Update README.md 2023-06-09 00:19:27 +08:00
Soulter
255db6ee57 Update README.md 2023-06-08 23:58:44 +08:00
Soulter
aa9ff99557 perf: better help 2023-06-06 12:31:14 +00:00
Soulter
5f024e9f30 fix: bugfixes 2023-06-06 12:28:55 +00:00
Soulter
cbdc7b7ce4 perf: better help 2023-06-06 12:23:48 +00:00
Soulter
5f636ca061 perf: improve text2img 2023-06-06 11:57:52 +00:00
Soulter
9fa3651170 perf: change word2img factors 2023-06-06 11:45:32 +00:00
Soulter
bba66788c3 fix: bugfixes 2023-06-06 11:41:26 +00:00
Soulter
200f3cce00 fix: bugfixes 2023-06-06 11:34:52 +00:00
Soulter
938490b739 fix: bugfixes 2023-06-06 11:31:08 +00:00
Soulter
e77e7b050a feat: QQ message plain texts to pic support #108 2023-06-06 11:21:55 +00:00
Soulter
bd2dbe5b63 feat:转发消息支持非文本类型 2023-06-03 14:21:47 +08:00
Soulter
c684d9cb4a fix: 修复某些插件调用send可能发生的错误 2023-06-03 10:49:12 +08:00
Soulter
7a39a9d45e feat: nick指令仅管理者能用 2023-06-01 22:09:51 +08:00
Soulter
2a3bb068db feat: bing支持自定义代理地址 2023-05-31 21:17:47 +08:00
Soulter
1aa4384ca3 perf: 优化日志输出长度限制 2023-05-31 20:31:11 +08:00
Soulter
3b26b7b26c feat: 将CmdConfig的一些方法改为静态方法 2023-05-31 10:25:39 +08:00
Soulter
3b097d662b perf: 增加支持查看新版配置文件的管理员指令newconfig 2023-05-31 10:18:08 +08:00
Soulter
c3acb3e77f feat: 支持修改入群欢迎 2023-05-31 10:07:15 +08:00
Soulter
55d58d30a8 fix: 修复手滑造成的启动报错 2023-05-29 16:40:02 +08:00
Soulter
020a8ace9f feat: 支持自定义qq回复折叠阈值
perf: 优化新版配置文件加载流程
2023-05-29 16:37:11 +08:00
Soulter
15f56ffc01 feat: 长文本支持折叠发送 #104 2023-05-29 01:10:37 +08:00
Soulter
3724659b32 perf: improve stater 2023-05-24 18:24:18 +08:00
Soulter
df77152581 chore: 更新说明 2023-05-23 23:11:24 +08:00
Soulter
339ea5f12a feat: 支持更多本地预设指令的图片化 2023-05-23 11:01:56 +08:00
Soulter
36f96ccc97 feat: 文字转图片的图片过期处理逻辑 2023-05-23 10:58:07 +08:00
Soulter
190e0a4971 feat: 支持文字转图片 2023-05-23 10:41:12 +08:00
Soulter
72638fac68 fix: 修复QQ频道@不回的问题 2023-05-23 07:58:03 +08:00
Soulter
807d19e381 fix: 修复gocq群聊时@无反应的问题 2023-05-22 20:54:31 +08:00
Soulter
10870172b4 fix: 修复私聊不回的问题 2023-05-22 20:17:53 +08:00
Soulter
1f7d3eccf9 fix: blank nick 2023-05-22 19:42:37 +08:00
Soulter
5fc58123bb fix: 修复频率限制消息识别的问题 2023-05-22 19:31:24 +08:00
Soulter
c84c9f4aaa fix: 修复gocq_loop 2023-05-22 18:47:33 +08:00
Soulter
cabe66fc0a perf: 优化gocq平台消息处理逻辑 2023-05-22 18:46:01 +08:00
Soulter
9f1315b06d perf: 优化gocq平台消息处理逻辑 2023-05-22 18:42:23 +08:00
Soulter
6f27f59730 fix: 修复GOCQ频道at报错的问题 2023-05-22 18:25:58 +08:00
Soulter
17815e7fe3 fix: 优化切换到未启动的模型报错的问题 2023-05-22 18:23:16 +08:00
Soulter
596ae80fea perf: 优化模型识别提示 2023-05-22 18:22:34 +08:00
Soulter
be2dc6ba70 feat: 指令操作不再需要在消息前加前缀
perf: 改善性能
2023-05-22 18:10:22 +08:00
Soulter
e5aa8c8270 fix: 修复群内欢迎 2023-05-21 11:12:50 +08:00
Soulter
7c5ac41c55 chore: 删除不必要的log日志 2023-05-21 11:02:00 +08:00
Soulter
c6cf1153c1 fix: 修复Windows下删除插件报错拒绝访问的问题;
修复权限组异常的问题
2023-05-21 11:00:59 +08:00
Soulter
a68338b651 perf: 优化bing报错提示 2023-05-21 10:23:50 +08:00
Soulter
bab46e912e fix: 修复默认昵称失效的问题;
修复启动时跳过管理者qq设置的问题
2023-05-21 10:18:51 +08:00
Soulter
4b158a1c89 feat: GOCQ适配QQ频道 2023-05-20 15:30:07 +08:00
Soulter
6894900e46 fix: 修复画画指令得到的图片风格像油画的问题 2023-05-20 14:27:02 +08:00
Soulter
2e11d6e007 perf: log perf 2023-05-18 22:21:29 +08:00
Soulter
348381be15 Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-05-18 22:15:07 +08:00
Soulter
9024c28e70 perf: fix some logs 2023-05-18 22:15:01 +08:00
Soulter
ae1702901b Update README.md 2023-05-18 08:34:41 +08:00
Soulter
c1c0df85e6 Update README.md 2023-05-17 20:36:54 +08:00
Soulter
f3c6d9c02b fix: draw command 2023-05-16 15:06:39 +08:00
Soulter
811a885411 fix: draw command 2023-05-16 15:04:55 +08:00
Soulter
b4ec28b71c Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-05-16 11:57:04 +08:00
Soulter
cdf4a5321b perf: 1.逆向ChatGPT库支持消息等待,不会回复忙碌。
2. 优化模型加载流程
2023-05-16 11:56:59 +08:00
Soulter
d83f155f80 Update README.md 2023-05-15 20:54:19 +08:00
Soulter
4c402ed5bd perf: 优化插件鉴别 2023-05-15 20:43:42 +08:00
Soulter
ec5aff8d0b fix: update helloworld default plugin 2023-05-15 20:14:14 +08:00
Soulter
eae0d6c422 fix: 修复一些奇怪的地方 2023-05-15 20:09:01 +08:00
Soulter
9c284b84b1 perf: 升级插件协议簇 2023-05-15 20:03:17 +08:00
Soulter
9f36e5ae05 perf: 在连接到go-cqhttp之前添加连接检测 2023-05-15 18:33:07 +08:00
Soulter
7caa380e54 perf: 优化控制台输出的长度限制 2023-05-14 21:58:45 +08:00
Soulter
41d81bb60e perf: 简化控制台字数 2023-05-14 20:54:56 +08:00
Soulter
454a74f4e1 perf: 颜色日志-优化控制台显示 2023-05-14 20:51:39 +08:00
Soulter
c5bdad02e5 fix: 修复ChatGPT逆向库回答报错的问题 2023-05-14 20:39:15 +08:00
Soulter
f46de3d518 perf: 颜色日志-美化控制台显示 2023-05-14 20:38:28 +08:00
Soulter
a3e21bea1a perf: 删除不必要的控制台信息显示 2023-05-14 19:54:47 +08:00
Soulter
d7e4707d5d perf: 简化控制台输出信息 2023-05-14 19:43:12 +08:00
Soulter
a78ebf2fd7 feat: plugin dev mode 2023-05-14 18:20:28 +08:00
Soulter
bd11541678 perf: 优化插件更新缓存策略 2023-05-14 18:16:12 +08:00
Soulter
0d99aa81e6 Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-05-14 17:40:16 +08:00
Soulter
f104d40d0a perf: 优化插件加载规则、更新插件接口规范
fix: 修复发言频率限制报错的问题
2023-05-14 17:40:13 +08:00
Soulter
0d69f8ab8a Update README.md 2023-05-13 17:03:32 +08:00
Soulter
66d1fc08b6 perf: 去除不必要的import 2023-05-13 14:25:16 +08:00
Soulter
e32fc27728 perf: 优化插件的删除逻辑 2023-05-13 14:23:05 +08:00
Soulter
eec890cd02 perf: 优化插件指令的卸载插件逻辑 2023-05-13 14:20:38 +08:00
Soulter
d30881e59b perf: 补充插件指令的帮助信息 2023-05-13 14:10:00 +08:00
Soulter
9afaf83368 perf: 优化插件指令的身份组鉴定 2023-05-13 14:07:40 +08:00
Soulter
33f9a9cfa0 feat: 支持显示插件列表和插件帮助 2023-05-13 14:04:15 +08:00
Soulter
bf72d5fa27 fix: 修复有依赖的插件拉取问题 2023-05-13 13:55:22 +08:00
Soulter
567c29bcd6 perf: 清除多余log 2023-05-13 13:45:04 +08:00
Soulter
dcdfe453fb perf: 优化插件类鉴定规则 2023-05-13 13:44:14 +08:00
Soulter
0d23c0900b perf: 优化插件卸载逻辑 2023-05-13 13:28:18 +08:00
Soulter
86eda7bdf8 perf: 优化插件缓存逻辑 2023-05-13 13:25:56 +08:00
Soulter
1e46525b0f perf: 优化pip更新逻辑 2023-05-13 13:02:24 +08:00
Soulter
8d41efea4d Update README.md 2023-05-13 11:43:03 +08:00
Soulter
f15d0eb0eb Update README.md 2023-05-13 11:10:56 +08:00
Soulter
1795362bcd perf: 优化插件调用逻辑 2023-05-13 11:03:16 +08:00
Soulter
2bf9c82617 perf: 更好的插件处理逻辑和更开放的插件功能 2023-05-13 10:54:57 +08:00
Soulter
33793a2053 chore: 更新版本号 2023-05-12 09:21:57 +08:00
Soulter
656fe14af4 perf: 优化身份组鉴定 2023-05-12 09:15:32 +08:00
Soulter
46197d49a4 perf: 调换语言模型启动顺序 2023-05-12 09:08:06 +08:00
Soulter
843ab56f50 perf: 优化身份组 2023-05-12 09:02:29 +08:00
Soulter
6b4b52f3c5 perf: 完善身份组功能 2023-05-11 22:56:38 +08:00
Soulter
392e5cd592 chore: 添加默认插件 2023-05-11 22:43:26 +08:00
Soulter
d273019830 chore: 删除一些没必要的文件 2023-05-11 22:39:55 +08:00
Soulter
fd59ec4b6c fix: 修复插件指令clone插件异常的问题 2023-05-11 22:12:39 +08:00
Soulter
bf33ccafca fix: 修复插件指令创建文件夹出错的问题 2023-05-11 22:06:38 +08:00
Soulter
425936872d fix: 修复插件指令结果显示异常的问题 2023-05-11 22:01:57 +08:00
Soulter
6627b2e1e5 fix: 修复插件指令报错的问题 2023-05-11 22:00:18 +08:00
Soulter
323c2cecf8 feat: 新增插件指令 2023-05-11 21:52:44 +08:00
Soulter
5b1dd3dce9 feat: 插件支持 2023-05-11 21:35:25 +08:00
Soulter
54af770dfb fix: 修复keyword指令的一些问题 2023-05-08 20:30:36 +08:00
Soulter
30a48fea6e feat: QQ群的免@唤醒支持多个前缀(nick指令) #92 2023-05-08 20:17:51 +08:00
Soulter
cfd5fb1452 perf: keyword指令支持删除关键词 2023-05-08 19:43:26 +08:00
Soulter
a78984376f perf: 优化与go-cqhttp的通信 2023-04-26 14:52:09 +08:00
Soulter
9887cae43c fix: replit web fix 2023-04-25 20:57:22 +08:00
Soulter
e63fe60f8d Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-04-25 20:48:08 +08:00
Soulter
b0ac2d676c feat: Replit平台支持 2023-04-25 20:48:04 +08:00
Soulter
5ef515165c Merge pull request #94 from RockChinQ/patch-1
chore: 更换使用nakuru-project-idk
2023-04-25 19:43:02 +08:00
Rock Chin
e21d43f920 chore: 更换使用nakuru-project-idk 2023-04-25 12:46:41 +08:00
Soulter
3a80ffad88 perf: 优化控制台信息显示 2023-04-25 10:42:03 +08:00
Soulter
47506d60cd perf: 优化pip检测 2023-04-25 10:29:16 +08:00
Soulter
b999b712b7 perf: 优化逆向库的错误管理 2023-04-25 10:21:12 +08:00
Soulter
6860ba3f05 Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-04-25 09:38:21 +08:00
Soulter
02594867c0 fix: 修复QQ平台昵称后的消息前导空格问题 2023-04-25 09:38:17 +08:00
Soulter
250435f3e7 Update requirements.txt 2023-04-25 09:29:35 +08:00
Soulter
3c593fb6f7 Update README.md 2023-04-24 19:34:11 +08:00
Soulter
807cad5c48 fix: 删除启动时对qq频道appid不应该的检查 2023-04-24 08:00:45 +00:00
Soulter
e92ecdd3f8 Update README.md 2023-04-23 17:16:31 +08:00
Soulter
1c91079d8f Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-04-23 09:05:32 +00:00
Soulter
376b2fef40 fix: 修复启动前检查依赖的问题 2023-04-23 09:05:30 +00:00
Soulter
300f3b6df8 Update README.md 2023-04-23 16:52:07 +08:00
Soulter
6e6f6d5cd4 Update README.md 2023-04-23 16:51:46 +08:00
Soulter
077e54d0f1 Merge branch 'master' of https://github.com/Soulter/QQChannelChatGPT 2023-04-23 08:35:02 +00:00
Soulter
18ffaa2b91 perf: 优化各种报错管理;
feat: 启动时检查依赖库
2023-04-23 08:31:22 +00:00
Soulter
a6555681a0 Update requirements.txt 2023-04-23 15:31:33 +08:00
Soulter
43ac0ef87c fix: remove judge_res 2023-04-22 08:09:22 +00:00
Soulter
754842be7c Update README.md 2023-04-22 14:35:22 +08:00
Soulter
5b3ee2dbe8 fix: 修复回复内容屏蔽词无效的问题 2023-04-22 06:07:33 +00:00
Soulter
ca5a1ddc0b perf: 优化bing模型达到单次会话上限后自动重置 2023-04-22 11:27:43 +08:00
Soulter
c9821132ad fix: QQ频道停止信息来源显示 2023-04-21 11:11:16 +08:00
Soulter
0641dca2a6 fix: 修复qqAt的一些问题 2023-04-21 01:04:57 +08:00
Soulter
fd983b9f5d fix: 修复了一些问题 2023-04-21 01:01:54 +08:00
Soulter
7e1e51c450 feat: QQ支持at发送方和画画指令支持 2023-04-21 01:00:31 +08:00
Soulter
d912b990e4 fix: 修复画画指令失效的问题 2023-04-21 00:45:59 +08:00
Soulter
8224aa87a5 fix: 修复bing模型不想继续会话自动重置的一些问题 2023-04-20 09:07:55 +08:00
Soulter
4cb5abc7b6 fix: 修复bing会话超时过期的问题 2023-04-20 08:59:58 +08:00
23 changed files with 1758 additions and 432 deletions

View File

@@ -1,34 +1,38 @@
<div align="center">
# QQChannelChatGPT
在QQ和QQ频道上使用ChatGPT、NewBing等语言模型稳定一次部署同时使用。
<img src="https://socialify.git.ci/Soulter/QQChannelChatGPT/image?description=1&forks=1&issues=1&language=1&name=1&owner=1&pattern=Circuit%20Board&stargazers=1&theme=Light" alt="QQChannelChatGPT" width="600" height="300" />
教程https://soulter.top/posts/qpdg.html
<!-- [![Language](https://img.shields.io/badge/language-python-green.svg?style=plastic)](https://www.python.org/)
[![License](https://img.shields.io/badge/license-AGPL3-orange.svg?style=plastic)](https://github.com/Soulter/QQChannelChatGPT/blob/master/LICENSE)
![Python](https://img.shields.io/badge/python-3.9+-blue) -->
欢迎体验😊(频道名: GPT机器人 | 频道号: x42d56aki2) | QQ群号322154837
基于go-cq和官方QQ频道SDK的机器人项目。支持ChatGPT、NewBing等大模型。一次部署同时使用
<img src="https://user-images.githubusercontent.com/37870767/230417115-9dd3c9d5-6b6b-4928-8fe3-82f559208aab.JPG" width="300"></img>
部署文档https://github.com/Soulter/QQChannelChatGPT/wiki
插件文档https://github.com/Soulter/QQChannelChatGPT/wiki/%E5%9B%9B%E3%80%81%E5%BC%80%E5%8F%91%E6%8F%92%E4%BB%B6
欢迎体验 | **QQ群号322154837**
<!-- <img src="https://user-images.githubusercontent.com/37870767/230417115-9dd3c9d5-6b6b-4928-8fe3-82f559208aab.JPG" width="300"></img> -->
</div>
## 功能:
通知部署好后如果使用的是bing或者逆向ChatGPT库需要使用切换模型指令`/bing`或者'/revgpt'
## 🧩功能:
近期新功能:
- 支持一键切换语言模型(使用/bing /revgpt /gpt分别可以切换newbing、逆向ChatGPT、官方ChatGPT模型
- Markdown渲染支持回复消息支持图片。
- 支持插件。https://github.com/Soulter/QQChannelChatGPT/wiki/%E5%9B%9B%E3%80%81%E5%BC%80%E5%8F%91%E6%8F%92%E4%BB%B6
- 热更新
- 接入QQ支持在QQ上和QQ频道上同时聊天https://github.com/Soulter/QQChannelChatGPT/issues/82
- Windows启动器。链接https://github.com/Soulter/QQChatGPTLauncher/releases/latest
支持的AI语言模型请在`configs/config.yaml`下配置):
- 逆向ChatGPT
- 官方ChatGPT AI
- 文心一言即将支持链接https://github.com/Soulter/ERNIEBot 欢迎Star
- NewBing
- Bard (即将支持)
部署QQ频道机器人教程链接https://soulter.top/posts/qpdg.html
- 逆向ChatGPT
- 官方ChatGPT API
- Bing
- ...
<!--
### 基本功能
<details>
<summary>✅ 回复符合上下文</summary>
@@ -83,10 +87,30 @@
- QQ频道机器人框架为QQ官方开源的框架稳定。
</details>
</details> -->
> 关于tokentoken就相当于是AI中的单词数但是不等于单词数`text-davinci-003`模型中最大可以支持`4097`个token。在发送信息时这个机器人会将用户的历史聊天记录打包发送给ChatGPT因此`token`也会相应的累加为了保证聊天的上下文的逻辑性就有了缓存token。
### 指令功能
<!-- > 关于tokentoken就相当于是AI中的单词数但是不等于单词数`text-davinci-003`模型中最大可以支持`4097`个token。在发送信息时这个机器人会将用户的历史聊天记录打包发送给ChatGPT因此`token`也会相应的累加为了保证聊天的上下文的逻辑性就有了缓存token。 -->
### 🛠️ 插件支持
本项目支持接入插件。
> 使用`plugin i 插件GitHub链接`即可安装。
插件开发教程https://github.com/Soulter/QQChannelChatGPT/wiki/%E5%9B%9B%E3%80%81%E5%BC%80%E5%8F%91%E6%8F%92%E4%BB%B6
部分好用的插件:
- `HuggingChat`: https://github.com/Soulter/HuggingChatForQQBot | HuggingChat模型接入
- `GoodPlugins`: https://github.com/Soulter/goodplugins | 随机动漫图片、搜番、喜报生成器等等
- `sysstat`: https://github.com/Soulter/sysstatqcbot | 查看系统状态
- `BiliMonitor`: https://github.com/Soulter/BiliMonitor | 订阅B站动态
<!--
### 指令
#### OpenAI官方API
在频道内需要先`@`机器人之后再输入指令在QQ中暂时需要在消息前加上`ai `,不需要@
@@ -113,15 +137,14 @@
- `/gpt` 切换为OpenAI官方API
- `/bing` 切换为bing
* 切换模型指令支持临时回复。如`/bing 你好`将会临时使用一次bing模型
* 切换模型指令支持临时回复。如`/bing 你好`将会临时使用一次bing模型 -->
## 📰使用方法:
**详细部署教程链接**https://soulter.top/posts/qpdg.html
**Windows用户推荐Windows一键安装请前往Release下载最新版本Beta**
有报错请先看issue解决不了再在频道内反馈。
使用文档https://github.com/Soulter/QQChannelChatGPT/wiki
**Windows用户可以使用启动器一键安装请前往Release下载最新版本Beta**
<!--
### 安装第三方库
```shell
@@ -131,15 +154,31 @@ pip install -r requirements.txt
### 配置
**详细部署教程链接**https://soulter.top/posts/qpdg.html
**详细部署教程链接**https://github.com/Soulter/QQChannelChatGPT/wiki
### 启动
- 启动main.py
- 启动main.py -->
## 🙇‍感谢
本项目使用了一下项目:
[ChatGPT by acheong08](https://github.com/acheong08/ChatGPT)
[EdgeGPT by acheong08](https://github.com/acheong08/EdgeGPT)
[go-cqhttp by Mrs4s](https://github.com/Mrs4s/go-cqhttp)
[nakuru-project by Lxns-Network](https://github.com/Lxns-Network/nakuru-project)
<!-- ## 👀部分演示截图
帮助中心(`help`指令)
![)F%2VQA`O)`4BHTXZ653(~9](https://github.com/Soulter/QQChannelChatGPT/assets/37870767/57eaa8c6-6962-4940-823c-2e26b5206cf5)
-->
## ⚙配置文件说明:
```yaml
# 如果你不知道怎么部署请查看https://soulter.top/posts/qpdg.html
# 如果你不知道怎么部署请查看https://github.com/Soulter/QQChannelChatGPT/wiki
# 不一定需要key了如果你没有key但有openAI账号或者必应账号可以考虑使用下面的逆向库

View File

@@ -0,0 +1,5 @@
# helloworld
QQChannelChatGPT项目的测试插件
A test plugin for QQChannelChatGPT plugin feature

View File

@@ -0,0 +1,79 @@
from nakuru.entities.components import *
from nakuru import (
GroupMessage,
FriendMessage
)
from botpy.message import Message, DirectMessage
from model.platform.qq import QQ
import time
import threading
class HelloWorldPlugin:
"""
初始化函数, 可以选择直接pass
"""
def __init__(self) -> None:
self.myThread = None # 线程对象如果要使用线程需要在此处定义。在run处定义会被释放掉
print("这是HelloWorld测试插件, 发送 helloworld 即可触发此插件。")
"""
入口函数,机器人会调用此函数。
参数规范: message: 消息文本; role: 身份; platform: 消息平台; message_obj: 消息对象; qq_platform: QQ平台对象可以通过调用qq_platform.send()直接发送消息。详见Helloworld插件示例
参数详情: role为admin或者member; platform为qqchan或者gocq; message_obj为nakuru的GroupMessage对象或者FriendMessage对象或者频道的Message, DirectMessage对象。
返回规范: bool: 是否hit到此插件(所有的消息均会调用每一个载入的插件, 如果没有hit到, 则应返回False)
Tuple: None或者长度为3的元组。当没有hit到时, 返回None. hit到时, 第1个参数为指令是否调用成功, 第2个参数为返回的消息文本或者gocq的消息链列表, 第3个参数为指令名称
例子:做一个名为"yuanshen"的插件;当接收到消息为“原神 可莉”, 如果不想要处理此消息则返回False, None如果想要处理但是执行失败了返回True, tuple([False, "请求失败啦~", "yuanshen"])
执行成功了返回True, tuple([True, "结果文本", "yuanshen"])
"""
def run(self, message: str, role: str, platform: str, message_obj, qq_platform: QQ):
if platform == "gocq":
"""
QQ平台指令处理逻辑
"""
img_url = "https://gchat.qpic.cn/gchatpic_new/905617992/720871955-2246763964-C6EE1A52CC668EC982453065C4FA8747/0?term=2&amp;is_origin=0"
if message == "helloworld":
return True, tuple([True, [Plain("Hello World!!"), Image.fromURL(url=img_url)], "helloworld"])
elif message == "hiloop":
if self.myThread is None:
self.myThread = threading.Thread(target=self.helloworldThread, args=(message_obj, qq_platform))
self.myThread.start()
return True, tuple([True, [Plain("A lot of Helloworlds!!"), Image.fromURL(url=img_url)], "helloworld"])
else:
return False, None
elif platform == "qqchan":
"""
频道处理逻辑(频道暂时只支持回复字符串类型的信息返回的信息都会被转成字符串如果不想处理某一个平台的信息直接返回False, None就行)
"""
if message == "helloworld":
return True, tuple([True, "Hello World!!", "helloworld"])
else:
return False, None
"""
帮助函数,当用户输入 plugin v 插件名称 时,会调用此函数,返回帮助信息
返回参数要求(必填)dict{
"name": str, # 插件名称
"desc": str, # 插件简短描述
"help": str, # 插件帮助信息
"version": str, # 插件版本
"author": str, # 插件作者
}
"""
def info(self):
return {
"name": "helloworld",
"desc": "测试插件",
"help": "测试插件, 回复helloworld即可触发",
"version": "v1.0.1 beta",
"author": "Soulter"
}
def helloworldThread(self, meseage_obj, qq_platform: QQ):
while True:
qq_platform.send(meseage_obj, [Plain("Hello World!!")]) # 第一个参数可以是message_obj, 也可以是qq群号
time.sleep(3) # 睡眠3秒。 用while True一定要记得sleep不然会卡死
# 热知识:检测消息开头指令,使用以下方法
# if message.startswith("原神"):
# pass

View File

@@ -1,8 +1,7 @@
import botpy
from botpy.message import Message
from botpy.message import Message, DirectMessage
from botpy.types.message import Reference
import re
from botpy.message import DirectMessage
import json
import threading
import asyncio
@@ -19,14 +18,21 @@ from nakuru import (
CQHTTP,
GroupMessage,
GroupMemberIncrease,
FriendMessage
FriendMessage,
GuildMessage
)
from nakuru.entities.components import Plain,At
from model.command.command import Command
from model.command.command_rev_chatgpt import CommandRevChatGPT
from model.command.command_rev_edgegpt import CommandRevEdgeGPT
from model.command.command_openai_official import CommandOpenAIOfficial
from util import general_utils as gu
from util.cmd_config import CmdConfig as cc
# QQBotClient实例
client = ''
# ChatGPT实例
global chatgpt
# 缓存的会话
session_dict = {}
# 最大缓存token在配置里改 configs/config.yaml
@@ -41,7 +47,7 @@ stat_file = ''
uniqueSession = False
# 日志记录
logf = open('log.log', 'a+', encoding='utf-8')
# logf = open('log.log', 'a+', encoding='utf-8')
# 是否上传日志,仅上传频道数量等数量的统计信息
is_upload_log = True
@@ -62,7 +68,7 @@ direct_message_mode = True
abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/'
# 版本
version = '3.0'
version = '3.0.2'
# 语言模型
REV_CHATGPT = 'rev_chatgpt'
@@ -72,8 +78,10 @@ REV_EDGEGPT = 'rev_edgegpt'
provider = None
chosen_provider = None
# 逆向库对象
# 语言模型对象
rev_chatgpt = None
rev_edgegpt = None
chatgpt = None
# gpt配置信息
gpt_config = {}
# 百度内容审核实例
@@ -96,16 +104,32 @@ gocq_app = CQHTTP(
port=6700,
http_port=5700,
)
admin_qq = "123456"
gocq_loop = None
nick_qq = "ai "
nick_qq = None
bing_cache_loop = None
# 插件
cached_plugins = {}
# 统计
cnt_total = 0
cnt_valid = 0
# 新版配置文件
cc.init_attributes(["qq_forward_threshold"], 200)
cc.init_attributes(["qq_welcome"], "欢迎加入本群!\n欢迎给https://github.com/Soulter/QQChannelChatGPT项目一个Star😊~\n输入help查看帮助~\n")
cc.init_attributes(["bing_proxy"], "")
cc.init_attributes(["qq_pic_mode"], False)
# cc.init_attributes(["qq_forward_mode"], False)
def new_sub_thread(func, args=()):
thread = threading.Thread(target=func, args=args, daemon=True)
thread.start()
# 写入统计信息
def toggle_count(at: bool, message):
global stat_file
@@ -129,38 +153,31 @@ def toggle_count(at: bool, message):
# 上传统计信息并检查更新
def upload():
global object_id
global version
global version, cnt_valid, cnt_total
while True:
addr = ''
addr_ip = ''
try:
# 用户唯一性标识
addr = requests.get('http://myip.ipip.net', timeout=5).text
except BaseException:
addr_ip = re.findall(r'\d+.\d+.\d+.\d+', addr)[0]
except BaseException as e:
print(e)
pass
try:
ts = str(time.time())
guild_count, guild_msg_count, guild_direct_msg_count, session_count = get_stat()
headers = {
'X-LC-Id': 'UqfXTWW15nB7iMT0OHvYrDFb-gzGzoHsz',
'X-LC-Key': 'QAZ1rQLY1ZufHrZlpuUiNff7',
'Content-Type': 'application/json'
}
key_stat = chatgpt.get_key_stat()
d = {"data": {'version': version, "guild_count": guild_count, "guild_msg_count": guild_msg_count, "guild_direct_msg_count": guild_direct_msg_count, "session_count": session_count, 'addr': addr, 'key_stat':key_stat}}
d = json.dumps(d).encode("utf-8")
res = requests.put(f'https://uqfxtww1.lc-cn-n1-shared.com/1.1/classes/bot_record/{object_id}', headers = headers, data = d)
if json.loads(res.text)['code'] == 1:
print("[System] New User.")
res = requests.post(f'https://uqfxtww1.lc-cn-n1-shared.com/1.1/classes/bot_record', headers = headers, data = d)
object_id = json.loads(res.text)['objectId']
object_id_file = open(abs_path+"configs/object_id", 'w+', encoding='utf-8')
object_id_file.write(str(object_id))
object_id_file.flush()
object_id_file.close()
o = {"cnt_total": cnt_total,"admin": admin_qq,"addr": addr,}
o_j = json.dumps(o)
res = {"version": version, "count": cnt_valid, "ip": addr_ip, "others": o_j}
resp = requests.post('https://api.soulter.top/upload', data=json.dumps(res), timeout=5)
# print(resp.text)
if resp.status_code == 200:
ok = resp.json()
if ok['status'] == 'ok':
cnt_valid = 0
cnt_total = 0
except BaseException as e:
print(e)
pass
# 每隔2小时上传一次
time.sleep(60*60*2)
time.sleep(60*10)
'''
初始化机器人
@@ -168,39 +185,50 @@ def upload():
def initBot(cfg, prov):
global chatgpt, provider, rev_chatgpt, baidu_judge, rev_edgegpt, chosen_provider
global reply_prefix, gpt_config, config, uniqueSession, frequency_count, frequency_time,announcement, direct_message_mode, version
global command_openai_official, command_rev_chatgpt, command_rev_edgegpt,reply_prefix, keywords
global command_openai_official, command_rev_chatgpt, command_rev_edgegpt,reply_prefix, keywords, cached_plugins
provider = prov
config = cfg
if 'reply_prefix' in cfg:
reply_prefix = cfg['reply_prefix']
# 语言模型提供商
gu.log("--------加载语言模型--------", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
if REV_CHATGPT in prov:
gu.log("- 逆向ChatGPT库 -", gu.LEVEL_INFO)
if cfg['rev_ChatGPT']['enable']:
if 'account' in cfg['rev_ChatGPT']:
from model.provider.provider_rev_chatgpt import ProviderRevChatGPT
from model.command.command_rev_chatgpt import CommandRevChatGPT
rev_chatgpt = ProviderRevChatGPT(cfg['rev_ChatGPT'])
command_rev_chatgpt = CommandRevChatGPT(cfg['rev_ChatGPT'])
chosen_provider = REV_CHATGPT
else:
input("[System-err] 请退出本程序, 然后在配置文件中填写rev_ChatGPT相关配置")
if REV_EDGEGPT in prov:
if cfg['rev_edgegpt']['enable']:
from model.provider.provider_rev_edgegpt import ProviderRevEdgeGPT
from model.command.command_rev_edgegpt import CommandRevEdgeGPT
rev_edgegpt = ProviderRevEdgeGPT()
command_rev_edgegpt = CommandRevEdgeGPT(rev_edgegpt)
chosen_provider = REV_EDGEGPT
gu.log("- New Bing -", gu.LEVEL_INFO)
if not os.path.exists('./cookies.json'):
input("[System-err] 导入Bing模型时发生错误, 没有找到cookies文件或者cookies文件放置位置错误。windows启动器启动的用户请把cookies.json文件放到和启动器相同的目录下。\n如何获取请看https://github.com/Soulter/QQChannelChatGPT仓库介绍。")
else:
if cfg['rev_edgegpt']['enable']:
try:
from model.provider.provider_rev_edgegpt import ProviderRevEdgeGPT
rev_edgegpt = ProviderRevEdgeGPT()
chosen_provider = REV_EDGEGPT
except BaseException as e:
gu.log("加载Bing模型时发生错误, 请检查1. cookies文件是否正确放置 2. 是否设置了代理(梯子)。", gu.LEVEL_ERROR, max_len=60)
if OPENAI_OFFICIAL in prov:
gu.log("- OpenAI官方 -", gu.LEVEL_INFO)
if cfg['openai']['key'] is not None:
from model.provider.provider_openai_official import ProviderOpenAIOfficial
from model.command.command_openai_official import CommandOpenAIOfficial
chatgpt = ProviderOpenAIOfficial(cfg['openai'])
command_openai_official = CommandOpenAIOfficial(chatgpt)
chosen_provider = OPENAI_OFFICIAL
command_rev_edgegpt = CommandRevEdgeGPT(rev_edgegpt)
command_rev_chatgpt = CommandRevChatGPT(rev_chatgpt)
command_openai_official = CommandOpenAIOfficial(chatgpt)
gu.log("--------加载个性化配置--------", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
# 得到关键词
if os.path.exists("keyword.json"):
with open("keyword.json", 'r', encoding='utf-8') as f:
@@ -217,32 +245,20 @@ def initBot(cfg, prov):
if 'baidu_aip' in cfg and 'enable' in cfg['baidu_aip'] and cfg['baidu_aip']['enable']:
try:
baidu_judge = BaiduJudge(cfg['baidu_aip'])
print("[System] 百度内容审核初始化成功")
gu.log("百度内容审核初始化成功", gu.LEVEL_INFO)
except BaseException as e:
input("[System] 百度内容审核初始化失败: " + str(e))
exit()
gu.log("百度内容审核初始化失败", gu.LEVEL_ERROR)
# 统计上传
if is_upload_log:
# 读取object_id
global object_id
if not os.path.exists(abs_path+"configs/object_id"):
with open(abs_path+"configs/object_id", 'w', encoding='utf-8') as f:
f.write("")
object_id_file = open(abs_path+"configs/object_id", 'r', encoding='utf-8')
object_id = object_id_file.read()
object_id_file.close()
# 创建上传定时器线程
threading.Thread(target=upload, daemon=True).start()
threading.Thread(target=upload, daemon=True).start()
# 得到私聊模式配置
if 'direct_message_mode' in cfg:
direct_message_mode = cfg['direct_message_mode']
print("[System] 私聊功能: "+str(direct_message_mode))
gu.log("私聊功能: "+str(direct_message_mode), gu.LEVEL_INFO)
# 得到发言频率配置
if 'limit' in cfg:
print('[System] 发言频率配置: '+str(cfg['limit']))
gu.log("发言频率配置: "+str(cfg['limit']), gu.LEVEL_INFO)
if 'count' in cfg['limit']:
frequency_count = cfg['limit']['count']
if 'time' in cfg['limit']:
@@ -250,42 +266,81 @@ def initBot(cfg, prov):
# 得到公告配置
if 'notice' in cfg:
print('[System] 公告配置: '+cfg['notice'])
gu.log("公告配置: "+cfg['notice'], gu.LEVEL_INFO)
announcement += cfg['notice']
try:
if 'uniqueSessionMode' in cfg and cfg['uniqueSessionMode']:
uniqueSession = True
else:
uniqueSession = False
print("[System] 独立会话: " + str(uniqueSession))
gu.log("独立会话: "+str(uniqueSession), gu.LEVEL_INFO)
if 'dump_history_interval' in cfg:
print("[System] 历史记录转储时间周期: " + cfg['dump_history_interval'] + "分钟")
gu.log("历史记录保存间隔: "+str(cfg['dump_history_interval']), gu.LEVEL_INFO)
except BaseException:
print("[System-Error] 读取uniqueSessionMode/version/dump_history_interval配置文件失败, 使用默认值。")
pass
print(f"[System] QQ开放平台AppID: {cfg['qqbot']['appid']} 令牌: {cfg['qqbot']['token']}")
print("\n[System] 如果有任何问题, 请在 https://github.com/Soulter/QQChannelChatGPT 上提交issue说明问题或者添加QQ905617992")
print("[System] 请给 https://github.com/Soulter/QQChannelChatGPT 点个star!")
gu.log(f"QQ开放平台AppID: {cfg['qqbot']['appid']} 令牌: {cfg['qqbot']['token']}")
if chosen_provider is None:
print("[System-Warning] 检测到没有启动任何一个语言模型。请至少在配置文件中启用一个语言模型。")
# 得到指令设置(cmd_config.json)
if os.path.exists("cmd_config.json"):
with open("cmd_config.json", 'r', encoding='utf-8') as f:
cmd_config = json.load(f)
# QQ机器人昵称
if 'nick_qq' in cmd_config:
global nick_qq
nick_qq = cmd_config['nick_qq']
gu.log("检测到没有启动任何一个语言模型。请至少在配置文件中启用一个语言模型。", gu.LEVEL_CRITICAL)
global nick_qq
nick_qq = cc.get('nick_qq', nick_qq)
thread_inst = None
gu.log("--------加载插件--------", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
# 加载插件
_command = Command(None)
ok, err = _command.plugin_reload(cached_plugins)
if ok:
gu.log("加载插件完成", gu.LEVEL_INFO)
else:
gu.log(err, gu.LEVEL_ERROR)
gu.log("--------加载平台--------", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
# GOCQ
global gocq_bot
if 'gocqbot' in cfg and cfg['gocqbot']['enable']:
gu.log("- 启用QQ机器人 -", gu.LEVEL_INFO)
global admin_qq, admin_qqchan
admin_qq = cc.get('admin_qq', None)
admin_qqchan = cc.get('admin_qqchan', None)
if admin_qq == None:
gu.log("未设置管理者QQ号(管理者才能使用update/plugin等指令)", gu.LEVEL_WARNING)
admin_qq = input("请输入管理者QQ号(必须设置): ")
gu.log("管理者QQ号设置为: " + admin_qq, gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
cc.put('admin_qq', admin_qq)
if admin_qqchan == None:
gu.log("未设置管理者QQ频道用户号(管理者才能使用update/plugin等指令)", gu.LEVEL_WARNING)
admin_qqchan = input("请输入管理者频道用户号(不是QQ号, 可以先回车跳过然后在频道发送指令!myid获取): ")
if admin_qqchan == "":
gu.log("跳过设置管理者频道用户号", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
else:
gu.log("管理者频道用户号设置为: " + admin_qqchan, gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
cc.put('admin_qqchan', admin_qqchan)
gu.log("管理者QQ: " + admin_qq, gu.LEVEL_INFO)
gu.log("管理者频道用户号: " + admin_qqchan, gu.LEVEL_INFO)
global gocq_app, gocq_loop
gocq_loop = asyncio.new_event_loop()
gocq_bot = QQ(True, cc, gocq_loop)
thread_inst = threading.Thread(target=run_gocq_bot, args=(gocq_loop, gocq_bot, gocq_app), daemon=False)
thread_inst.start()
else:
gocq_bot = QQ(False)
gu.log("机器人部署教程: https://github.com/Soulter/QQChannelChatGPT/wiki/", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
gu.log("如果有任何问题, 请在 https://github.com/Soulter/QQChannelChatGPT 上提交issue说明问题", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
gu.log("请给 https://github.com/Soulter/QQChannelChatGPT 点个star!", gu.LEVEL_INFO, fg=gu.FG_COLORS['yellow'])
# QQ频道
if 'qqbot' in cfg and cfg['qqbot']['enable']:
print("[System] 启用QQ频道机器人")
gu.log("- 启用QQ频道机器人(旧版) -", gu.LEVEL_INFO)
global qqchannel_bot, qqchan_loop
qqchannel_bot = QQChan()
qqchan_loop = asyncio.new_event_loop()
@@ -293,17 +348,8 @@ def initBot(cfg, prov):
thread_inst.start()
# thread.join()
# GOCQ
if 'gocqbot' in cfg and cfg['gocqbot']['enable']:
print("[System] 启用QQ机器人")
global gocq_app, gocq_bot, gocq_loop
gocq_bot = QQ()
gocq_loop = asyncio.new_event_loop()
thread_inst = threading.Thread(target=run_gocq_bot, args=(gocq_loop, gocq_bot, gocq_app), daemon=False)
thread_inst.start()
if thread_inst == None:
input("[System-Error] 没有启用任何机器人,程序退出")
input("[System-Error] 没有启用/成功启用任何机器人,程序退出")
exit()
thread_inst.join()
@@ -316,10 +362,23 @@ def run_qqchan_bot(cfg, loop, qqchannel_bot):
try:
qqchannel_bot.run_bot(client, cfg['qqbot']['appid'], cfg['qqbot']['token'])
except BaseException as e:
input(f"\n[System-Error] 启动QQ频道机器人时出现错误原因如下{e}\n可能是没有填写QQBOT appid和token请在config中完善你的appid和token\n配置教程https://soulter.top/posts/qpdg.html\n")
gu.log("启动QQ频道机器人时出现错误, 原因如下: " + str(e), gu.LEVEL_CRITICAL, tag="QQ频道")
gu.log(r"【提醒】有可能你想启动的是gocq, 并不是这个旧版的QQ频道SDK, 如果是这样, 请修改配置文件QQChannelChatGPT/config.yaml详情请看https://github.com/Soulter/QQChannelChatGPT/wiki/%E4%BA%8C%E3%80%81%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE。" + str(e), gu.LEVEL_CRITICAL, tag="QQ频道")
# gu.log("如果你使用了go-cqhttp, 则可以忽略上面的报错。" + str(e), gu.LEVEL_CRITICAL, tag="QQ频道")
# input(f"\n[System-Error] 启动QQ频道机器人时出现错误原因如下{e}\n可能是没有填写QQBOT appid和token请在config中完善你的appid和token\n配置教程https://soulter.top/posts/qpdg.html\n")
def run_gocq_bot(loop, gocq_bot, gocq_app):
asyncio.set_event_loop(loop)
gu.log("正在检查本地GO-CQHTTP连接...端口5700, 6700", tag="QQ")
while True:
if not gu.port_checker(5700) or not gu.port_checker(6700):
gu.log("与GO-CQHTTP通信失败, 请检查GO-CQHTTP是否启动并正确配置。5秒后自动重试。", gu.LEVEL_CRITICAL, tag="QQ")
time.sleep(5)
else:
gu.log("检查完毕,未发现问题。", tag="QQ")
break
global gocq_client
gocq_client = gocqClient()
try:
@@ -356,53 +415,62 @@ def save_provider_preference(chosen_provider):
'''
通用回复方法
'''
def send_message(platform, message, res, msg_ref = None, image = None, gocq_loop = None, qqchannel_bot = None, gocq_bot = None):
def send_message(platform, message, res, msg_ref = None, image = None, gocq_loop = None, qqchannel_bot = None, gocq_bot = None, image_mode=False):
# imagemode:
# For GOCQ: when image_mode is true, ALL plain texts in res will change into a new pic
global cnt_valid
cnt_valid += 1
if platform == PLATFORM_QQCHAN:
if image != None:
qqchannel_bot.send_qq_msg(message, res, image_mode=True, msg_ref=msg_ref)
qqchannel_bot.send_qq_msg(message, str(res), image_mode=True, msg_ref=msg_ref)
else:
qqchannel_bot.send_qq_msg(message, res, msg_ref=msg_ref)
if platform == PLATFORM_GOCQ: asyncio.run_coroutine_threadsafe(gocq_bot.send_qq_msg(message, res), gocq_loop).result()
qqchannel_bot.send_qq_msg(message, str(res), msg_ref=msg_ref)
if platform == PLATFORM_GOCQ:
if image != None:
# image is a url string
asyncio.run_coroutine_threadsafe(gocq_bot.send_qq_msg(message, [Plain(text="好的,我根据你的需要为你生成了一张图片😊"),Image.fromURL(image)], False), gocq_loop).result()
else:
asyncio.run_coroutine_threadsafe(gocq_bot.send_qq_msg(message, res, image_mode), gocq_loop).result()
'''
处理消息
group: 群聊模式
'''
def oper_msg(message, group=False, msg_ref = None, platform = None):
def oper_msg(message,
group: bool=False,
msg_ref: Reference = None,
platform: str = None):
"""
处理消息。
group: 群聊模式,
message: 频道是频道的消息对象, QQ是nakuru-gocq的消息对象
"""
global session_dict, provider
qq_msg = ''
session_id = ''
user_id = ''
user_name = ''
global chosen_provider, reply_prefix, keywords, qqchannel_bot, gocq_bot, gocq_loop, bing_cache_loop
global chosen_provider, reply_prefix, keywords, qqchannel_bot, gocq_bot, gocq_loop, bing_cache_loop, qqchan_loop
role = "member" # 角色
hit = False # 是否命中指令
command_result = () # 调用指令返回的结果
global admin_qq, admin_qqchan, cached_plugins, gocq_bot, nick_qq
global cnt_total
cnt_total += 1
with_tag = False # 是否带有昵称
# 将nick_qq(昵称)统一转换为tuple
if nick_qq == None:
nick_qq = ("ai","!","")
if isinstance(nick_qq, str):
nick_qq = (nick_qq,)
if isinstance(nick_qq, list):
nick_qq = tuple(nick_qq)
if platform == PLATFORM_QQCHAN:
print("[QQCHAN-BOT] 接收到消息:"+ str(message.content))
with_tag = True
gu.log(f"收到消息:{message.content}", gu.LEVEL_INFO, tag="QQ频道")
user_id = message.author.id
user_name = message.author.username
global qqchan_loop
if platform == PLATFORM_GOCQ:
if isinstance(message.message[0], Plain):
print("[GOCQ-BOT] 接收到消息:"+ str(message.message[0].text))
elif isinstance(message.message[0], At):
print("[GOCQ-BOT] 接收到消息:"+ str(message.message[1].text))
user_id = message.user_id
user_name = message.user_id
global gocq_loop
if chosen_provider is None:
send_message(platform, message, f"没有启动任何一个语言模型。请至少在配置文件中启用一个语言模型。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
# 检查发言频率
if not check_frequency(user_id):
qqchannel_bot.send_qq_msg(message, f'{user_name}的发言超过频率限制(╯▔皿▔)╯。\n{frequency_time}秒内只能提问{frequency_count}次。')
return
if platform == PLATFORM_QQCHAN:
if group:
# 频道内
# 过滤@
@@ -417,7 +485,7 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
session_id = message.channel_id
# 得到身份
if "2" in message.member.roles or "4" in message.member.roles or "5" in message.member.roles:
print("[QQCHAN-BOT] 检测到管理员身份")
# gu.log(f"检测到管理员身份", gu.LEVEL_INFO, tag="QQ频道")
role = "admin"
else:
role = "member"
@@ -427,22 +495,64 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
session_id = user_id
if platform == PLATFORM_GOCQ:
if group:
if isinstance(message.message[0], Plain):
qq_msg = str(message.message[0].text)
elif isinstance(message.message[0], At):
qq_msg = str(message.message[1].text).strip()
else:
return
session_id = message.group_id
else:
qq_msg = message.message[0].text
session_id = message.user_id
# todo: 暂时将所有人设为管理员
role = "admin"
_len = 0
for i in message.message:
if isinstance(i, Plain):
qq_msg += str(i.text).strip()
if isinstance(i, At):
# @机器人
if message.type == "GuildMessage":
if i.qq == message.self_tiny_id:
with_tag = True
if message.type == "FriendMessage":
if i.qq == message.self_id:
with_tag = True
if message.type == "GroupMessage":
if i.qq == message.self_id:
with_tag = True
logf.write("[QQBOT] "+ qq_msg+'\n')
logf.flush()
for i in nick_qq:
if i != '' and qq_msg.startswith(i):
_len = len(i)
with_tag = True
break
qq_msg = qq_msg[_len:].strip()
gu.log(f"收到消息:{qq_msg}", gu.LEVEL_INFO, tag="QQ")
user_id = message.user_id
if group:
# 适配GO-CQHTTP的频道功能
if message.type == "GuildMessage":
session_id = message.channel_id
else:
session_id = message.group_id
else:
with_tag = True
# qq_msg = message.message[0].text
session_id = message.user_id
role = "member"
if message.type == "GuildMessage":
sender_id = str(message.sender.tiny_id)
else:
sender_id = str(message.sender.user_id)
if sender_id == admin_qq or sender_id == admin_qqchan:
# gu.log("检测到管理员身份", gu.LEVEL_INFO, tag="GOCQ")
role = "admin"
if qq_msg == "":
send_message(platform, message, f"Hi~", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
if with_tag:
# 检查发言频率
if not check_frequency(user_id):
send_message(platform, message, f'你的发言超过频率限制(╯▔皿▔)╯。\n管理员设置{frequency_time}秒内只能提问{frequency_count}次。', msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
# logf.write("[GOCQBOT] "+ qq_msg+'\n')
# logf.flush()
# 关键词回复
for k in keywords:
@@ -490,28 +600,40 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
chatgpt_res = ""
if chosen_provider == OPENAI_OFFICIAL:
hit, command_result = command_openai_official.check_command(qq_msg, session_id, user_name, role, platform=platform)
# hit: 是否触发了指令.
hit, command_result = command_openai_official.check_command(qq_msg, session_id, user_name, role, platform=platform, message_obj=message, cached_plugins=cached_plugins, qq_platform=gocq_bot)
# hit: 是否触发了指令
if not hit:
if not with_tag:
return
if chatgpt == None:
send_message(platform, message, f"管理员未启动OpenAI模型或初始化时失败。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
# 请求ChatGPT获得结果
try:
chatgpt_res = chatgpt.text_chat(qq_msg, session_id)
if OPENAI_OFFICIAL in reply_prefix:
chatgpt_res = reply_prefix[OPENAI_OFFICIAL] + chatgpt_res
except (BaseException) as e:
print("[System-Err] OpenAI API错误。原因如下:\n"+str(e))
send_message(platform, message, f"OpenAI API错误。原因如下:\n{str(e)} \n前往官方频道反馈~", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
gu.log("OpenAI API请求错误, 原因: "+str(e), gu.LEVEL_ERROR)
send_message(platform, message, f"OpenAI API错误, 原因: {str(e)}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
elif chosen_provider == REV_CHATGPT:
hit, command_result = command_rev_chatgpt.check_command(qq_msg, role, platform=platform)
hit, command_result = command_rev_chatgpt.check_command(qq_msg, role, platform=platform, message_obj=message, cached_plugins=cached_plugins, qq_platform=gocq_bot)
if not hit:
if not with_tag:
return
if rev_chatgpt == None:
send_message(platform, message, f"管理员未启动此模型或者此模型初始化时失败。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
try:
while rev_chatgpt.is_all_busy():
time.sleep(1)
chatgpt_res = str(rev_chatgpt.text_chat(qq_msg))
if REV_CHATGPT in reply_prefix:
chatgpt_res = reply_prefix[REV_CHATGPT] + chatgpt_res
except BaseException as e:
print("[System-Err] Rev ChatGPT API错误。原因如下:\n"+str(e))
send_message(platform, message, f"Rev ChatGPT API错误。原因如下\n{str(e)} \n前往官方频道反馈~", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
gu.log("逆向ChatGPT请求错误, 原因: "+str(e), gu.LEVEL_ERROR)
send_message(platform, message, f"RevChatGPT错误, 原因: \n{str(e)}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
elif chosen_provider == REV_EDGEGPT:
if bing_cache_loop == None:
@@ -519,17 +641,22 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
bing_cache_loop = gocq_loop
elif platform == PLATFORM_QQCHAN:
bing_cache_loop = qqchan_loop
hit, command_result = command_rev_edgegpt.check_command(qq_msg, bing_cache_loop, role, platform=platform)
hit, command_result = command_rev_edgegpt.check_command(qq_msg, bing_cache_loop, role, platform=platform, message_obj=message, cached_plugins=cached_plugins, qq_platform=gocq_bot)
if not hit:
try:
if not with_tag:
return
if rev_edgegpt == None:
send_message(platform, message, f"管理员未启动此模型或者此模型初始化时失败。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
while rev_edgegpt.is_busy():
time.sleep(1)
res, res_code = asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg), bing_cache_loop).result()
res, res_code = asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg, platform), bing_cache_loop).result()
if res_code == 0: # bing不想继续话题重置会话后重试。
send_message(platform, message, "Bing不想继续话题了, 正在自动重置会话并重试。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
asyncio.run_coroutine_threadsafe(rev_edgegpt.forget(), bing_cache_loop).result()
res, res_code = asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg), bing_cache_loop).result()
res, res_code = asyncio.run_coroutine_threadsafe(rev_edgegpt.text_chat(qq_msg, platform), bing_cache_loop).result()
if res_code == 0: # bing还是不想继续话题大概率说明提问有问题。
asyncio.run_coroutine_threadsafe(rev_edgegpt.forget(), bing_cache_loop).result()
send_message(platform, message, "Bing仍然不想继续话题, 会话已重置, 请检查您的提问后重试。", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
@@ -538,7 +665,7 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
if REV_EDGEGPT in reply_prefix:
chatgpt_res = reply_prefix[REV_EDGEGPT] + chatgpt_res
except BaseException as e:
print("[System-Err] Rev NewBing API错误。原因如下:\n"+str(e))
gu.log("NewBing请求错误, 原因: "+str(e), gu.LEVEL_ERROR)
send_message(platform, message, f"Rev NewBing API错误。原因如下\n{str(e)} \n前往官方频道反馈~", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
# 切换回原来的语言模型
@@ -551,29 +678,30 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
if command_result != None:
command = command_result[2]
if command == "keyword":
with open("keyword.json", "r", encoding="utf-8") as f:
keywords = json.load(f)
if os.path.exists("keyword.json"):
with open("keyword.json", "r", encoding="utf-8") as f:
keywords = json.load(f)
# QQ昵称
# 昵称
if command == "nick":
with open("cmd_config.json", "r", encoding="utf-8") as f:
global nick_qq
nick_qq = json.load(f)["nick_qq"]
nick_qq = cc.get("nick_qq", nick_qq)
if command_result[0]:
# 是否是画图指令
if len(command_result) == 3 and command_result[2] == 'draw':
for i in command_result[1]:
send_message(platform, message, i, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
if isinstance(command_result[1], list) and len(command_result) == 3 and command_result[2] == 'draw':
if chatgpt != None:
for i in command_result[1]:
send_message(platform, message, i, msg_ref=msg_ref, image=i, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
else:
send_message(platform, message, "画图指令需要启用OpenAI官方模型.", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
else:
try:
send_message(platform, message, command_result[1], msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
except BaseException as e:
t = command_result[1].replace(".", " . ")
send_message(platform, message, t, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
send_message(platform, message, f"回复消息出错: {str(e)}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
else:
send_message(platform, message, f"指令调用错误: \n{command_result[1]}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
send_message(platform, message, f"指令调用错误: \n{str(command_result[1])}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
@@ -581,26 +709,31 @@ def oper_msg(message, group=False, msg_ref = None, platform = None):
return
# 记录日志
logf.write(f"{reply_prefix} {str(chatgpt_res)}\n")
logf.flush()
# logf.write(f"{reply_prefix} {str(chatgpt_res)}\n")
# logf.flush()
# 敏感过滤
# 过滤不合适的词
judged_res = chatgpt_res
for i in uw.unfit_words:
judged_res = re.sub(i, "***", judged_res)
chatgpt_res = re.sub(i, "***", chatgpt_res)
# 百度内容审核服务二次审核
if baidu_judge != None:
check, msg = baidu_judge.judge(judged_res)
check, msg = baidu_judge.judge(chatgpt_res)
if not check:
send_message(platform, message, f"你的提问得到的回复【百度内容审核】未通过,不予回复。\n\n{msg}", msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
return
# 发送qq信息
try:
send_message(platform, message, chatgpt_res, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
if platform==PLATFORM_GOCQ:
if cc.get("qq_pic_mode", False):
send_message(platform, message, chatgpt_res, image_mode=True, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
else:
send_message(platform, message, chatgpt_res, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
else:
send_message(platform, message, chatgpt_res, msg_ref=msg_ref, gocq_loop=gocq_loop, qqchannel_bot=qqchannel_bot, gocq_bot=gocq_bot)
except BaseException as e:
print("回复消息错误: \n"+str(e))
gu.log("回复消息错误: \n"+str(e), gu.LEVEL_ERROR)
'''
获取统计信息
@@ -650,15 +783,12 @@ class gocqClient():
# 收到群聊消息
@gocq_app.receiver("GroupMessage")
async def _(app: CQHTTP, source: GroupMessage):
global nick_qq
# gu.log(str(source), gu.LEVEL_INFO, max_len=9999)
if isinstance(source.message[0], Plain):
if source.message[0].text.startswith(nick_qq):
source.message[0].text = source.message[0].text[len(nick_qq):]
new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ))
new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ))
if isinstance(source.message[0], At):
if source.message[0].qq == source.self_id:
if source.message[1].text.startswith(nick_qq):
source.message[1].text = source.message[0].text[len(nick_qq):]
new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ))
else:
return
@@ -672,7 +802,25 @@ class gocqClient():
@gocq_app.receiver("GroupMemberIncrease")
async def _(app: CQHTTP, source: GroupMemberIncrease):
global nick_qq
global nick_qq, cc
await app.sendGroupMessage(source.group_id, [
Plain(text=f"欢迎加入本群!\n欢迎给https://github.com/Soulter/QQChannelChatGPT项目一个Star😊~\n@我输入help查看帮助~\n我叫{nick_qq}, 你也可以以【{nick_qq}+问题】的格式来提醒我并问我问题哦~\n")
Plain(text=cc.get("qq_welcome", "欢迎新人~")),
])
@gocq_app.receiver("GuildMessage")
async def _(app: CQHTTP, source: GuildMessage):
# gu.log(str(source), gu.LEVEL_INFO, max_len=9999)
if isinstance(source.message[0], Plain):
# if source.message[0].text.startswith(nick_qq):
# _len = 0
# for i in nick_qq:
# if source.message[0].text.startswith(i):
# _len = len(i)
# source.message[0].text = source.message[0].text[_len:].strip()
new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ))
if isinstance(source.message[0], At):
if source.message[0].qq == source.self_tiny_id:
new_sub_thread(oper_msg, (source, True, None, PLATFORM_GOCQ))
else:
return

View File

@@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
from git.repo import Repo
import git
import os
# import zipfile
if __name__ == "__main__":
try:
# 检测文件夹
if not os.path.exists('QQChannelChatGPT'):
os.mkdir('QQChannelChatGPT')
project_path = os.path.join('QQChannelChatGPT')
try:
repo = Repo(project_path)
# 检查当前commit的hash值
commit_hash = repo.head.object.hexsha
print("当前commit的hash值为: " + commit_hash)
# 得到远程仓库的origin的commit的列表
origin = repo.remotes.origin
try:
origin.fetch()
except:
pass
# 得到远程仓库的commit的hash值
remote_commit_hash = origin.refs.master.commit.hexsha
print("https://github.com/Soulter/QQChannelChatGPT的commit的hash值为: " + remote_commit_hash)
# 比较两个commit的hash值
if commit_hash != remote_commit_hash:
res = input("检测到项目有更新, 是否更新? (y/n): ")
if res == 'y':
repo.remotes.origin.pull()
print("项目更新完毕")
if res == 'n':
print("已取消更新")
except:
print("正在从https://github.com/Soulter/QQChannelChatGPT.git拉取项目...")
Repo.clone_from('https://github.com/Soulter/QQChannelChatGPT.git',to_path=project_path,branch='master')
print("项目拉取完毕")
print("【重要提醒】如果你没有Python版本>=3.8或者Git环境, 请先安装, 否则接下来的操作会造成闪退。")
print("【重要提醒】Python下载地址: https://npm.taobao.org/mirrors/python/3.9.7/python-3.9.7-amd64.exe ")
print("【重要提醒】Git下载地址: https://registry.npmmirror.com/-/binary/git-for-windows/v2.39.2.windows.1/Git-2.39.2-64-bit.exe")
print("【重要提醒】安装时, 请务必勾选“Add Python to PATH”选项。")
input("已确保安装了Python3.9+的版本,按下回车继续...")
print("正在安装依赖库")
os.system('python -m pip install -r QQChannelChatGPT\\requirements.txt')
print("依赖库安装完毕")
input("初次启动, 请先在QQChannelChatGPT/configs/config.yaml填写相关配置! 按任意键继续...")
finally:
print("正在启动项目...")
os.system('python QQChannelChatGPT\main.py')
except BaseException as e:
print(e)
input("程序出错。可以截图发给QQ905617992.按下回车键退出...")

105
main.py
View File

@@ -1,14 +1,19 @@
import threading
import asyncio
import os, sys
from pip._internal import main as pipmain
import util.general_utils as gu
abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/'
def main(loop, event):
import cores.qqbot.core as qqBot
import yaml
ymlfile = open(abs_path+"configs/config.yaml", 'r', encoding='utf-8')
cfg = yaml.safe_load(ymlfile)
def main():
try:
import cores.qqbot.core as qqBot
import yaml
ymlfile = open(abs_path+"configs/config.yaml", 'r', encoding='utf-8')
cfg = yaml.safe_load(ymlfile)
except BaseException as e:
print(e)
input("第三方依赖库未完全安装完毕,请退出程序重试。")
exit()
if 'http_proxy' in cfg:
os.environ['HTTP_PROXY'] = cfg['http_proxy']
@@ -16,7 +21,11 @@ def main(loop, event):
os.environ['HTTPS_PROXY'] = cfg['https_proxy']
provider = privider_chooser(cfg)
print('[System] 当前语言模型提供商: ' + str(provider))
if len(provider) == 0:
gu.log("未开启任何语言模型, 请在configs/config.yaml下选择开启相应语言模型。", gu.LEVEL_CRITICAL)
input("按任意键退出...")
exit()
print('[System] 开启的语言模型: ' + str(provider))
# 执行Bot
qqBot.initBot(cfg, provider)
@@ -39,28 +48,50 @@ def check_env():
print("请使用Python3.8运行本项目")
input("按任意键退出...")
exit()
# try:
# print("检查依赖库中...")
# if os.path.exists('requirements.txt'):
# os.system("pip3 install -r requirements.txt")
# elif os.path.exists('QQChannelChatGPT'+ os.sep +'requirements.txt'):
# os.system('pip3 install -r QQChannelChatGPT'+ os.sep +'requirements.txt')
# os.system("clear")
# print("安装依赖库完毕...")
# except BaseException as e:
# print("安装依赖库失败,请手动安装依赖库。")
# print(e)
# input("按任意键退出...")
# exit()
# 检查key
with open(abs_path+"configs/config.yaml", 'r', encoding='utf-8') as ymlfile:
import yaml
cfg = yaml.safe_load(ymlfile)
if cfg['openai']['key'] == '' or cfg['openai']['key'] == None:
print("请先在configs/config.yaml下添加一个可用的OpenAI Key。详情请前往https://beta.openai.com/account/api-keys")
if cfg['qqbot']['appid'] == '' or cfg['qqbot']['token'] == '' or cfg['qqbot']['appid'] == None or cfg['qqbot']['token'] == None:
print("请先在configs/config.yaml下完善appid和token令牌(在https://q.qq.com/上注册一个QQ机器人即可获得)")
# 检查pip
# pip_tag = "pip"
# mm = os.system("pip -V")
# if mm != 0:
# mm1 = os.system("pip3 -V")
# if mm1 != 0:
# print("未检测到pip, 请安装Python(版本应>=3.9)")
# input("按任意键退出...")
# exit()
# else:
# pip_tag = "pip3"
if os.path.exists('requirements.txt'):
pth = 'requirements.txt'
else:
pth = 'QQChannelChatGPT'+ os.sep +'requirements.txt'
print("正在更新三方依赖库...")
try:
pipmain(['install', '-r', pth])
print("依赖库安装完毕。")
except BaseException as e:
print(e)
while True:
res = input("依赖库可能安装失败了。\n如果是报错ValueError: check_hostname requires server_hostname请尝试先关闭代理后重试。\n输入y回车重试\n输入c回车使用国内镜像源下载\n输入其他按键回车继续往下执行。")
if res == "y":
try:
pipmain(['install', '-r', pth])
print("依赖库安装完毕。")
break
except BaseException as e:
print(e)
continue
elif res == "c":
try:
pipmain(['install', '-r', pth, '-i', 'https://mirrors.aliyun.com/pypi/simple/'])
print("依赖库安装完毕。")
break
except BaseException as e:
print(e)
continue
else:
break
def get_platform():
import platform
@@ -76,6 +107,16 @@ def get_platform():
if __name__ == "__main__":
check_env()
bot_event = threading.Event()
loop = asyncio.get_event_loop()
main(loop, bot_event)
# 获取参数
args = sys.argv
if len(args) > 1:
if args[1] == '-replit':
print("[System] 启动Replit Web保活服务...")
try:
from webapp_replit import keep_alive
keep_alive()
except BaseException as e:
print(e)
print(f"[System-err] Replit Web保活服务启动失败:{str(e)}")
main()

View File

@@ -1,4 +1,3 @@
import abc
import json
import git.exc
from git.repo import Repo
@@ -7,28 +6,263 @@ import sys
import requests
from model.provider.provider import Provider
import json
import util.plugin_util as putil
import shutil
import importlib
from util import general_utils as gu
from util.cmd_config import CmdConfig as cc
from model.platform.qq import QQ
import stat
from nakuru.entities.components import (
Plain,
Image
)
from PIL import Image as PILImage
PLATFORM_QQCHAN = 'qqchan'
PLATFORM_GOCQ = 'gocq'
# 指令功能的基类,通用的(不区分语言模型)的指令就在这实现
class Command:
def __init__(self, provider: Provider):
self.provider = Provider
@abc.abstractmethod
def check_command(self, message, role, platform):
def get_plugin_modules(self):
plugins = []
try:
if os.path.exists("addons/plugins"):
plugins = putil.get_modules("addons/plugins")
return plugins
elif os.path.exists("QQChannelChatGPT/addons/plugins"):
plugins = putil.get_modules("QQChannelChatGPT/addons/plugins")
return plugins
else:
return None
except BaseException as e:
raise e
def check_command(self, message, role, platform, message_obj, cached_plugins: dict, qq_platform: QQ):
# 插件
for k, v in cached_plugins.items():
try:
hit, res = v["clsobj"].run(message, role, platform, message_obj, qq_platform)
if hit:
return True, res
except BaseException as e:
gu.log(f"{k}插件加载出现问题,原因: {str(e)}\n已安装插件: {cached_plugins.keys}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)
if self.command_start_with(message, "nick"):
return True, self.set_nick(message, platform)
return True, self.set_nick(message, platform, role)
if self.command_start_with(message, "plugin"):
return True, self.plugin_oper(message, role, cached_plugins, platform)
if self.command_start_with(message, "myid"):
return True, self.get_my_id(message_obj, platform)
if self.command_start_with(message, "nconf") or self.command_start_with(message, "newconf"):
return True, self.get_new_conf(message, role, platform)
return False, None
def get_my_id(self, message_obj, platform):
if platform == "gocq":
if message_obj.type == "GuildMessage":
return True, f"你的频道id是{str(message_obj.sender.tiny_id)}", "plugin"
else:
return True, f"你的QQ是{str(message_obj.sender.user_id)}", "plugin"
def get_new_conf(self, message, role, platform):
if role != "admin":
return False, f"你的身份组{role}没有权限使用此指令。", "newconf"
if platform == gu.PLATFORM_GOCQ:
l = message.split(" ")
if len(l) <= 1:
obj = cc.get_all()
p = gu.create_text_image("【cmd_config.json】", json.dumps(obj, indent=4, ensure_ascii=False))
return True, [Image.fromFileSystem(p)], "newconf"
return False, f"Not support or not implemented.", "newconf"
def plugin_reload(self, cached_plugins: dict, target: str = None, all: bool = False):
plugins = self.get_plugin_modules()
fail_rec = ""
if plugins != None:
for p in plugins:
try:
if p not in cached_plugins or p == target or all:
module = __import__("addons.plugins." + p + "." + p, fromlist=[p])
if p in cached_plugins:
module = importlib.reload(module)
cls = putil.get_classes(p, module)
obj = getattr(module, cls[0])()
try:
info = obj.info()
if 'name' not in info or 'desc' not in info or 'version' not in info or 'author' not in info:
fail_rec += f"载入插件{p}失败,原因: 插件信息不完整\n"
continue
if isinstance(info, dict) == False:
fail_rec += f"载入插件{p}失败,原因: 插件信息格式不正确\n"
continue
except BaseException as e:
fail_rec += f"调用插件{p} info失败, 原因: {str(e)}\n"
continue
cached_plugins[p] = {
"module": module,
"clsobj": obj,
"info": info
}
except BaseException as e:
raise e
fail_rec += f"加载{p}插件出现问题,原因{str(e)}\n"
if fail_rec == "":
return True, None
else:
return False, fail_rec
else:
return False, "未找到任何插件模块"
'''
存储机器人的昵称
插件指令
'''
def set_nick(self, message: str, platform: str):
def plugin_oper(self, message: str, role: str, cached_plugins: dict, platform: str):
l = message.split(" ")
if len(l) < 2:
if platform == gu.PLATFORM_GOCQ:
p = gu.create_text_image("【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n")
return True, [Image.fromFileSystem(p)], "plugin"
return True, "\n=====插件指令面板=====\n安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n===============", "plugin"
else:
ppath = ""
if os.path.exists("addons/plugins"):
ppath = "addons/plugins"
elif os.path.exists("QQChannelChatGPT/addons/plugins"):
ppath = "QQChannelChatGPT/addons/plugins"
else:
return False, "未找到插件目录", "plugin"
if l[1] == "i":
if role != "admin":
return False, f"你的身份组{role}没有权限安装插件", "plugin"
try:
# 得到url的最后一段
d = l[2].split("/")[-1]
# 创建文件夹
plugin_path = os.path.join(ppath, d)
if os.path.exists(plugin_path):
shutil.rmtree(plugin_path)
os.mkdir(plugin_path)
Repo.clone_from(l[2],to_path=plugin_path,branch='master')
# 读取插件的requirements.txt
if os.path.exists(os.path.join(plugin_path, "requirements.txt")):
with open(os.path.join(plugin_path, "requirements.txt"), "r", encoding="utf-8") as f:
for line in f.readlines():
mm = os.system(f"pip3 install {line.strip()}")
if mm != 0:
return False, "插件依赖安装失败需要您手动pip安装对应插件的依赖。", "plugin"
# 加载没缓存的插件
ok, err = self.plugin_reload(cached_plugins, target=d)
if ok:
return True, "插件拉取并载入成功~", "plugin"
else:
# if os.path.exists(plugin_path):
# shutil.rmtree(plugin_path)
return False, f"插件拉取载入失败。\n跟踪: \n{err}", "plugin"
except BaseException as e:
return False, f"拉取插件失败,原因: {str(e)}", "plugin"
elif l[1] == "d":
if role != "admin":
return False, f"你的身份组{role}没有权限删除插件", "plugin"
try:
# 删除文件夹
# shutil.rmtree(os.path.join(ppath, l[2]))
self.remove_dir(os.path.join(ppath, l[2]))
if l[2] in cached_plugins:
del cached_plugins[l[2]]
return True, "插件卸载成功~", "plugin"
except BaseException as e:
return False, f"卸载插件失败,原因: {str(e)}", "plugin"
elif l[1] == "u":
plugin_path = os.path.join(ppath, l[2])
try:
repo = Repo(path = plugin_path)
repo.remotes.origin.pull()
ok, err = self.plugin_reload(cached_plugins, target=l[2])
if ok:
return True, "\n更新插件成功!!", "plugin"
else:
return False, "更新插件成功,但是重载插件失败。\n问题跟踪: \n"+err, "plugin"
except BaseException as e:
return False, "更新插件失败, 请使用plugin i指令覆盖安装", "plugin"
elif l[1] == "l":
try:
plugin_list_info = "\n".join([f"{k}: \n名称: {v['info']['name']}\n简介: {v['info']['desc']}\n版本: {v['info']['version']}\n作者: {v['info']['author']}\n" for k, v in cached_plugins.items()])
if platform == gu.PLATFORM_GOCQ:
p = gu.create_text_image("【已激活插件列表】", plugin_list_info + "\n使用plugin v 插件名 查看插件帮助\n")
return True, [Image.fromFileSystem(p)], "plugin"
return True, "\n=====已激活插件列表=====\n" + plugin_list_info + "\n使用plugin v 插件名 查看插件帮助\n=================", "plugin"
except BaseException as e:
return False, f"获取插件列表失败,原因: {str(e)}", "plugin"
elif l[1] == "v":
try:
if l[2] in cached_plugins:
info = cached_plugins[l[2]]["info"]
if platform == gu.PLATFORM_GOCQ:
p = gu.create_text_image(f"【插件信息】", f"名称: {info['name']}\n{info['desc']}\n版本: {info['version']}\n作者: {info['author']}\n\n帮助:\n{info['help']}")
return True, [Image.fromFileSystem(p)], "plugin"
res = f"\n=====插件信息=====\n名称: {info['name']}\n{info['desc']}\n版本: {info['version']}作者: {info['author']}\n\n帮助:\n{info['help']}"
return True, res, "plugin"
else:
return False, "未找到该插件", "plugin"
except BaseException as e:
return False, f"获取插件信息失败,原因: {str(e)}", "plugin"
elif l[1] == "reload":
if role != "admin":
return False, f"你的身份组{role}没有权限重载插件", "plugin"
try:
ok, err = self.plugin_reload(cached_plugins, all = True)
if ok:
return True, "\n重载插件成功~", "plugin"
else:
# if os.path.exists(plugin_path):
# shutil.rmtree(plugin_path)
return False, f"插件重载失败。\n跟踪: \n{err}", "plugin"
except BaseException as e:
return False, f"插件重载失败,原因: {str(e)}", "plugin"
elif l[1] == "dev":
if role != "admin":
return False, f"你的身份组{role}没有权限开发者模式", "plugin"
return True, "cached_plugins: \n" + str(cached_plugins), "plugin"
def remove_dir(self, file_path):
while 1:
if not os.path.exists(file_path):
break
try:
shutil.rmtree(file_path)
except PermissionError as e:
err_file_path = str(e).split("\'", 2)[1]
if os.path.exists(err_file_path):
os.chmod(err_file_path, stat.S_IWUSR)
'''
nick: 存储机器人的昵称
'''
def set_nick(self, message: str, platform: str, role: str = "member"):
if role != "admin":
return True, "你无权使用该指令 :P", "nick"
if platform == PLATFORM_GOCQ:
nick = message.split(" ")[1]
l = message.split(" ")
if len(l) == 1:
return True, "【设置机器人昵称】示例:\n支持多昵称\nnick 昵称1 昵称2 昵称3", "nick"
nick = l[1:]
self.general_command_storer("nick_qq", nick)
return True, f"设置成功!现在你可以叫我{nick}来提问我啦~", "nick"
return True, f"设置成功!现在你可以叫我这些昵称来提问我啦~", "nick"
elif platform == PLATFORM_QQCHAN:
nick = message.split(" ")[2]
return False, "QQ频道平台不支持为机器人设置昵称。", "nick"
@@ -54,27 +288,41 @@ class Command:
"keyword": "设置关键词/关键指令回复",
"update": "更新面板",
"update latest": "更新到最新版本",
"update r": "重启程序",
"update r": "重启机器人",
"reset": "重置会话",
"nick": "设置机器人昵称",
"plugin": "插件安装、卸载和重载",
"/bing": "切换到bing模型",
"/gpt": "切换到OpenAI ChatGPT API",
"/revgpt": "切换到网页版ChatGPT",
"/bing 问题": "临时使用一次bing模型进行会话",
"/gpt 问题": "临时使用一次OpenAI ChatGPT API进行会话",
"/revgpt 问题": "临时使用一次网页版ChatGPT进行会话",
}
def help_messager(self, commands: dict):
def help_messager(self, commands: dict, platform: str, cached_plugins: dict = None):
try:
resp = requests.get("https://soulter.top/channelbot/notice.json").text
notice = json.loads(resp)["notice"]
except BaseException as e:
notice = ""
msg = "Github项目名QQChannelChatGPT, 有问题提交issue, 欢迎Star\n指令列表\n"
msg = "# Help Center\n## 指令列表\n"
# msg = "Github项目名QQChannelChatGPT, 有问题提交issue, 欢迎Star\n【指令列表】\n"
for key, value in commands.items():
msg += key + ": " + value + "\n"
msg += f"`{key}` - {value}\n"
# plugins
if cached_plugins != None:
plugin_list_info = "\n".join([f"`{k}` {v['info']['name']}\n{v['info']['desc']}\n" for k, v in cached_plugins.items()])
if plugin_list_info.strip() != "":
msg += "\n## 插件列表\n> 使用plugin v 插件名 查看插件帮助\n"
msg += plugin_list_info
msg += notice
if platform == gu.PLATFORM_GOCQ:
try:
# p = gu.create_text_image("【Help Center】", msg)
p = gu.create_markdown_image(msg)
return [Image.fromFileSystem(p)]
except BaseException as e:
gu.log(str(e))
return msg
return msg
# 接受可变参数
@@ -84,23 +332,40 @@ class Command:
return True
return False
# keyword: 关键字
def keyword(self, message: str, role: str):
if role != "admin":
return True, "你没有权限使用该指令", "keyword"
if len(message.split(" ")) != 3:
return True, "【设置关键词/关键指令回复】示例:\nkeyword hi 你好\n当发送hi的时候会回复你好\nkeyword /hi 你好\n当发送/hi时会回复你好", "keyword"
l = message.split(" ")
if len(l) < 3:
return True, "【设置关键词回复】示例:\nkeyword hi 你好\n当发送hi的时候会回复你好\nkeyword /hi 你好\n当发送/hi时会回复你好\n删除关键词: keyword d hi\n删除hi关键词的回复", "keyword"
del_mode = False
if l[1] == "d":
del_mode = True
try:
if os.path.exists("keyword.json"):
with open("keyword.json", "r", encoding="utf-8") as f:
keyword = json.load(f)
keyword[l[1]] = l[2]
if del_mode:
# 删除关键词
if l[2] not in keyword:
return False, "该关键词不存在", "keyword"
else: del keyword[l[2]]
else:
keyword[l[1]] = l[2]
else:
if del_mode:
return False, "该关键词不存在", "keyword"
keyword = {l[1]: l[2]}
with open("keyword.json", "w", encoding="utf-8") as f:
json.dump(keyword, f, ensure_ascii=False, indent=4)
f.flush()
if del_mode:
return True, "删除成功: "+l[2], "keyword"
return True, "设置成功: "+l[1]+" -> "+l[2], "keyword"
except BaseException as e:
return False, "设置失败: "+str(e), "keyword"
@@ -143,18 +408,10 @@ class Command:
pash_tag = "QQChannelChatGPT"+os.sep
repo.remotes.origin.pull()
# 检查是否是windows环境
# if platform.system().lower() == "windows":
# if os.path.exists("launcher.exe"):
# os.system("start launcher.exe")
# elif os.path.exists("QQChannelChatGPT\\main.py"):
# os.system("start python QQChannelChatGPT\\main.py")
# else:
# return True, "更新成功,未发现启动项,因此需要手动重启程序。"
# exit()
# else:
# py = sys.executable
# os.execl(py, py, *sys.argv)
if len(l) == 3 and l[2] == "r":
py = sys.executable
os.execl(py, py, *sys.argv)
return True, "更新成功~是否重启输入update r重启重启指令不返回任何确认信息", "update"
except BaseException as e:

View File

@@ -1,13 +1,26 @@
from model.command.command import Command
from model.provider.provider_openai_official import ProviderOpenAIOfficial
from cores.qqbot.personality import personalities
from model.platform.qq import QQ
from util import general_utils as gu
class CommandOpenAIOfficial(Command):
def __init__(self, provider: ProviderOpenAIOfficial):
self.provider = provider
self.cached_plugins = {}
def check_command(self, message: str, session_id: str, user_name: str, role, platform: str):
hit, res = super().check_command(message, role, platform)
def check_command(self,
message: str,
session_id: str,
user_name: str,
role: str,
platform: str,
message_obj,
cached_plugins: dict,
qq_platform: QQ):
self.platform = platform
hit, res = super().check_command(message, role, platform, message_obj=message_obj, cached_plugins=cached_plugins, qq_platform=qq_platform)
if hit:
return True, res
if self.command_start_with(message, "reset", "重置"):
@@ -23,7 +36,7 @@ class CommandOpenAIOfficial(Command):
elif self.command_start_with(message, "count"):
return True, self.count()
elif self.command_start_with(message, "help", "帮助"):
return True, self.help()
return True, self.help(cached_plugins)
elif self.command_start_with(message, "unset"):
return True, self.unset(session_id)
elif self.command_start_with(message, "set"):
@@ -42,7 +55,7 @@ class CommandOpenAIOfficial(Command):
return False, None
def help(self):
def help(self, cached_plugins):
commands = super().general_commands()
commands[''] = '画画'
commands['key'] = '添加OpenAI key'
@@ -50,14 +63,18 @@ class CommandOpenAIOfficial(Command):
commands['gpt'] = '查看gpt配置信息'
commands['status'] = '查看key使用状态'
commands['token'] = '查看本轮会话token'
return True, super().help_messager(commands), "help"
return True, super().help_messager(commands, self.platform, cached_plugins), "help"
def reset(self, session_id: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "reset"
self.provider.forget(session_id)
return True, "重置成功", "reset"
def his(self, message: str, session_id: str, name: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "his"
#分页每页5条
msg = ''
size_per_page = 3
@@ -74,12 +91,18 @@ class CommandOpenAIOfficial(Command):
return True, f"历史记录如下:\n{p}\n{page}页 | 共{max_page}\n*输入/his 2跳转到第2页", "his"
def token(self, session_id: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "token"
return True, f"会话的token数: {self.provider.get_user_usage_tokens(self.provider.session_dict[session_id])}\n系统最大缓存token数: {self.provider.max_tokens}", "token"
def gpt(self):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "gpt"
return True, f"OpenAI GPT配置:\n {self.provider.chatGPT_configs}", "gpt"
def status(self):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "status"
chatgpt_cfg_str = ""
key_stat = self.provider.get_key_stat()
index = 1
@@ -100,10 +123,14 @@ class CommandOpenAIOfficial(Command):
return True, f"⭐使用情况({str(gg_count)}个已用):\n{chatgpt_cfg_str}⏰全频道已用{total}tokens", "status"
def count(self):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "reset"
guild_count, guild_msg_count, guild_direct_msg_count, session_count = self.provider.get_stat()
return True, f"当前会话数: {len(self.provider.session_dict)}\n共有频道数: {guild_count} \n共有消息数: {guild_msg_count}\n私信数: {guild_direct_msg_count}\n历史会话数: {session_count}", "count"
def key(self, message: str, user_name: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "reset"
l = message.split(" ")
if len(l) == 1:
msg = "感谢您赞助keykey为官方API使用请以以下格式赞助:\n/key xxxxx"
@@ -116,11 +143,15 @@ class CommandOpenAIOfficial(Command):
return True, "该Key被验证为无效。也许是输入错误了或者重试。", "key"
def unset(self, session_id: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "unset"
self.provider.now_personality = {}
self.provider.forget(session_id)
return True, "已清除人格并重置历史记录。", "unset"
def set(self, message: str, session_id: str):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "set"
l = message.split(" ")
if len(l) == 1:
return True, f"【由Github项目QQChannelChatGPT支持】\n\n【人格文本由PlexPt开源项目awesome-chatgpt-pr \
@@ -179,6 +210,12 @@ class CommandOpenAIOfficial(Command):
return True, f"自定义人格已设置。 \n人格信息: {ps}", "set"
def draw(self, message):
if self.provider is None:
return False, "未启动OpenAI ChatGPT语言模型.", "draw"
if message.startswith("/画"):
message = message[2:]
elif message.startswith(""):
message = message[1:]
try:
# 画图模式传回3个参数
img_url = self.provider.image_chat(message)

View File

@@ -1,16 +1,25 @@
from model.command.command import Command
from model.provider.provider_rev_chatgpt import ProviderRevChatGPT
from model.platform.qq import QQ
class CommandRevChatGPT(Command):
def __init__(self, provider: ProviderRevChatGPT):
self.provider = provider
self.cached_plugins = {}
def check_command(self, message: str, role, platform: str):
hit, res = super().check_command(message, role, platform)
def check_command(self,
message: str,
role: str,
platform: str,
message_obj,
cached_plugins: dict,
qq_platform: QQ):
self.platform = platform
hit, res = super().check_command(message, role, platform, message_obj=message_obj, cached_plugins=cached_plugins, qq_platform=qq_platform)
if hit:
return True, res
if self.command_start_with(message, "help", "帮助"):
return True, self.help()
return True, self.help(cached_plugins)
elif self.command_start_with(message, "reset"):
return True, self.reset()
elif self.command_start_with(message, "update"):
@@ -25,5 +34,6 @@ class CommandRevChatGPT(Command):
def reset(self):
return False, "此功能暂未开放", "reset"
def help(self):
return True, super().help_messager(super().general_commands()), "help"
def help(self, cached_plugins: dict):
return True, super().help_messager(super().general_commands(), self.platform, cached_plugins), "help"

View File

@@ -1,18 +1,30 @@
from model.command.command import Command
from model.provider.provider_rev_edgegpt import ProviderRevEdgeGPT
import asyncio
from model.platform.qq import QQ
class CommandRevEdgeGPT(Command):
def __init__(self, provider: ProviderRevEdgeGPT):
self.provider = provider
self.cached_plugins = {}
def check_command(self, message: str, loop, role, platform: str):
hit, res = super().check_command(message, role, platform)
def check_command(self,
message: str,
loop,
role: str,
platform: str,
message_obj,
cached_plugins: dict,
qq_platform: QQ):
self.platform = platform
hit, res = super().check_command(message, role, platform, message_obj=message_obj, cached_plugins=cached_plugins, qq_platform=qq_platform)
if hit:
return True, res
if self.command_start_with(message, "reset"):
return True, self.reset(loop)
elif self.command_start_with(message, "help"):
return True, self.help()
return True, self.help(cached_plugins)
elif self.command_start_with(message, "update"):
return True, self.update(message, role)
elif self.command_start_with(message, "keyword"):
@@ -23,6 +35,8 @@ class CommandRevEdgeGPT(Command):
return False, None
def reset(self, loop):
if self.provider is None:
return False, "未启动Bing语言模型.", "reset"
res = asyncio.run_coroutine_threadsafe(self.provider.forget(), loop).result()
print(res)
if res:
@@ -30,6 +44,6 @@ class CommandRevEdgeGPT(Command):
else:
return res, "重置失败", "reset"
def help(self):
return True, super().help_messager(super().general_commands()), "help"
def help(self, cached_plugins: dict):
return True, super().help_messager(super().general_commands(), self.platform, cached_plugins), "help"

View File

@@ -1,18 +1,143 @@
from nakuru.entities.components import Plain
from nakuru.entities.components import Plain, At, Image, Node
from util import general_utils as gu
from util.cmd_config import CmdConfig
import asyncio
from nakuru import (
CQHTTP,
GuildMessage
)
import time
class FakeSource:
def __init__(self, type, group_id):
self.type = type
self.group_id = group_id
class QQ:
def __init__(self, is_start: bool, cc: CmdConfig = None, gocq_loop = None) -> None:
self.is_start = is_start
self.gocq_loop = gocq_loop
self.cc = cc
def run_bot(self, gocq):
self.client = gocq
self.client: CQHTTP = gocq
self.client.run()
async def send_qq_msg(self, source, res):
print("[System-Info] 回复QQ消息中..."+res)
# 通过消息链处理
if source.type == "GroupMessage":
await self.client.sendGroupMessage(source.group_id, [
Plain(text=res)
])
elif source.type == "FriendMessage":
await self.client.sendFriendMessage(source.user_id, [
Plain(text=res)
])
def get_msg_loop(self):
return self.gocq_loop
async def send_qq_msg(self,
source,
res,
image_mode: bool = False):
if not self.is_start:
raise Exception("管理员未启动GOCQ平台")
"""
res可以是一个数组, 也就是gocq的消息链。
插件开发者请使用send方法, 可以不用直接调用这个方法。
"""
gu.log("回复GOCQ消息: "+str(res), level=gu.LEVEL_INFO, tag="GOCQ", max_len=300)
if isinstance(source, int):
source = FakeSource("GroupMessage", source)
# str convert to CQ Message Chain
if isinstance(res, str):
res_str = res
res = []
if source.type == "GroupMessage":
res.append(At(qq=source.user_id))
res.append(Plain(text=res_str))
# if image mode, put all Plain texts into a new picture.
if image_mode and isinstance(res, list):
plains = []
news = []
for i in res:
if isinstance(i, Plain):
plains.append(i.text)
else:
news.append(i)
p = gu.create_markdown_image("".join(plains))
news.append(Image.fromFileSystem(p))
res = news
# 回复消息链
if isinstance(res, list) and len(res) > 0:
if source.type == "GuildMessage":
await self.client.sendGuildChannelMessage(source.guild_id, source.channel_id, res)
return
elif source.type == "FriendMessage":
await self.client.sendFriendMessage(source.user_id, res)
return
elif source.type == "GroupMessage":
# 过长时forward发送
plain_text_len = 0
image_num = 0
for i in res:
if isinstance(i, Plain):
plain_text_len += len(i.text)
elif isinstance(i, Image):
image_num += 1
if plain_text_len > self.cc.get('qq_forward_threshold', 200):
# 删除At
for i in res:
if isinstance(i, At):
res.remove(i)
node = Node(res)
# node.content = res
node.uin = source.self_id
node.name = f"To {source.sender.nickname}:"
node.time = int(time.time())
print(node)
nodes=[node]
await self.client.sendGroupForwardMessage(source.group_id, nodes)
return
await self.client.sendGroupMessage(source.group_id, res)
return
def send(self,
to,
res,
):
'''
提供给插件的发送QQ消息接口, 不用在外部await。
参数说明第一个参数可以是消息对象也可以是QQ群号。第二个参数是消息内容消息内容可以是消息链列表也可以是纯文字信息
'''
try:
asyncio.run_coroutine_threadsafe(self.send_qq_msg(to, res), self.gocq_loop).result()
except BaseException as e:
raise e
def send_guild(self,
message_obj,
res,
):
'''
提供给插件的发送GOCQ QQ频道消息接口, 不用在外部await。
参数说明:第一个参数必须是消息对象, 第二个参数是消息内容(消息内容可以是消息链列表,也可以是纯文字信息)。
'''
try:
asyncio.run_coroutine_threadsafe(self.send_qq_msg(message_obj, res), self.gocq_loop).result()
except BaseException as e:
raise e
def create_text_image(title: str, text: str, max_width=30, font_size=20):
'''
文本转图片。
title: 标题
text: 文本内容
max_width: 文本宽度最大值默认30
font_size: 字体大小默认20
返回:文件路径
'''
try:
img = gu.word2img(title, text, max_width, font_size)
p = gu.save_temp_img(img)
return p
except Exception as e:
raise e

View File

@@ -1,11 +1,12 @@
import io
import botpy
from PIL import Image
from botpy.message import Message, DirectMessage
import re
import asyncio
import requests
from cores.qqbot.personality import personalities
from util import general_utils as gu
class QQChan():
@@ -15,13 +16,14 @@ class QQChan():
self.client.run(appid=appid, token=token)
def send_qq_msg(self, message, res, image_mode=False, msg_ref = None):
print("[System-Info] 回复QQ频道消息中..."+res)
gu.log("回复QQ频道消息: "+str(res), level=gu.LEVEL_INFO, tag="QQ频道", max_len=30)
if not image_mode:
try:
if msg_ref is not None:
reply_res = asyncio.run_coroutine_threadsafe(message.reply(content=res, message_reference = msg_ref), self.client.loop)
reply_res = asyncio.run_coroutine_threadsafe(message.reply(content=str(res), message_reference = msg_ref), self.client.loop)
else:
reply_res = asyncio.run_coroutine_threadsafe(message.reply(content=res), self.client.loop)
reply_res = asyncio.run_coroutine_threadsafe(message.reply(content=str(res)), self.client.loop)
reply_res.result()
except BaseException as e:
# 分割过长的消息

View File

@@ -6,6 +6,7 @@ import sys
from cores.database.conn import dbConn
from model.provider.provider import Provider
import threading
from util import general_utils as gu
abs_path = os.path.dirname(os.path.realpath(sys.argv[0])) + '/'
key_record_path = abs_path+'chatgpt_key_record'
@@ -16,7 +17,7 @@ class ProviderOpenAIOfficial(Provider):
if 'api_base' in cfg and cfg['api_base'] != 'none' and cfg['api_base'] != '':
openai.api_base = cfg['api_base']
if cfg['key'] != '' and cfg['key'] != None:
print("[System] 读取ChatGPT Key成功")
gu.log("读取ChatGPT Key成功")
self.key_list = cfg['key']
else:
input("[System] 请先去完善ChatGPT的Key。详情请前往https://beta.openai.com/account/api-keys")
@@ -25,7 +26,7 @@ class ProviderOpenAIOfficial(Provider):
self.init_key_record()
self.chatGPT_configs = cfg['chatGPTConfigs']
print(f'[System] 加载ChatGPTConfigs: {self.chatGPT_configs}')
gu.log(f'加载ChatGPTConfigs: {self.chatGPT_configs}')
self.openai_configs = cfg
# 会话缓存
self.session_dict = {}
@@ -39,9 +40,10 @@ class ProviderOpenAIOfficial(Provider):
db1 = dbConn()
for session in db1.get_all_session():
self.session_dict[session[0]] = json.loads(session[1])['data']
print("[System] 历史记录读取成功喵")
gu.log("历史记录读取成功喵")
except BaseException as e:
print("[System] 历史记录读取失败: " + str(e))
gu.log("历史记录读取失败", level=gu.LEVEL_ERROR)
# 读取统计信息
if not os.path.exists(abs_path+"configs/stat"):
@@ -110,6 +112,7 @@ class ProviderOpenAIOfficial(Provider):
cache_data_list, new_record, req = self.wrap(prompt, session_id)
retry = 0
response = None
err = ''
while retry < 5:
try:
response = openai.ChatCompletion.create(
@@ -118,9 +121,8 @@ class ProviderOpenAIOfficial(Provider):
)
break
except Exception as e:
print(e)
if 'You exceeded' in str(e) or 'Billing hard limit has been reached' in str(e) or 'No API key provided' in str(e) or 'Incorrect API key provided' in str(e):
print("[System] 当前Key已超额或者不正常,正在切换")
gu.log("当前Key已超额或常, 正在切换", level=gu.LEVEL_WARNING)
self.key_stat[openai.api_key]['exceed'] = True
self.save_key_record()
@@ -130,17 +132,21 @@ class ProviderOpenAIOfficial(Provider):
raise e
else:
break
if 'maximum context length' in str(e):
print("token超限, 清空对应缓存")
elif 'maximum context length' in str(e):
gu.log("token超限, 清空对应缓存")
self.session_dict[session_id] = []
cache_data_list, new_record, req = self.wrap(prompt, session_id)
else:
gu.log(str(e), level=gu.LEVEL_ERROR)
err = str(e)
retry+=1
if retry >= 5:
raise BaseException("连接超时")
gu.log(r"如果报错, 且您的机器在中国大陆内, 请确保您的电脑已经设置好代理软件(梯子), 并在配置文件设置了系统代理地址。详见https://github.com/Soulter/QQChannelChatGPT/wiki/%E4%BA%8C%E3%80%81%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE", max_len=999)
raise BaseException("连接出错: "+str(err))
self.key_stat[openai.api_key]['used'] += response['usage']['total_tokens']
self.save_key_record()
print("[ChatGPT] "+str(response["choices"][0]["message"]["content"]))
# print("[ChatGPT] "+str(response["choices"][0]["message"]["content"]))
chatgpt_res = str(response["choices"][0]["message"]["content"]).strip()
current_usage_tokens = response['usage']['total_tokens']
@@ -192,13 +198,12 @@ class ProviderOpenAIOfficial(Provider):
image_url = []
for i in range(img_num):
image_url.append(response['data'][i]['url'])
print(image_url)
break
except Exception as e:
print(e)
gu.log(str(e), level=gu.LEVEL_ERROR)
if 'You exceeded' in str(e) or 'Billing hard limit has been reached' in str(
e) or 'No API key provided' in str(e) or 'Incorrect API key provided' in str(e):
print("[System] 当前Key已超额或者不正常,正在切换")
gu.log("当前Key已超额或者不正常, 正在切换", level=gu.LEVEL_WARNING)
self.key_stat[openai.api_key]['exceed'] = True
self.save_key_record()
@@ -305,7 +310,7 @@ class ProviderOpenAIOfficial(Provider):
if not self.key_stat[key]['exceed']:
is_all_exceed = False
openai.api_key = key
print(f"[System] 切换到Key: {key}, 已使用token: {self.key_stat[key]['used']}")
gu.log(f"切换到Key: {key}, 已使用token: {self.key_stat[key]['used']}", level=gu.LEVEL_INFO)
if len(req) > 0:
try:
response = openai.ChatCompletion.create(
@@ -314,20 +319,21 @@ class ProviderOpenAIOfficial(Provider):
)
return response, True
except Exception as e:
print(e)
if 'You exceeded' in str(e):
print("[System] 当前Key已超额,正在切换")
gu.log("当前Key已超额, 正在切换")
self.key_stat[openai.api_key]['exceed'] = True
self.save_key_record()
time.sleep(1)
continue
else:
gu.log(str(e), level=gu.LEVEL_ERROR)
else:
return True
if is_all_exceed:
print("[System] 所有Key已超额")
gu.log("所有Key已超额", level=gu.LEVEL_CRITICAL)
return None, False
else:
print("[System] 在切换key时程序异常。")
gu.log("在切换key时程序异常。", level=gu.LEVEL_ERROR)
return None, False
def getConfigs(self):
@@ -375,7 +381,7 @@ class ProviderOpenAIOfficial(Provider):
try:
self.key_stat = json.load(keyfile)
except Exception as e:
print(e)
gu.log(str(e), level=gu.LEVEL_ERROR)
self.key_stat = {}
finally:
for key in self.key_list:

View File

@@ -1,12 +1,16 @@
from revChatGPT.V1 import Chatbot
from revChatGPT import typings
from model.provider.provider import Provider
from util import general_utils as gu
class ProviderRevChatGPT(Provider):
def __init__(self, config):
self.rev_chatgpt = []
for i in range(0, len(config['account'])):
try:
print(f"[System] 创建rev_ChatGPT负载{str(i)}: " + str(config['account'][i]))
gu.log(f"创建rev_ChatGPT负载{str(i)}中...", level=gu.LEVEL_INFO, tag="RevChatGPT")
if 'password' in config['account'][i]:
config['account'][i]['password'] = str(config['account'][i]['password'])
revstat = {
@@ -15,7 +19,7 @@ class ProviderRevChatGPT(Provider):
}
self.rev_chatgpt.append(revstat)
except BaseException as e:
print(f"[System] 创建rev_ChatGPT负载失败: {str(e)}")
gu.log(f"创建rev_ChatGPT负载{str(i)}失败: {str(e)}", level=gu.LEVEL_ERROR, tag="RevChatGPT")
def forget(self) -> bool:
return False
@@ -30,41 +34,57 @@ class ProviderRevChatGPT(Provider):
for data in bot.ask(prompt):
resp = data["message"]
break
except BaseException as e:
try:
print("[RevChatGPT] 请求出现了一些问题, 正在重试。次数"+str(err_count))
err_count += 1
if err_count >= retry_count:
raise e
except BaseException:
err_count += 1
except typings.Error as e:
if e.code == typings.ErrorType.RATE_LIMIT_ERROR:
raise e
if e.code == typings.ErrorType.INVALID_ACCESS_TOKEN_ERROR:
raise e
if e.code == typings.ErrorType.EXPIRED_ACCESS_TOKEN_ERROR:
raise e
if e.code == typings.ErrorType.PROHIBITED_CONCURRENT_QUERY_ERROR:
raise e
print("[RevChatGPT] "+str(resp))
err_count += 1
gu.log(f"请求出现问题: {str(e)} | 正在重试: {str(err_count)}", level=gu.LEVEL_WARNING, tag="RevChatGPT")
if err_count >= retry_count:
raise e
except BaseException as e:
err_count += 1
gu.log(f"请求出现问题: {str(e)} | 正在重试: {str(err_count)}", level=gu.LEVEL_WARNING, tag="RevChatGPT")
if err_count >= retry_count:
raise e
if resp == '':
resp = "RevChatGPT出现故障."
# print("[RevChatGPT] "+str(resp))
return resp
def text_chat(self, prompt):
def text_chat(self, prompt) -> str:
res = ''
print("[Debug] "+str(self.rev_chatgpt))
err_msg = ''
cursor = 0
for revstat in self.rev_chatgpt:
cursor += 1
if not revstat['busy']:
try:
revstat['busy'] = True
print("[Debug] 使用逆向ChatGPT回复ing", end='', flush=True)
res = self.request_text(prompt, revstat['obj'])
print("OK")
revstat['busy'] = False
# 处理结果文本
chatgpt_res = res.strip()
return res
except Exception as e:
print("[System-Error] 逆向ChatGPT回复失败" + str(e))
try:
if e.code == 2:
print("[System-Error] 频率限制,正在切换账号。"+ str(e))
continue
else:
res = '所有的非忙碌OpenAI账号经过测试都暂时出现问题请稍后再试或者联系管理员~'
return res
except BaseException:
continue
res = '所有的OpenAI账号都有负载, 请稍后再试~'
return res.strip()
# todo: 细化错误管理
except BaseException as e:
revstat['busy'] = False
gu.log(f"请求出现问题: {str(e)}", level=gu.LEVEL_WARNING, tag="RevChatGPT")
err_msg += f"账号{cursor} - 错误原因: {str(e)}"
continue
else:
err_msg += f"账号{cursor} - 错误原因: 忙碌"
continue
res = f'回复失败。错误跟踪:{err_msg}'
return res
def is_all_busy(self) -> bool:
for revstat in self.rev_chatgpt:
if not revstat['busy']:
return False
return True

View File

@@ -1,6 +1,10 @@
from model.provider.provider import Provider
from EdgeGPT import Chatbot, ConversationStyle
import json
import os
from util import general_utils as gu
from util.cmd_config import CmdConfig as cc
class ProviderRevEdgeGPT(Provider):
def __init__(self):
@@ -8,7 +12,10 @@ class ProviderRevEdgeGPT(Provider):
self.wait_stack = []
with open('./cookies.json', 'r') as f:
cookies = json.load(f)
self.bot = Chatbot(cookies=cookies)
proxy = cc.get("bing_proxy", None)
if proxy == "":
proxy = None
self.bot = Chatbot(cookies=cookies, proxy = proxy)
def is_busy(self):
return self.busy
@@ -20,7 +27,7 @@ class ProviderRevEdgeGPT(Provider):
except BaseException:
return False
async def text_chat(self, prompt):
async def text_chat(self, prompt, platform = 'none'):
if self.busy:
return
self.busy = True
@@ -32,6 +39,8 @@ class ProviderRevEdgeGPT(Provider):
try:
resp = await self.bot.ask(prompt=prompt, conversation_style=ConversationStyle.creative)
# print("[RevEdgeGPT] "+str(resp))
if 'messages' not in resp['item']:
await self.bot.reset()
msj_obj = resp['item']['messages'][len(resp['item']['messages'])-1]
reply_msg = msj_obj['text']
if 'sourceAttributions' in msj_obj:
@@ -43,7 +52,7 @@ class ProviderRevEdgeGPT(Provider):
# print(throttling)
else:
throttling = None
if 'I\'m sorry but I prefer not to continue this conversation. I\'m still learning so I appreciate your understanding and patience.' in resp:
if 'I\'m sorry but I prefer not to continue this conversation. I\'m still learning so I appreciate your understanding and patience.' in reply_msg:
self.busy = False
return '', 0
if reply_msg == prompt:
@@ -51,29 +60,37 @@ class ProviderRevEdgeGPT(Provider):
await self.forget()
err_count += 1
continue
if reply_msg is None:
if reply_source is None:
# 不想答复
return '', 0
else:
index = 1
if len(reply_source) > 0:
reply_msg += "\n\n信息来源:\n"
for i in reply_source:
reply_msg += f"[{str(index)}]: {i['seeMoreUrl']} | {i['providerDisplayName']}\n"
index += 1
if platform != 'qqchan':
index = 1
if len(reply_source) > 0:
reply_msg += "\n\n信息来源:\n"
for i in reply_source:
reply_msg += f"[{str(index)}]: {i['seeMoreUrl']} | {i['providerDisplayName']}\n"
index += 1
if throttling is not None:
reply_msg += f"\n{throttling['numUserMessagesInConversation']}/{throttling['maxNumUserMessagesInConversation']}"
if throttling['numUserMessagesInConversation'] == throttling['maxNumUserMessagesInConversation']:
# 达到上限,重置会话
await self.forget()
if throttling['numUserMessagesInConversation'] > throttling['maxNumUserMessagesInConversation']:
await self.forget()
err_count += 1
continue
reply_msg += f"\n[{throttling['numUserMessagesInConversation']}/{throttling['maxNumUserMessagesInConversation']}]"
break
except BaseException as e:
# raise e
print(e)
gu.log(str(e), level=gu.LEVEL_WARNING, tag="RevEdgeGPT")
err_count += 1
if err_count >= retry_count:
gu.log(r"如果报错, 且您的机器在中国大陆内, 请确保您的电脑已经设置好代理软件(梯子), 并在配置文件设置了系统代理地址。详见https://github.com/Soulter/QQChannelChatGPT/wiki/%E4%BA%8C%E3%80%81%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE", max_len=999)
self.busy = False
raise e
print("[RevEdgeGPT] 请求出现了一些问题, 正在重试。次数"+str(err_count))
gu.log("请求出现了一些问题, 正在重试。次数"+str(err_count), level=gu.LEVEL_WARNING, tag="RevEdgeGPT")
self.busy = False
print("[RevEdgeGPT] "+str(reply_msg))
# print("[RevEdgeGPT] "+str(reply_msg))
return reply_msg, 1

View File

@@ -1,10 +1,10 @@
requests~=2.28.1
openai~=0.27.4
qq-botpy~=1.1.2
revChatGPT~=4.0.8
revChatGPT~=5.0.0
baidu-aip~=4.16.9
EdgeGPT~=0.1.22.1
chardet~=5.1.0
Pillow~=9.4.0
GitPython~=3.1.31
git+https://github.com/Lxns-Network/nakuru-project.git
nakuru-project

BIN
resources/fonts/simhei.ttf Normal file

Binary file not shown.

BIN
resources/fonts/syst.otf Normal file

Binary file not shown.

53
util/cmd_config.py Normal file
View File

@@ -0,0 +1,53 @@
import os
import json
cpath = "cmd_config.json"
def check_exist():
if not os.path.exists(cpath):
with open(cpath, "w", encoding="utf-8") as f:
json.dump({}, f, indent=4)
f.flush()
class CmdConfig():
@staticmethod
def get(key, default=None):
check_exist()
with open(cpath, "r", encoding="utf-8") as f:
d = json.load(f)
if key in d:
return d[key]
else:
return default
@staticmethod
def get_all():
check_exist()
with open(cpath, "r", encoding="utf-8") as f:
return json.load(f)
@staticmethod
def put(key, value):
check_exist()
with open(cpath, "r", encoding="utf-8") as f:
d = json.load(f)
d[key] = value
with open(cpath, "w", encoding="utf-8") as f:
json.dump(d, f, indent=4)
f.flush()
@staticmethod
def init_attributes(keys: list, init_val = ""):
check_exist()
with open(cpath, "r", encoding="utf-8") as f:
d = json.load(f)
_tag = False
for k in keys:
if k not in d:
d[k] = init_val
_tag = True
if _tag:
with open(cpath, "w", encoding="utf-8") as f:
json.dump(d, f, indent=4)
f.flush()

493
util/general_utils.py Normal file
View File

@@ -0,0 +1,493 @@
import datetime
import time
import socket
from PIL import Image, ImageDraw, ImageFont
import os
import re
import requests
PLATFORM_GOCQ = 'gocq'
PLATFORM_QQCHAN = 'qqchan'
FG_COLORS = {
"black": "30",
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"purple": "35",
"cyan": "36",
"white": "37",
"default": "39",
}
BG_COLORS = {
"black": "40",
"red": "41",
"green": "42",
"yellow": "43",
"blue": "44",
"purple": "45",
"cyan": "46",
"white": "47",
"default": "49",
}
LEVEL_INFO = "INFO"
LEVEL_WARNING = "WARNING"
LEVEL_ERROR = "ERROR"
LEVEL_CRITICAL = "CRITICAL"
level_colors = {
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "purple",
}
def log(
msg: str,
level: str = "INFO",
tag: str = "System",
fg: str = None,
bg: str = None,
max_len: int = 100):
"""
日志记录函数
"""
if len(msg) > max_len:
msg = msg[:max_len] + "..."
now = datetime.datetime.now().strftime("%m-%d %H:%M:%S")
pre = f"[{now}] [{level}] [{tag}]: {msg}"
if level == "INFO":
if fg is None:
fg = FG_COLORS["green"]
if bg is None:
bg = BG_COLORS["default"]
elif level == "WARNING":
if fg is None:
fg = FG_COLORS["yellow"]
if bg is None:
bg = BG_COLORS["default"]
elif level == "ERROR":
if fg is None:
fg = FG_COLORS["red"]
if bg is None:
bg = BG_COLORS["default"]
elif level == "CRITICAL":
if fg is None:
fg = FG_COLORS["purple"]
if bg is None:
bg = BG_COLORS["default"]
print(f"\033[{fg};{bg}m{pre}\033[0m")
def port_checker(port: int, host: str = "localhost"):
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.settimeout(1)
try:
sk.connect((host, port))
sk.close()
return True
except Exception:
sk.close()
return False
def word2img(title: str, text: str, max_width=30, font_size=20):
if os.path.exists("resources/fonts/syst.otf"):
font_path = "resources/fonts/syst.otf"
elif os.path.exists("QQChannelChatGPT/resources/fonts/syst.otf"):
font_path = "QQChannelChatGPT/resources/fonts/syst.otf"
elif os.path.exists("C:/Windows/Fonts/simhei.ttf"):
font_path = "C:/Windows/Fonts/simhei.ttf"
elif os.path.exists("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"):
font_path = "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"
else:
raise Exception("找不到字体文件")
width_factor = 1.0
height_factor = 1.5
# 格式化文本宽度最大为30
lines = text.split('\n')
i = 0
length = len(lines)
for l in lines:
if len(l) > max_width:
# lines[i] = l[:max_width] + '\n' + l[max_width:]
# for
cp = l
for ii in range(len(l)):
if ii % max_width == 0:
cp = cp[:ii] + '\n' + cp[ii:]
length += 1
lines[i] = cp
i += 1
text = '\n'.join(lines)
width = int(max_width * font_size * width_factor)
height = int(length * font_size * height_factor)
image = Image.new('RGB', (width, height), (255, 255, 255))
draw = ImageDraw.Draw(image)
text_font = ImageFont.truetype(font_path, font_size)
title_font = ImageFont.truetype(font_path, font_size + 5)
# 标题居中
title_width, title_height = title_font.getsize(title)
draw.text(((width - title_width) / 2, 10), title, fill=(0, 0, 0), font=title_font)
# 文本不居中
draw.text((10, title_height+20), text, fill=(0, 0, 0), font=text_font)
return image
def render_markdown(markdown_text, image_width=800, image_height=600, font_size=26, font_color=(0, 0, 0), bg_color=(255, 255, 255)):
HEADER_MARGIN = 20
HEADER_FONT_STANDARD_SIZE = 42
QUOTE_LEFT_LINE_MARGIN = 10
QUOTE_FONT_LINE_MARGIN = 6 # 引用文字距离左边线的距离和上下的距离
QUOTE_LEFT_LINE_HEIGHT = font_size + QUOTE_FONT_LINE_MARGIN * 2
QUOTE_LEFT_LINE_WIDTH = 5
QUOTE_LEFT_LINE_COLOR = (180, 180, 180)
QUOTE_FONT_SIZE = font_size
QUOTE_FONT_COLOR = (180, 180, 180)
# QUOTE_BG_COLOR = (255, 255, 255)
CODE_BLOCK_MARGIN = 10
CODE_BLOCK_FONT_SIZE = font_size
CODE_BLOCK_FONT_COLOR = (255, 255, 255)
CODE_BLOCK_BG_COLOR = (240, 240, 240)
CODE_BLOCK_CODES_MARGIN_VERTICAL = 5 # 代码块和代码之间的距离
CODE_BLOCK_CODES_MARGIN_HORIZONTAL = 5 # 代码块和代码之间的距离
CODE_BLOCK_TEXT_MARGIN = 4 # 代码和代码之间的距离
INLINE_CODE_MARGIN = 8
INLINE_CODE_FONT_SIZE = font_size
INLINE_CODE_FONT_COLOR = font_color
INLINE_CODE_FONT_MARGIN = 4
INLINE_CODE_BG_COLOR = (230, 230, 230)
INLINE_CODE_BG_HEIGHT = INLINE_CODE_FONT_SIZE + INLINE_CODE_FONT_MARGIN * 2
LIST_MARGIN = 8
LIST_FONT_SIZE = font_size
LIST_FONT_COLOR = font_color
TEXT_LINE_MARGIN = 8
IMAGE_MARGIN = 15
# 用于匹配图片的正则表达式
IMAGE_REGEX = r"!\s*\[.*?\]\s*\((.*?)\)"
if os.path.exists("resources/fonts/syst.otf"):
font_path = "resources/fonts/syst.otf"
elif os.path.exists("QQChannelChatGPT/resources/fonts/syst.otf"):
font_path = "QQChannelChatGPT/resources/fonts/syst.otf"
elif os.path.exists("C:/Windows/Fonts/simhei.ttf"):
font_path = "C:/Windows/Fonts/simhei.ttf"
elif os.path.exists("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"):
font_path = "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"
else:
raise Exception("找不到字体文件")
# backup
if os.path.exists("resources/fonts/simhei.ttf"):
font_path1 = "resources/fonts/simhei.ttf"
elif os.path.exists("QQChannelChatGPT/resources/fonts/simhei.ttf"):
font_path1 = "QQChannelChatGPT/resources/fonts/simhei.ttf"
else:
font_path1 = font_path
# 加载字体
font = ImageFont.truetype(font_path, font_size)
images: Image = {}
# pre_process, get height of each line
pre_lines = markdown_text.split('\n')
height = 0
pre_in_code = False
i = -1
_pre_lines = []
for line in pre_lines:
i += 1
# 处理图片
if re.search(IMAGE_REGEX, line):
try:
image_url = re.findall(IMAGE_REGEX, line)[0]
print(image_url)
image_res = Image.open(requests.get(image_url, stream=True, timeout=5).raw)
images[i] = image_res
# 最大不得超过image_width的50%
img_height = image_res.size[1]
if image_res.size[0] > image_width*0.5:
image_res = image_res.resize((int(image_width*0.5), int(image_res.size[1]*image_width*0.5/image_res.size[0])))
img_height = image_res.size[1]
height += img_height + IMAGE_MARGIN*2
line = re.sub(IMAGE_REGEX, "", line)
except Exception as e:
print(e)
line = re.sub(IMAGE_REGEX, "\n[加载失败的图片]\n", line)
continue
line.replace("\t", " ")
if font.getsize(line)[0] > image_width:
cp = line
_width = 0
_word_cnt = 0
for ii in range(len(line)):
# 检测是否是中文
_width += font.getsize(line[ii])[0]
_word_cnt+=1
if _width > image_width:
_pre_lines.append(cp[:_word_cnt])
cp = cp[_word_cnt:]
_word_cnt=0
_width=0
_pre_lines.append(cp)
else:
_pre_lines.append(line)
pre_lines = _pre_lines
i=-1
for line in pre_lines:
if line == "":
height += TEXT_LINE_MARGIN
continue
i += 1
line = line.strip()
if pre_in_code and not line.startswith("```"):
height += font_size + CODE_BLOCK_TEXT_MARGIN
# pre_codes.append(line)
continue
if line.startswith("#"):
header_level = line.count("#")
height += HEADER_FONT_STANDARD_SIZE + HEADER_MARGIN*2 - header_level * 4
elif line.startswith("-"):
height += font_size+LIST_MARGIN*2
elif line.startswith(">"):
height += font_size+QUOTE_LEFT_LINE_MARGIN*2
elif line.startswith("```"):
if pre_in_code:
pre_in_code = False
# pre_codes = []
height += CODE_BLOCK_MARGIN
else:
pre_in_code = True
height += CODE_BLOCK_MARGIN
elif re.search(r"`(.*?)`", line):
height += font_size+INLINE_CODE_FONT_MARGIN*2+INLINE_CODE_MARGIN*2
else:
height += font_size + TEXT_LINE_MARGIN*2
markdown_text = '\n'.join(pre_lines)
print("Pre process done, height: ", height)
image_height = height
if image_height < 100:
image_height = 100
image_width += 20
# 创建空白图像
image = Image.new('RGB', (image_width, image_height), bg_color)
draw = ImageDraw.Draw(image)
# # get all the emojis unicode in the markdown text
# unicode_text = markdown_text.encode('unicode_escape').decode()
# # print(unicode_text)
# unicode_emojis = re.findall(r'\\U\w{8}', unicode_text)
# emoji_base_url = "https://abs.twimg.com/emoji/v1/72x72/{unicode_emoji}.png"
# 设置初始位置
x, y = 10, 10
# 解析Markdown文本
lines = markdown_text.split("\n")
# lines = pre_lines
in_code_block = False
code_block_start_y = 0
code_block_codes = []
index = -1
for line in lines:
index += 1
if in_code_block and not line.startswith("```"):
code_block_codes.append(line)
y += font_size + CODE_BLOCK_TEXT_MARGIN
continue
line = line.strip()
if line.startswith("#"):
# unicode_emojis = re.findall(r'\\U0001\w{4}', line)
# for unicode_emoji in unicode_emojis:
# line = line.replace(unicode_emoji, "")
# unicode_emoji = ""
# if len(unicode_emojis) > 0:
# unicode_emoji = unicode_emojis[0]
# 处理标题
header_level = line.count("#")
line = line.strip("#").strip()
font_size_header = HEADER_FONT_STANDARD_SIZE - header_level * 4
# if unicode_emoji != "":
# emoji_url = emoji_base_url.format(unicode_emoji=unicode_emoji[-5:])
# emoji = Image.open(requests.get(emoji_url, stream=True).raw)
# emoji = emoji.resize((font_size, font_size))
# image.paste(emoji, (x, y))
# x += font_size
font = ImageFont.truetype(font_path, font_size_header)
y += HEADER_MARGIN # 上边距
# 字间距
draw.text((x, y), line, font=font, fill=font_color)
draw.line((x, y + font_size_header + 8, image_width - 10, y + font_size_header + 8), fill=(230, 230, 230), width=3)
y += font_size_header + HEADER_MARGIN
elif line.startswith(">"):
# 处理引用
quote_text = line.strip(">")
# quote_width = image_width - 20 # 引用框的宽度为图像宽度减去左右边距
# quote_height = font_size + 10 # 引用框的高度为字体大小加上上下边距
# quote_box = (x, y, x + quote_width, y + quote_height)
# draw.rounded_rectangle(quote_box, radius=5, fill=(230, 230, 230), width=2) # 使用灰色填充矩形框作为引用背景
y+=QUOTE_LEFT_LINE_MARGIN
draw.line((x, y, x, y + QUOTE_LEFT_LINE_HEIGHT), fill=QUOTE_LEFT_LINE_COLOR, width=QUOTE_LEFT_LINE_WIDTH)
font = ImageFont.truetype(font_path, QUOTE_FONT_SIZE)
draw.text((x + QUOTE_FONT_LINE_MARGIN, y + QUOTE_FONT_LINE_MARGIN), quote_text, font=font, fill=QUOTE_FONT_COLOR)
y += font_size + QUOTE_LEFT_LINE_HEIGHT + QUOTE_LEFT_LINE_MARGIN
elif line.startswith("-"):
# 处理列表
list_text = line.strip("-").strip()
font = ImageFont.truetype(font_path, LIST_FONT_SIZE)
y += LIST_MARGIN
draw.text((x, y), " · " + list_text, font=font, fill=LIST_FONT_COLOR)
y += font_size + LIST_MARGIN
elif line.startswith("```"):
if not in_code_block:
code_block_start_y = y+CODE_BLOCK_MARGIN
in_code_block = True
else:
# print(code_block_codes)
in_code_block = False
codes = "\n".join(code_block_codes)
code_block_codes = []
draw.rounded_rectangle((x, code_block_start_y, image_width - 10, y+CODE_BLOCK_CODES_MARGIN_VERTICAL + CODE_BLOCK_TEXT_MARGIN), radius=5, fill=CODE_BLOCK_BG_COLOR, width=2)
font = ImageFont.truetype(font_path1, CODE_BLOCK_FONT_SIZE)
draw.text((x + CODE_BLOCK_CODES_MARGIN_HORIZONTAL, code_block_start_y + CODE_BLOCK_CODES_MARGIN_VERTICAL), codes, font=font, fill=font_color)
y += CODE_BLOCK_CODES_MARGIN_VERTICAL + CODE_BLOCK_MARGIN
# y += font_size+10
elif re.search(r"`(.*?)`", line):
y += INLINE_CODE_MARGIN # 上边距
# 处理行内代码
code_regex = r"`(.*?)`"
parts_inline = re.findall(code_regex, line)
# print(parts_inline)
parts = re.split(code_regex, line)
# print(parts)
for part in parts:
# the judge has a tiny bug.
# when line is like "hi`hi`". all the parts will be in parts_inline.
if part in parts_inline:
font = ImageFont.truetype(font_path, INLINE_CODE_FONT_SIZE)
code_text = part.strip("`")
code_width = font.getsize(code_text)[0] + INLINE_CODE_FONT_MARGIN*2
x += INLINE_CODE_MARGIN
code_box = (x, y, x + code_width, y + INLINE_CODE_BG_HEIGHT)
draw.rounded_rectangle(code_box, radius=5, fill=INLINE_CODE_BG_COLOR, width=2) # 使用灰色填充矩形框作为引用背景
draw.text((x+INLINE_CODE_FONT_MARGIN, y), code_text, font=font, fill=font_color)
x += code_width+INLINE_CODE_MARGIN-INLINE_CODE_FONT_MARGIN
else:
font = ImageFont.truetype(font_path, font_size)
draw.text((x, y), part, font=font, fill=font_color)
x += font.getsize(part)[0]
y += font_size + INLINE_CODE_MARGIN
x = 10
else:
# 处理普通文本
if line == "":
y += TEXT_LINE_MARGIN
else:
font = ImageFont.truetype(font_path, font_size)
draw.text((x, y), line, font=font, fill=font_color)
y += font_size + TEXT_LINE_MARGIN*2
# 图片特殊处理
if index in images:
image_res = images[index]
# 最大不得超过image_width的50%
if image_res.size[0] > image_width*0.5:
image_res = image_res.resize((int(image_width*0.5), int(image_res.size[1]*image_width*0.5/image_res.size[0])))
image.paste(image_res, (IMAGE_MARGIN, y))
y += image_res.size[1] + IMAGE_MARGIN*2
return image
def save_temp_img(img: Image) -> str:
if not os.path.exists("temp"):
os.makedirs("temp")
# 获得文件创建时间清除超过1小时的
try:
for f in os.listdir("temp"):
path = os.path.join("temp", f)
if os.path.isfile(path):
ctime = os.path.getctime(path)
if time.time() - ctime > 3600:
os.remove(path)
except Exception as e:
log(f"清除临时文件失败: {e}", level=LEVEL_WARNING, tag="GeneralUtils")
# 获得时间戳
timestamp = int(time.time())
p = f"temp/{timestamp}.png"
img.save(p)
return p
def create_text_image(title: str, text: str, max_width=30, font_size=20):
'''
文本转图片。
title: 标题
text: 文本内容
max_width: 文本宽度最大值默认30
font_size: 字体大小默认20
返回:文件路径
'''
try:
img = word2img(title, text, max_width, font_size)
p = save_temp_img(img)
return p
except Exception as e:
raise e
def create_markdown_image(text: str):
'''
markdown文本转图片。
返回:文件路径
'''
try:
img = render_markdown(text)
p = save_temp_img(img)
return p
except Exception as e:
raise e
def test_markdown():
# 示例使用
markdown_text = """# Help Center
! [] (https://soulter.top/helpme.jpg)
"""
image = render_markdown(markdown_text)
image.show()
test_markdown()

View File

@@ -1,11 +0,0 @@
import logging
from logging.handlers import RotatingFileHandler
import colorlog
logger = logging.getLogger("QQChannelChatGPT")
logger.setLevel(logging.DEBUG)
handler = colorlog.StreamHandler()
fmt = "%(log_color)s[%(name)s] %(message)s"
handler.setFormatter(colorlog.ColoredFormatter(
fmt))
logger.addHandler(handler)

22
util/plugin_util.py Normal file
View File

@@ -0,0 +1,22 @@
import os
import inspect
# 找出模块里所有的类名
def get_classes(p_name, arg):
classes = []
clsmembers = inspect.getmembers(arg, inspect.isclass)
for (name, _) in clsmembers:
# print(name, p_name)
if p_name.lower() == name.lower()[:-6]:
classes.append(name)
break
return classes
# 获取一个文件夹下所有的模块
def get_modules(path):
modules = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".py") and not file.startswith("__"):
modules.append(file[:-3])
return modules

25
webapp_replit.py Normal file
View File

@@ -0,0 +1,25 @@
from flask import Flask
from threading import Thread
import datetime
app = Flask(__name__)
@app.route('/')
def main_func():
content = "<h1>QQChannelChatGPT Web APP</h1>"
content += "<p>" + "Online @ " + str(datetime.datetime.now()) + "</p>"
content += "<p>欢迎Star本项目</p>"
return content
def run():
app.run(host="0.0.0.0", port=8080)
def keep_alive():
server = Thread(target=run)
server.start()