一个简单且很糙的 ChatGPT TG Bot

2022-12-12 21:37:33 +08:00
 sleeplessy
Repo: https://github.com/code-monad/ChatGPTBot
功能要点:

1. 支持回滚。 使用指令`/rollback`可以反悔,让 ChatGPT 忘记当前 session 下的上一条你的 prompt
2. 支持保存对话网络,支持历史加载(快速加载调教效果)。程序关闭时会把你保存的历史记录保存在本地目录的`memories.sav`下,启动时会自动加载。效果如下图:
3. 支持代理设置。需要在配置文件中配置相关的字段。因为 api 相关问题,你可能还需要额外传`HTTPS_PROX=xxx`环境变量运行。

https://imgur.com/a/FhEbrF3
https://imgur.com/a/PByivQK
https://imgur.com/a/PQu3afm


很久没写 py 了,代码毕竟糙,不过能跑,逻辑应该理解起来也不难,比网页的体验要好不少,关键是方便。做了最简单的 debug/error handle ,出了问题的时候请尽量先自己查
https://github.com/acheong08/ChatGPT/wiki
为了防止一键滥用,目前默认是白名单模式,只会回复配置文件中 `allow` 字段配置的 tg 用户 ID 。
关于如何获取你的 tg ID:tg 中搜索 @userinfobot
5181 次点击
所在节点    OpenAI
44 条回复
sleeplessy
2022-12-13 12:57:00 +08:00
@xiaoz 已经在 main branch 了,他前不久合并更新的。我上一条回复说的就是这个更新后的一些变更。
genhikari
2022-12-13 16:55:17 +08:00
大佬,抱歉,我基本没接触过编程之类的东西。V2 也是我搜索时无意发现的,并不知道它是什么性质的论坛。折腾了一天,前面四步我百度后都完成了,但是用 idle 运行 Chatgpt-main 中的 setup.py 它会显示 Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\chatgpt\ChatGPT-main\setup.py", line 20, in <module>
long_description=open(" README.md", encoding="utf-8").read(),
FileNotFoundError: [Errno 2] No such file or directory: ' README.md'
而运行 python-telegram-bot-master 的 setup.py 它会显示 SystemExit: usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help

error: no commands supplied
请问该怎么解决啊?
limon
2022-12-13 19:09:37 +08:00
2022-12-13 19:05:34.778 | INFO | __main__:<module>:290 - Loading memories from memories.sav
2022-12-13 19:05:34.778 | INFO | __main__:<module>:292 - Memories:{}
/home/limon/project/ChatGPTBot/main.py:317: PTBUserWarning: No `JobQueue` set up. To use `JobQueue`, you must install PTB via `pip install python-telegram-bot[job_queue]`.
application.job_queue.run_repeating(refresh_session, interval=20 * 60, first=20 * 60)
Traceback (most recent call last):
File "/home/limon/project/ChatGPTBot/main.py", line 317, in <module>
application.job_queue.run_repeating(refresh_session, interval=20 * 60, first=20 * 60)
AttributeError: 'NoneType' object has no attribute 'run_repeating'
2022-12-13 19:05:34.790 | INFO | __main__:save_datas:258 - Saving datas...
2022-12-13 19:05:34.790 | INFO | __main__:save_datas:259 - Updating memories..
2022-12-13 19:05:34.791 | INFO | __main__:save_datas:264 - Updating Session keys...
limon
2022-12-13 19:12:40 +08:00
@limon 没事了,pip install 'python-telegram-bot[all]' 就好了
l1ve
2022-12-13 20:16:26 +08:00
M1 mac 是不能用 chrome 登录吗。。。
sleeplessy
2022-12-13 20:54:53 +08:00
@l1ve 暂时没测试过,理论上应该没有问题,不过可能需要设置下 binary 地址
sleeplessy
2022-12-13 20:57:03 +08:00
@genhikari 你如果不明白的话,建议你安装 git for windows ,然后复制我发的那段命令
tangneo
2022-12-13 21:36:12 +08:00
@sleeplessy 大佬,我那个初始化问题,暂时无解吗?
sleeplessy
2022-12-13 22:01:47 +08:00
@yaovenus 更新所有依赖到最新版就行了
sleeplessy
2022-12-13 22:07:24 +08:00
@genhikari
python setup.py install
genhikari
2022-12-13 23:13:40 +08:00
用 git for windows 它说 bash: python: command not found
用 cmd 它一直提示找不到 READ.md
XiaoBaiYa
2022-12-14 00:01:38 +08:00
期待 docker 版本
m1s4k1
2022-12-14 03:55:15 +08:00
部署在远端 server
Playwright 这个库一直在抛出异常,最终.....

