该项目已存档。由于个人情况,我没有时间维护这个仓库。
新版Bing聊天功能逆向工程
英语 - 简体中文 - 繁体中文 - 西班牙语 - 日本语
python3 -m pip install EdgeGPT --upgrade
!!!可能不再需要了!
在某些地区,Microsoft 已向所有人提供聊天功能,因此您也许可以跳过此步骤。您可以通过尝试在不登录的情况下开始聊天,使用浏览器(将用户代理设置为反映 Edge)来检查这一点。
我们还发现,这可能取决于您的 IP 地址。例如,如果您尝试从已知属于数据中心范围(虚拟服务器、根服务器、VPN、通用代理等)的 IP 访问聊天功能,则可能需要登录才能从您的家庭 IP 地址即可正常访问这些功能。
如果您收到以下错误,您可以尝试提供 cookie并查看它是否有效:
Exception: Authentication failed. You have not been accepted into the beta.
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51
)。您可以使用 Chrome 和 Firefox 的“用户代理切换器和管理器”等扩展轻松完成此操作。bing_cookies_*.json
中。bing_cookies_*.json
,以便内部 cookie 处理机制可以识别它们 cookies = json . loads ( open ( "./path/to/cookies.json" , encoding = "utf-8" ). read ()) # might omit cookies option
bot = await Chatbot . create ( cookies = cookies )
$ 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)
(中国/美国/英国/挪威加强了对locale的支持)
Chatbot
类和asyncio
用于更精细的控制使用异步以获得最佳体验,例如:
import asyncio , json
from EdgeGPT . EdgeGPT import Chatbot , ConversationStyle
async def main ():
bot = await Chatbot . create () # Passing cookies is "optional", as explained above
response = await bot . ask ( prompt = "Hello world" , conversation_style = ConversationStyle . creative , simplify_response = True )
print ( json . dumps ( response , indent = 2 )) # Returns
"""
{
"text": str,
"author": str,
"sources": list[dict],
"sources_text": str,
"suggestions": list[str],
"messages_left": int
}
"""
await bot . close ()
if __name__ == "__main__" :
asyncio . run ( main ())
Query
和Cookie
辅助类创建一个简单的 Bing Chat AI 查询(默认情况下使用“精确”对话样式)并仅查看主要文本输出,而不是整个 API 响应:
请记住以特定格式存储您的 cookie: bing_cookies_*.json
。
from EdgeGPT . EdgeUtils import Query , Cookie
q = Query ( "What are you? Give your answer as Python code" )
print ( q )
存储 Cookie 文件的默认目录是HOME/bing_cookies
,但您可以使用以下命令更改它:
Cookie . dir_path = Path ( r"..." )
或者更改要使用的对话方式或 cookie 文件:
q = Query (
"What are you? Give your answer as Python code" ,
style = "creative" , # or: 'balanced', 'precise'
cookie_file = "./bing_cookies_alternative.json"
)
# Use `help(Query)` to see other supported parameters.
使用以下属性从响应中快速提取文本输出、代码片段、来源/参考列表或建议的后续问题:
q . output # Also: print(q)
q . sources
q . sources_dict
q . suggestions
q . code
q . code_blocks
q . code_block_formatsgiven )
获取原始提示和您指定的对话风格:
q . prompt
q . ignore_cookies
q . style
q . simplify_response
q . locale
repr ( q )
访问自导入Query
以来所做的先前查询:
Query . index # A list of Query objects; updated dynamically
Query . image_dir_path
最后, Cookie
类支持多个 cookie 文件,因此,如果您使用命名约定bing_cookies_*.json
创建其他 cookie 文件,并且您的查询将自动尝试使用下一个文件(按字母顺序),如果您超出了每日请求配额(目前设置为 200)。
以下是您可以访问的主要属性:
Cookie . current_file_index
Cookie . current_file_path
Cookie . current_data
Cookie . dir_path
Cookie . search_pattern
Cookie . files
Cookie . image_token
Cookie . import_next
Cookie . rotate_cookies
Cookie . ignore_files
Cookie . supplied_files
Cookie . request_count
这假设您当前工作目录中有一个 cookies.json 文件
docker run --rm -it -v $( pwd ) /cookies.json:/cookies.json:ro -e COOKIE_FILE= ' /cookies.json ' ghcr.io/acheong08/edgegpt
您可以添加任何额外的标志,如下所示
docker run --rm -it -v $( pwd ) /cookies.json:/cookies.json:ro -e COOKIE_FILE= ' /cookies.json ' ghcr.io/acheong08/edgegpt --rich --style creative
$ python3 -m ImageGen.ImageGen -h
usage: ImageGen.py [-h] [-U U] [--cookie-file COOKIE_FILE] --prompt PROMPT [--output-dir OUTPUT_DIR] [--quiet] [--asyncio]
optional arguments:
-h, --help show this help message and exit
-U U Auth cookie from browser
--cookie-file COOKIE_FILE
File containing auth cookie
--prompt PROMPT Prompt to generate images for
--output-dir OUTPUT_DIR
Output directory
--quiet Disable pipeline messages
--asyncio Run ImageGen using asyncio
ImageQuery
辅助类根据简单的提示生成图像并下载到当前工作目录:
from EdgeGPT . EdgeUtils import ImageQuery
q = ImageQuery ( "Meerkats at a garden party in Devon" )
更改此会话中所有未来图像的下载目录:
Query.image_dirpath = Path("./to_another_folder")
ImageGen
类和asyncio
用于更精细的控制 from EdgeGPT . ImageGen import ImageGen
import argparse
import json
async def async_image_gen ( args ) -> None :
async with ImageGenAsync ( args . U , args . quiet ) as image_generator :
images = await image_generator . get_images ( args . prompt )
await image_generator . save_images ( images , output_dir = args . output_dir )
if __name__ == "__main__" :
parser = argparse . ArgumentParser ()
parser . add_argument ( "-U" , help = "Auth cookie from browser" , type = str )
parser . add_argument ( "--cookie-file" , help = "File containing auth cookie" , type = str )
parser . add_argument (
"--prompt" ,
help = "Prompt to generate images for" ,
type = str ,
required = True ,
)
parser . add_argument (
"--output-dir" ,
help = "Output directory" ,
type = str ,
default = "./output" ,
)
parser . add_argument (
"--quiet" , help = "Disable pipeline messages" , action = "store_true"
)
parser . add_argument (
"--asyncio" , help = "Run ImageGen using asyncio" , action = "store_true"
)
args = parser . parse_args ()
# Load auth cookie
with open ( args . cookie_file , encoding = "utf-8" ) as file :
cookie_json = json . load ( file )
for cookie in cookie_json :
if cookie . get ( "name" ) == "_U" :
args . U = cookie . get ( "value" )
break
if args . U is None :
raise Exception ( "Could not find auth cookie" )
if not args . asyncio :
# Create image generator
image_generator = ImageGen ( args . U , args . quiet )
image_generator . save_images (
image_generator . get_images ( args . prompt ),
output_dir = args . output_dir ,
)
else :
asyncio . run ( async_image_gen ( args ))
这个项目的存在要感谢所有做出贡献的人。