1.实现思路
- 通过Chat GPT作为API,提供对话机器人功能
- 通过PyOfficeRobot实现微信的操作
- 使用ChatGPT做聊天机器人,这肯定==大材小用==
2.关于Chat GPT
最近ChatGPT很火,可以以对话的形式与用户交互,这使得ChatGPT能够回答问题、承认错误、质疑假设、拒绝不当问题。
- 常用算法实现
- 给程序找BUG
- 解答奥数问题
确实有点东西,下图为例,不但有代码,还有思路解答
3.关于PyOfficeRobot 的微信聊天机器人
- 非基于网页版微信的机器人
- 详情:https://github.com/CoderWanFeng/PyOfficeRobot
4. ChatGPT OpenAI注册
需要非HK,TW,CN的IP节点,一般日韩ok,实在不行TB搞一个
5.代码实现
关于session_token 的获取可参照 https://github.com/acheong08/ChatGPT/wiki/Setup
要么使用session_token,要么使用邮箱密码
from PyOfficeRobot.core.WeChatType import *
import datetime
from revChatGPT.revChatGPT import Chatbot
global config
config = {
# "email": "<邮箱地址>",
# "password": "<密码>",
"session_token": "<session_token>"
# Deprecated. Use only if you encounter captcha with email/password
# "proxy": "<HTTP/HTTPS_PROXY>"}
global chatbot
chatbot = Chatbot(config, conversation_id=None)
def get_ans(_message):
res = chatbot.get_chat_response(_message)['message']
print(res+'\n')
return res
wx = WeChat()
def chat_by_bots(who):
wx.GetSessionList() # 获取会话列表
wx.ChatWith(who) # 打开`who`聊天窗口
# wx.SendMsg('开始自动聊天模式')
# wx.SendMsg('正在启动')
# wx.SendMsg('3')
# wx.SendMsg('2')
# wx.SendMsg('1')
# wx.SendMsg('成功开启')
# wx.SendMsg('Hello,伙计们')
temp_msg = ''
while True:
try:
friend_name, receive_msg = wx.GetAllMessage[-1][0], wx.GetAllMessage[-1][1] # 获取朋友的名字、发送的信息
if (friend_name == who) & (receive_msg != temp_msg):
with open('./chatlog/chatlog.txt', 'a', encoding='utf8') as f:
f.write(friend_name + receive_msg + '---' + str(datetime.datetime.now()) + '\n')
"""
条件:
朋友名字正确:(friend_name == who)
不是上次的对话:(receive_msg != temp_msg)
对方内容在自己的预设里:(receive_msg in kv.keys())
"""
temp_msg = receive_msg
ans1= get_ans(receive_msg)
wx.SendMsg(ans1) # 向`who`发送消息
with open('./chatlog/chatlog.txt', 'a', encoding='utf8') as f:
f.write('me:'+ans1+'---'+str(datetime.datetime.now())+'\n')
print(who+receive_msg)
print('me:'+ans1+'---'+str(datetime.datetime.now())+'\n')
except Exception as e:
print(e)
pass
if __name__ == '__main__':
# wx = WeChat()
chat_by_bots('<微信ID>')
主题测试文章,只做测试使用。发布者:yuri,转转请注明出处:http://www.yunjichang.com/%e4%bd%bf%e7%94%a8chatgpt%e5%81%9a%e4%b8%80%e4%b8%aa%e5%be%ae%e4%bf%a1%e8%81%8a%e5%a4%a9%e6%9c%ba%e5%99%a8%e4%ba%ba-3/