ChatGPT Robot for WhatsApp
Architecture
In [ ]:
import IPython
In [ ]:
from revChatGPT.V1 import Chatbot, configure
bot = Chatbot(
config = configure(),
conversation_id = 'fdf52ab3-f8c1-450f-86f2-5a25d43fc48d',
lazy_loading = True
)
In [ ]:
for response in bot.ask('''
我刚刚有了一个 WhatsApp 群聊机器人的设计思路。我可以利用 https://web.whatsapp.com/ ,然后通过谷歌浏览器插件的方式实现 BioGPT 群聊机器人。
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
有利用 https://web.whatsapp.com/ ,然后通过谷歌浏览器插件的方式实现 WhatsApp 群聊机器人的开源项目吗?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
比较 Whatsapp-bot whatsapp-web.js venom-bot
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
详细介绍 whatsapp-web.js
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
详细说明 whatsapp-web.js 的原理
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
什么是 Puppeteer ?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
puppeteer 的 --no-sandbox 有什么用?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
Body
Ask ChatGPT by Python
In [ ]:
import IPython
In [ ]:
from revChatGPT.V1 import Chatbot, configure
bot = Chatbot(
config = configure(),
conversation_id = 'fdf52ab3-f8c1-450f-86f2-5a25d43fc48d',
lazy_loading = True
)
In [ ]:
for response in bot.ask('''
给出一个用 NodeJS 以 HTTP 协议 访问某个 URL 的 Demo
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
用 NodeJS 实现 curl
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
在 NodeJS 中,`console.log` 和 `process.stdout.write` 有什么区别?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
在 NodeJS 中,有哪几种类型的字符串?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
Run by IJavaScript
In [ ]:
console.log('Hello, world!');
In [ ]:
process.stdout.write('Hello, world!');
Out[ ]:
In [ ]:
const http = require('http');
const url = 'http://127.0.0.1:5000/与主同在';
In [ ]:
var answer = '';
http.get(url, (res) => {
console.log(`Got response: ${res.statusCode}`);
res.on('data', (chunk) => {
console.log(`Received data: ${chunk}`);
answer = `${chunk}`;
});
res.on('end', () => {
console.log('---');
});
}).on('error', (err) => {
console.error(`Got error: ${err.message}`);
});
console.log('Sent request.');
In [ ]:
answer
Out[ ]:
Install by Z shell
In [ ]:
npm install 'whatsapp-web.js'
In [ ]:
npm install qrcode-terminal
Run by IJavaScript
In [ ]:
const http = require('http');
const querystring = require('querystring');
const qrcode = require('qrcode-terminal');
const { Client } = require('whatsapp-web.js');
const client = new Client();
client.on('qr', (qr) => {
// Generate and scan this code with your phone
qrcode.generate(qr, {small: true});
});
client.on('ready', () => {
console.log('Client is ready!');
console.log('---');
});
client.on('message', msg => {
const prompt = querystring.escape(msg.body);
const url = `http://127.0.0.1:5000/${prompt}`;
http.get(url, (res) => {
console.log(`Got response: ${res.statusCode}`);
res.on('data', (chunk) => {
console.log(`Received data: ${chunk}`);
if (res.statusCode == 200) {
msg.reply(`${chunk}`);
}
});
res.on('end', () => {
console.log('---');
});
}).on('error', (err) => {
console.error(`Got error: ${err.message}`);
});
console.log('Sent request.');
});
client.initialize();
Out[ ]:
NodeJS 中保持 WhatsApp 的 auth。 重启免扫 QR 。
- 分割 prompt 、合并 answer 。
- 给每个聊天分配独立的 conversion id 。
- 在消息最开始处加上 "{username}: " 。
OpenAI Whisper
- How to use Bacalhau and OpenAI Whisper to transcribe your video and audio files?
- I'd like to use Kubernetes instead of Bacalhau, because of:
- 「隐私保密」「存储托管」「去中心化」三者之间的不可能三角。
- openapi-whisper.py
- I'd like to use Kubernetes instead of Bacalhau, because of:
Mind
In [ ]:
import json, os
In [ ]:
# open the JSON file and read the conversation_id
with open(os.path.expanduser('~/.config/revChatGPT/config.json'), 'r') as f:
conversation_id = json.load(f).get('conversation_id', None)
In [ ]:
from revChatGPT.V1 import Chatbot, configure
bot = Chatbot(
config = configure(),
conversation_id = conversation_id,
lazy_loading = True
)
In [ ]:
import IPython
In [ ]:
for response in bot.ask('''
给出一个 Flask 的 Demo
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
开发一个 Flask 应用,解析来访的 URL 地址,将来访 URL 地址的字符串返回给客户端。
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
for response in bot.ask('''
怎么设置 flask app 串行处理请求?
'''):
IPython.display.display(IPython.core.display.Markdown(response['message']))
IPython.display.clear_output(wait=True)
In [ ]:
!zsh -c 'pip install flask'
In [ ]:
import json, os
In [ ]:
# open the JSON file and read the conversation_id
with open(os.path.expanduser('~/.config/revChatGPT/config.json'), 'r') as f:
conversation_id = json.load(f).get('conversation_id', None)
In [ ]:
from flask import Flask, request
from revChatGPT.V1 import Chatbot, configure
app = Flask(__name__)
bot = Chatbot(
config = configure(),
conversation_id = conversation_id,
lazy_loading = True
)
@app.route('/<string:prompt>')
def answer(prompt):
for response in bot.ask(prompt):
pass
return response['message']
if __name__ == '__main__':
app.run(threaded=False)
In [ ]:
from flask import Flask, request
from ipymock.browser import start_conversation as ask
In [ ]:
app = Flask(__name__)
@app.route('/')
def answer():
prompt = request.args.get('q')
for response in ask(prompt):
pass
return response
In [ ]:
if __name__ == '__main__':
app.run(threaded=False)