本文翻译整理自:https://github.com/acheong08/EdgeGPT
文章目录
项目已归档 :由于个人原因,作者无法继续维护此代码库。
一、关于 EdgeGPT
逆向工程实现新版Bing的聊天功能
相关链接资源
关键功能特性
- 通过逆向工程实现Bing聊天功能
- 支持命令行和Python API两种调用方式
- 提供图像生成功能
- 支持多种对话风格(creative/balanced/precise)
- 支持cookie认证
二、安装
系统要求
- Python 3.8+
- 可访问https://bing.com/chat的微软账户(视地区而定)
- 位于支持New Bing的国家/地区(中国大陆需VPN)
- Selenium(用于自动设置cookie)
安装命令
python3 -m pip install EdgeGPT --upgrade
三、认证配置
某些地区可能无需认证即可使用聊天功能。您可以通过浏览器(用户代理设置为Edge)尝试在不登录的情况下发起聊天来验证。
如果收到以下错误,可以尝试提供cookie:
Exception: Authentication failed. You have not been accepted into the beta.
获取Cookie步骤
1、使用类似Microsoft Edge的浏览器:
- a) 安装最新版Microsoft Edge
- b) 或设置用户代理为Edge样式
2、访问bing.com/chat
3、安装Cookie编辑器扩展(Chrome版或Firefox版)
4、访问bing.com
5、打开扩展程序
6、点击"Export"→"Export as JSON"
7、将cookie保存为bing_cookies_*.json
文件
代码中使用Cookie
cookies = json.loads(open("./path/to/cookies.json", encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies)
四、使用方法
1、命令行方式
$ python3 -m EdgeGPT.EdgeGPT -h
EdgeGPT - A demo of reverse engineering the Bing GPT chatbot
Repo: github.com/acheong08/EdgeGPT
By: Antonio Cheong
!help for help
Type !exit to exit
usage: EdgeGPT.py [-h] [--enter-once] [--search-result] [--no-stream] [--rich] [--proxy PROXY] [--wss-link WSS_LINK]
[--style {creative,balanced,precise}] [--prompt PROMPT] [--cookie-file COOKIE_FILE]
[--history-file HISTORY_FILE] [--locale LOCALE]
options:
-h, --help show this help message and exit
--enter-once
--search-result
--no-stream
--rich
--proxy PROXY Proxy URL (e.g. socks5://127.0.0.1:1080)
--wss-link WSS_LINK WSS URL(e.g. wss://sydney.bing.com/sydney/ChatHub)
--style {creative,balanced,precise}
--prompt PROMPT prompt to start with
--cookie-file COOKIE_FILE
path to cookie file
--history-file HISTORY_FILE
path to history file
--locale LOCALE your locale (e.g. en-US, zh-CN, en-IE, en-GB)
支持参数:
--style
:设置对话风格(creative/balanced/precise)--cookie-file
:指定cookie文件路径--locale
:设置地区(如en-US/zh-CN)
2、Python API方式
使用Chatbot
类
import asyncio, json
from EdgeGPT.EdgeGPT import Chatbot, ConversationStyle
async def main():
bot = await Chatbot.create()
response = await bot.ask(prompt="Hello world", conversation_style=ConversationStyle.creative)
print(json.dumps(response, indent=2))
await bot.close()
asyncio.run(main())
使用Query
辅助类
from EdgeGPT.EdgeUtils import Query
q = Query("What are you? Give your answer as Python code")
print(q.output) # 获取文本输出
print(q.sources) # 获取来源
print(q.suggestions) # 获取建议
3、Docker方式运行
docker run --rm -it -v $(pwd)/cookies.json:/cookies.json:ro -e COOKIE_FILE='/cookies.json' ghcr.io/acheong08/edgegpt
五、图像生成功能
命令行方式
$ python3 -m ImageGen.ImageGen -h
Python API方式
from EdgeGPT.EdgeUtils import ImageQuery
q = ImageQuery("Meerkats at a garden party in Devon")
六、高级控制
使用ImageGen
类
from EdgeGPT.ImageGen import ImageGen
import json
with open("cookies.json", encoding="utf-8") as file:
cookies = json.load(file)
_U = next(cookie["value"] for cookie in cookies if cookie["name"] == "_U")
image_generator = ImageGen(_U)
images = image_generator.get_images("Astronaut riding a horse")
image_generator.save_images(images)
伊织 xAI 2025-04-19(六)