```
An exception was raised while handling an update
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 318, in refresh_session
asyncio.run(self.get_cf_cookies())
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio-1.5.6-py3.10.egg/nest_asyncio.py", line 35, in run
return loop.run_until_complete(task)
File "/usr/local/lib/python3.10/dist-packages/nest_asyncio-1.5.6-py3.10.egg/nest_asyncio.py", line 90, in run_until_complete
return f.result()
File "/usr/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception.with_traceback(self._exception_tb)
File "/usr/lib/python3.10/asyncio/tasks.py", line 232, in __step
result = coro.send(None)
File "/home/ubuntu/.local/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 368, in get_cf_cookies
browser = await p.chromium.launch(headless=False)
File "/usr/local/lib/python3.10/dist-packages/playwright-1.28.0-py3.10-linux-x86_64.egg/playwright/async_api/_generated.py", line 12809, in launch
await self._impl_obj.launch(
File "/usr/local/lib/python3.10/dist-packages/playwright-1.28.0-py3.10-linux-x86_64.egg/playwright/_impl/_browser_type.py", line 93, in launch
Browser, from_channel(await self._channel.send("launch", params))
File "/usr/local/lib/python3.10/dist-packages/playwright-1.28.0-py3.10-linux-x86_64.egg/playwright/_impl/_connection.py", line 44, in send
return await self._connection.wrap_api_call(
File "/usr/local/lib/python3.10/dist-packages/playwright-1.28.0-py3.10-linux-x86_64.egg/playwright/_impl/_connection.py", line 419, in wrap_api_call
return await cb()
File "/usr/local/lib/python3.10/dist-packages/playwright-1.28.0-py3.10-linux-x86_64.egg/playwright/_impl/_connection.py", line 79, in inner_send
result = next(iter(done)).result()
File "/usr/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception.with_traceback(self._exception_tb)
playwright._impl._api_types.Error:
╔════════════════════════════════════════════════════════════════════════════════════════════════╗
║ Looks like you launched a headed browser without having a XServer running. ║
║ Set either 'headless: true' or use 'xvfb-run <your-playwright-app>' before running Playwright. ║
║ ║
║ <3 Playwright Team ║
╚════════════════════════════════════════════════════════════════════
```
sleeplessy
2022-12-14 04:04:20 +08:00
@m1s4k1 这个库最新的版本依赖非 headless 的 Chome ,你可能需要在服务器上安装 xvfb 来模拟一个假的 xserver 。
安装后用 xvfb-run python main.py 即可
m1s4k1
2022-12-14 04:14:28 +08:00
@sleeplessy 帅气, 已解决
tangneo
2022-12-14 08:48:44 +08:00
@sleeplessy 大佬,我重新下载后安装了一遍。还是报错初始化失败呢。
sleeplessy
2022-12-14 09:25:24 +08:00
@yaovenus 更新最新的 revChatGPT:
pip3 install git+http://github.com/acheong08/ChatGPT
同时更新我最新的代码即可。
meiweijia
2022-12-14 09:37:22 +08:00
Playwright 是这个的原因吗?
```
An exception was raised while handling an update
Traceback (most recent call last):
File "/mnt/hdd/code/ChatGPTBot/main.py", line 124, in start
chatbot = Chatbot(config, conversation_id=None, request_timeout=300)
File "/home/mei/.local/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 114, in __init__
self.refresh_session()
File "/home/mei/.local/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 293, in refresh_session
asyncio.run(self.get_cf_cookies())
File "/home/mei/.local/lib/python3.10/site-packages/nest_asyncio.py", line 35, in run
return loop.run_until_complete(task)
File "/home/mei/.local/lib/python3.10/site-packages/nest_asyncio.py", line 90, in run_until_complete
return f.result()
File "/usr/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception.with_traceback(self._exception_tb)
File "/usr/lib/python3.10/asyncio/tasks.py", line 232, in __step
result = coro.send(None)
File "/home/mei/.local/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 368, in get_cf_cookies
browser = await p.chromium.launch(headless=False)
File "/home/mei/.local/lib/python3.10/site-packages/playwright/async_api/_generated.py", line 12809, in launch
await self._impl_obj.launch(
File "/home/mei/.local/lib/python3.10/site-packages/playwright/_impl/_browser_type.py", line 93, in launch
Browser, from_channel(await self._channel.send("launch", params))
File "/home/mei/.local/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 44, in send
return await self._connection.wrap_api_call(
File "/home/mei/.local/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 419, in wrap_api_call
return await cb()
File "/home/mei/.local/lib/python3.10/site-packages/playwright/_impl/_connection.py", line 79, in inner_send
result = next(iter(done)).result()
File "/usr/lib/python3.10/asyncio/futures.py", line 201, in result
raise self._exception.with_traceback(self._exception_tb)
playwright._impl._api_types.Error: Executable doesn't exist at /home/mei/.cache/ms-playwright/chromium-1033/chrome-linux/chrome
╔════════════════════════════════════════════════════════════╗
║ Looks like Playwright was just installed or updated. ║
║ Please run the following command to download new browsers: ║
║ ║
║ playwright install ║
║ ║
║ <3 Playwright Team ║
╚════════════════════════════════════════════════════════════╝

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/ext/_application.py", line 1057, in process_update
await coroutine
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/ext/_handler.py", line 143, in handle_update
return await self.callback(update, context)
File "/mnt/hdd/code/ChatGPTBot/main.py", line 128, in start
await update.message.reply_text(emojize(":sweat_drops:初始化失败! 原因: `{}` ".format(e)),
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/_message.py", line 875, in reply_text
return await self.get_bot().send_message(
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/ext/_extbot.py", line 2482, in send_message
return await super().send_message(
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/_bot.py", line 313, in decorator
result = await func(*args, **kwargs) # skipcq: PYL-E1102
File "/home/mei/.local/lib/python3.10/site-packages/python_telegram_bot-20.0a6-py3.10.egg/telegram/_bot.py", line 734, in send_message
return await self._send_message( # type: ignore[retu
```
sleeplessy
2022-12-14 09:46:37 +08:00
@meiweijia 参考我前面的消息。
最简单的修复方式,安装 xvfb(可以通过 apt,pacman 之类的安装)运行下面这个命令:
python3 -m playwright install
然后启动命令改成:
xvfb-run +命令
meiweijia
2022-12-14 09:52:13 +08:00
@sleeplessy 好的,谢谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/902050

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX