如出现报错,请先将EdgeGPT库更新到0.1.10.1+,还不能解决再提交issue.
Bing Chat服务端,通过WebSocket/API实现通讯.
- 已通过候补名单的New Bing账户
- 语言: Python3.8+.
- 包: fastapi, uvicorn, asyncio, python-multipart, EdgeGPT.
- 软件: docker, docker-compose
- 浏览器安装Cookie-Editor扩展.
- 在https://www.bing.com/chat页面中点击扩展.
- 点击扩展右下角的Export,将复制的内容粘贴到cookie.json.
- (可选: 配置地址和端口,在bing_chat.py第15-16行) 运行bing_chat.py.
- 直接从文件系统中打开example中三个web窗口之一
- 打开.env文件,默认值为srv_port=5000(服务端端口) cli_port=80(客户端端口),按需修改,避免和现有端口冲突
- 在代码根目录执行 docker-compose up -d
- 从局域网其他设备访问客户端,即 host_ip:cli_port
web.html web2.html web3/html/index.html
名称 | 必填 | 中文名 | 说明 |
---|---|---|---|
token | 否 | 令牌 | 当请求API时,填则为连续对话,不填则为新对话,值可在响应中获取 |
style | 是 | 风格 | balanced代表平衡,creative代表创造,precise代表精确 |
question | 是 | 问题 |
注:WebSocket发送需JSON格式.
名称 | 中文名 | 说明 |
---|---|---|
code | 状态码 | |
message | 消息 | |
data | 数据 | |
answer | 回答 | |
urls | 链接 | |
done | 完成 | 部分传输是否完成,当为流传输时存在 |
reset | 重置 | 下次对话是否被重置(code为500时也会被重置) |
token | 令牌 | 用于连续对话,当请求API时存在 |
等待Bing Chat响应完后返回.
连接/ws.
{"code": 200, "message": "success", "data": {"answer": "您好,这是必应。", "urls":[{"title": "The New Bing - Learn More", "url": "https://www.bing.com/new"}], "reset": false}}
- 请求方式: GET/POST.
- 请求地址: /api.
{"code": 200, "message": "success", "data": {"answer": "您好,这是必应。", "urls":[{"title": "The New Bing - Learn More", "url": "https://www.bing.com/new"}], "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
一部分一部分返回.
当部分传输完成时,将会返回整体,url才会有值,done改为true,reset显示为真实值(部分传输过程中无论下次是否被重置都显示为false).
WebSocket连接/ws_stream.
{"code": 200, "message": "success", "data": {"answer": "您。", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "好", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": ",", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "这。", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "是", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "必应", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "。", "urls": [], "done": false, "reset": false}}
{"code": 200, "message": "success", "data": {"answer": "您好,这是必应。", "urls": [{"title": "The New Bing - Learn More", "url": "https://www.bing.com/new"}], "done": true, "reset": false}}
- 请求方式: GET/POST.
- 请求地址: /api_stream.
{"code": 200, "message": "success", "data": {"answer": "您。", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "好", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": ",", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "这。", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "是", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "必应", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "。", "urls": [], "done": false, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
{"code": 200, "message": "success", "data": {"answer": "您好,这是必应。", "urls": [{"title": "The New Bing - Learn More", "url": "https://www.bing.com/new"}], "done": true, "reset": false, "token": "7953d67b-eac2-457e-a2ee-fedc8ba53599"}}
- 页面写的有点丑,有能力的大神,可以pull request一下,如果你有的example也可以提交.
- 搭建好建议不要对外开放,因为目前Bing Chat24小时内有次数限制.
- 至于反应快慢的问题,要看回答文本的长度,如果文本长度过长,回复时间会比较长.
- 关于整体传输和流传输,整体传输由于要等待Bing完全响应才能开始传输,所以时间要久一点。流传输会先返回一部分,所以看起来比较快,但其实最终的完成时间都是一样的.
- 连续对话问题:websocket是默认支持连续对话的。对于API来说,如果需要进行连续对话,首先需要在第一次请求时获取token,然后在后续请求中带上token,就可以实现连续对话了.