Update README and Fix code

This commit is contained in:
linghaihui 2023-03-27 10:51:11 +08:00
parent 9b98e60870
commit 47f2694e07
7 changed files with 73 additions and 50 deletions

View File

@ -1,3 +1,5 @@
# New Bing Bot # New Bing 微信小程序
接口来自[new-bing](../new-bing)项目
<image style="width: 320px" src="./snapshoot.png"/> <image style="width: 320px" src="./snapshoot.png"/>

View File

@ -319,7 +319,7 @@ Page({
var robContent = '' var robContent = ''
var num_in_conversation = -1 var num_in_conversation = -1
if (!data['final']) { if (!data['final']) {
robContent = data['data'] robContent = data['data'] + ' ...'
} else { } else {
robContent = that.processData(data['data'], suggests, that.data.lastContent) robContent = that.processData(data['data'], suggests, that.data.lastContent)
num_in_conversation = data['data']['data']['num_in_conversation'] num_in_conversation = data['data']['data']['num_in_conversation']

View File

@ -1,14 +1,23 @@
[登录 bing 账号](https://login.live.com/) 使用[Cookie-Editor 扩展](https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm)将 cookie 以 json 格式导出,保存为`cookie.json`文件,同时将`COOKIE_FILE`环境变量的值指向该文件。 ## New Bing 接口
包括http和websocket两种协议的接口可根据实际情况选用
## 部署 ## 部署
[登录 bing 账号](https://login.live.com/) 使用[Cookie-Editor 扩展](https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm)将 cookie 以 json 格式导出,保存为`cookie.json`文件,同时将`COOKIE_FILE`环境变量的值指向该文件。
``` ```
1.新建cookies目录将cookie.json放入该目录支持3个cookie可分别命名为cookie.json,cookie1.json,cookie2.json 1.git clone https://github.com/bujnlc8/gptbing
2.cp env.example env # 根据实际情况修改 2.cd gptbing/new-bing
3.bash start.sh 0.0.1 3.mkdir cookies && mv cookie.json cookies # 支持3个cookie可分别命名为cookie.json,cookie1.json,cookie2.json
4.curl -X POST 'http://127.0.0.1:8000/chat' -H 'content-type: application/json' --data '{"q":"你是谁?","t":1,"sid":"1"}' 验证是否成功 4.cp env.example env # 根据实际情况修改
5.bash start.sh 0.0.3
6.curl -X POST 'http://127.0.0.1:8000/chat' -H 'content-type: application/json' --data '{"q":"你是谁?","t":1,"sid":"1"}' # 验证是否成功
``` ```
**⚠️ 目前中国大陆的IP会返回404自备代理** 见[https://github.com/acheong08/EdgeGPT/issues/178](https://github.com/acheong08/EdgeGPT/issues/178) ## 说明
`EdgeGPT.py`文件 fork 至[https://github.com/acheong08/EdgeGPT](https://github.com/acheong08/EdgeGPT),并做了些许修改,在此表示感谢! `EdgeGPT.py`文件 fork 至[https://github.com/acheong08/EdgeGPT](https://github.com/acheong08/EdgeGPT),并做了些许修改,在此表示感谢!
**⚠️ 目前中国大陆的IP会返回404自备代理** 见[https://github.com/acheong08/EdgeGPT/issues/178](https://github.com/acheong08/EdgeGPT/issues/178)

View File

@ -49,14 +49,27 @@ def reset_cookie():
LOCK.release() LOCK.release()
def make_response_data(status, text, suggests, message, num_in_conversation=-1):
return {
'data': {
'status': status,
'text': text,
'suggests': suggests,
'message': message,
'num_in_conversation': num_in_conversation,
},
'cookie': os.environ.get('COOKIE_FILE'),
}
@app.websocket('/chat') @app.websocket('/chat')
async def ws_chat(request, ws): async def ws_chat(_, ws):
while True: while True:
data = raw_json.loads(await ws.recv())
logger.warn('Receive data: %s', data)
sid = data['sid']
q = data['q']
try: try:
data = raw_json.loads(await ws.recv())
logger.warn('Receive data: %s', data)
sid = data['sid']
q = data['q']
async for response in get_bot(sid).ask_stream(q, conversation_style=ConversationStyle.creative): async for response in get_bot(sid).ask_stream(q, conversation_style=ConversationStyle.creative):
final, res = response final, res = response
if final: if final:
@ -76,21 +89,10 @@ async def ws_chat(request, ws):
})) }))
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
await ws.send( await ws.send(raw_json.dumps({
raw_json.dumps({ 'final': True,
'final': True, 'data': make_response_data('Error', str(e), [], str(e))
'data': { }))
'data': {
'status': 'Error',
'text': str(e),
'suggests': [q],
'message': str(e),
'num_in_conversation': -1,
},
'cookie': os.environ.get('COOKIE_FILE'),
}
})
)
def get_bot(sid): def get_bot(sid):
@ -141,17 +143,10 @@ async def process_data(res, q, sid, auto_reset=None):
msg = res['item']['result']['message'] if 'message' in res['item']['result'] else '' msg = res['item']['result']['message'] if 'message' in res['item']['result'] else ''
if auto_reset and ('New topic' in text or 'has expired' in msg): if auto_reset and ('New topic' in text or 'has expired' in msg):
await get_bot(sid).reset() await get_bot(sid).reset()
return { return make_response_data(
'data': { status, text, suggests, msg,
'status': status, res['item']['throttling']['numUserMessagesInConversation'] if 'throttling' in res['item'] else -1
'text': text, )
'suggests': suggests,
'message': msg,
'num_in_conversation': res['item']['throttling']['numUserMessagesInConversation']
if 'throttling' in res['item'] else -1,
},
'cookie': os.environ.get('COOKIE_FILE'),
}
@app.post('/chat') @app.post('/chat')

View File

@ -1,15 +1,16 @@
# wechatbot ## 微信机器人
最近ChatGPT异常火爆想到将其接入到个人微信是件比较有趣的事所以有了这个项目。项目基于[openwechat](https://github.com/eatmoreapple/openwechat)开发支持ChatGPT和New Bing[依赖于new-bing项目](../new-bing)
### 目前实现了以下功能 最近ChatGPT异常火爆想到将其接入到个人微信是件比较有趣的事所以有了这个项目。项目基于[openwechat](https://github.com/eatmoreapple/openwechat)开发支持ChatGPT和New Bing其中New Bing[依赖于new-bing项目](../new-bing)提供的http接口。
## 目前实现了以下功能
+ 群聊@回复 + 群聊@回复
+ 私聊回复 + 私聊回复
+ 自动通过回复 + 自动通过回复
# 注册openai ## 注册openai
ChatGPT注册可以参考[这里](https://juejin.cn/post/7173447848292253704) ChatGPT注册可以参考[这里](https://juejin.cn/post/7173447848292253704)
# 安装使用 ## 部署
``` ```
# 获取项目 # 获取项目
@ -18,14 +19,21 @@ git clone https://github.com/bujnlc8/gptbing
# 进入项目目录 # 进入项目目录
cd gptbing/wechatbot cd gptbing/wechatbot
# 复制配置文件 # 复制配置文件, 改成实际的值,注意去掉#注释
copy config.dev.json config.json copy config.json.example config.json
# 启动项目 # 启动项目
go run main.go go run main.go
或者docker部署
bash start.sh 0.0.2 # 代理地址根据实际情况修改,运行之后`docker logs wechatbot`会打印出登录地址
``` ```
本项目fork至[https://github.com/djun/wechatbot](https://github.com/djun/wechatbot),修改使之支持`ChatGPT`和`New bing`,在此致谢! ## 说明
本项目fork至[https://github.com/djun/wechatbot](https://github.com/djun/wechatbot),修改使之支持`ChatGPT`和`New Bing`,在此致谢!
**⚠️ 有一定的几率导致被微信封号,请谨慎使用,由此导致的封号,本人概不负责** **⚠️ 有一定的几率导致被微信封号,请谨慎使用,由此导致的封号,本人概不负责**

View File

@ -1,5 +1,5 @@
{ {
"api_key": "", "api_key": "", # chatgpt api key
"auto_pass": true, "auto_pass": true, # 是否自动通过好友申请
"bing_chat_url": "" "bing_chat_url": "" # New Bing 聊天接口
} }

9
wechatbot/start.sh Normal file
View File

@ -0,0 +1,9 @@
docker pull yy194131/chatgpt:$1
count=$(docker ps -a | grep wechatbot | wc -l)
if [ $count -gt 0 ]; then
docker stop wechatbot && docker rm wechatbot
fi
# 代理地址改成实际的地址
docker run --name wechatbot -d -e https_proxy=代理地址 -v $(pwd)/config.json:/app/config.json yy194131/chatgpt:$1