該項目已存檔。由於個人狀況,我沒有時間維護這個倉庫。
新版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 ))
這個項目的存在要感謝所有做出貢獻的人。