The reverse engineering the chat feature of the new version of Bing
If you have any problem watch bottom Q&A first.
python3 -m pip install re_edge_gpt --upgrade
!!! POSSIBLY NOT REQUIRED ANYMORE !!!
In some regions, Microsoft has made the chat feature available to everyone, so you might be able to skip this step. You can check this with a browser (with user-agent set to reflect Edge), by trying to start a chat without logging in.
It was also found that it might depend on your IP address. For example, if you try to access the chat features from an IP that is known to belong to a datacenter range (vServers, root servers, VPN, common proxies, ...), you might be required to log in while being able to access the features just fine from your home IP address.
If you receive the following error, you can try providing a cookie and see if it works then:
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
). You can do this easily with an extension like "User-Agent Switcher and Manager" for Chrome and Firefox.bing_cookies.json
.
bing_cookies.json
, so that they could be recognized by internal cookie processing mechanisms</details> <summary>import json from re_edge_gpt import Chatbot async def create_bot(): cookies = json.loads(open("./path/to/bing_cookies.json", encoding="utf-8").read()) bot = await Chatbot.create(cookies=cookies) return bot
$ python3 -m re_edge_gpt -h
ReEdgeGPT - A demo of reverse engineering the Bing GPT chatbot
!help for help
Type !exit to exit
usage: __main__.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)
(China/US/UK/Norway has enhanced support for locale)
Chatbot
class and asyncio
for more granular controlUse Async for the best experience, for example:
</details> <summary>import asyncio import json from pathlib import Path from re_edge_gpt import Chatbot from re_edge_gpt import ConversationStyle # If you are using jupyter pls install this package # from nest_asyncio import apply async def test_ask() -> None: bot = None try: cookies = json.loads(open( str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read()) bot = await Chatbot.create(cookies=cookies) response = await bot.ask( prompt="How to boil the egg", conversation_style=ConversationStyle.balanced, simplify_response=True ) # If you are using non ascii char you need set ensure_ascii=False print(json.dumps(response, indent=2, ensure_ascii=False)) # Raw response # print(response) assert response except Exception as error: raise error finally: if bot is not None: await bot.close() if __name__ == "__main__": # If you are using jupyter pls use nest_asyncio apply() # apply() try: loop = asyncio.get_running_loop() except RuntimeError: loop = asyncio.get_event_loop() loop.run_until_complete(test_ask())
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
). You can do this easily with an extension like "User-Agent Switcher and Manager" for Chrome and Firefox.copilot_cookies.json
.
copilot_cookies.json
, so that they could be recognized by internal cookie processing mechanismsimport asyncio import json from pathlib import Path from re_edge_gpt import Chatbot from re_edge_gpt import ConversationStyle # If you are using jupyter pls install this package # from nest_asyncio import apply async def test_ask() -> None: bot = None try: mode = "Copilot" if mode == "Bing": cookies: list[dict] = json.loads(open( str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read()) else: cookies: list[dict] = json.loads(open( str(Path(str(Path.cwd()) + "/copilot_cookies.json")), encoding="utf-8").read()) # Notice when mode != "Bing" (Bing is default) will set mode is copilot bot = await Chatbot.create(cookies=cookies, mode=mode) response = await bot.ask( prompt="Is your name Copilot", conversation_style=ConversationStyle.balanced, simplify_response=True ) # If you are using non ascii char you need set ensure_ascii=False print(json.dumps(response, indent=2, ensure_ascii=False)) # Raw response # print(response) assert response except Exception as error: raise error finally: if bot is not None: await bot.close() if __name__ == "__main__": # If you are using jupyter pls use nest_asyncio apply() # apply() try: loop = asyncio.get_running_loop() except RuntimeError: loop = asyncio.get_event_loop() loop.run_until_complete(test_ask())
Chromium based browsers (Edge, Opera, Vivaldi, Brave)
- Go to https://bing.com/
- F12 to open console
- In the JavaScript console, type cookieStore.get("_U").then(result => console.log(result.value)) and press enter
- Copy the output. This is used in --U or auth_cookie.
Firefox
- Go to https://bing.com/.
- F12 to open developer tools
- navigate to the storage tab
- expand the cookies tab
- click on the https://bing.com cookie
- copy the value from the _U
</details> <summary>import os import shutil from pathlib import Path from re_edge_gpt import ImageGen, ImageGenAsync # create a temporary output directory for testing purposes test_output_dir = "test_output" # download a test image test_image_url = "https://picsum.photos/200" auth_cooker = open("bing_cookies.txt", "r+").read() sync_gen = ImageGen(auth_cookie=auth_cooker) async_gen = ImageGenAsync(auth_cookie=auth_cooker) def test_save_images_sync(): sync_gen.save_images([test_image_url], test_output_dir) sync_gen.save_images([test_image_url], test_output_dir, file_name="test_image") # check if the image was downloaded and saved correctly assert os.path.exists(os.path.join(test_output_dir, "test_image_0.jpeg")) assert os.path.exists(os.path.join(test_output_dir, "0.jpeg")) # Generate image list sync def test_generate_image_sync(): image_list = sync_gen.get_images("tree") print(image_list) if __name__ == "__main__": # Make dir to save image Path("test_output").mkdir(exist_ok=True) # Save image test_save_images_sync() # Generate image sync test_generate_image_sync() # Remove dir shutil.rmtree(test_output_dir)
https://github.com/Integration-Automation/ReEdgeGPT/issues/119
Example:
import asyncio import json from pathlib import Path from random import choice from string import ascii_uppercase from re_edge_gpt import Chatbot from re_edge_gpt import ConversationStyle # If you are using jupyter pls install this package # from nest_asyncio import apply async def test_ask() -> None: bot = None try: cookies: list[dict] = json.loads(open( str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read()) bot = await Chatbot.create(cookies=cookies, mode="Bing", plugin_ids=["c310c353-b9f0-4d76-ab0d-1dd5e979cf68"]) prompt = """Rome (Italian and Latin: Roma, Italian: [ˈroːma] ⓘ) is the capital city of Italy. It is also the capital of the Lazio region, the centre of the Metropolitan City of Rome Capital, and a special comune (municipality) named Comune di Roma Capitale. With 2,860,009 residents in 1,285 km2 (496.1 sq mi),[2] Rome is the country's most populated comune and the third most populous city in the European Union by population within city limits. The Metropolitan City of Rome, with a population of 4,355,725 residents, is the most populous metropolitan city in Italy.[3] Its metropolitan area is the third-most populous within Italy.[5] Rome is located in the central-western portion of the Italian Peninsula, within Lazio (Latium), along the shores of the Tiber. Vatican City (the smallest country in the world)[6] is an independent country inside the city boundaries of Rome, the only existing example of a country within a city. Rome is often referred to as the City of Seven Hills due to its geographic location, and also as the "Eternal City". Rome is generally considered to be the cradle of Western civilization and Western Christian culture, and the centre of the Catholic Church.[7][8][9] Rome's history spans 28 centuries. While Roman mythology dates the founding of Rome at around 753 BC, the site has been inhabited for much longer, making it a major human settlement for almost three millennia and one of the oldest continuously occupied cities in Europe.[10] The city's early population originated from a mix of Latins, Etruscans, and Sabines. Eventually, the city successively became the capital of the Roman Kingdom, the Roman Republic and the Roman Empire, and is regarded by many as the first-ever Imperial city and metropolis.[11] It was first called The Eternal City (Latin: Urbs Aeterna; Italian: La Città Eterna) by the Roman poet Tibullus in the 1st century BC, and the expression was also taken up by Ovid, Virgil, and Livy.[12][13] Rome is also called "Caput Mundi" (Capital of the World). After the fall of the Empire in the west, which marked the beginning of the Middle Ages, Rome slowly fell under the political control of the Papacy, and in the 8th century, it became the capital of the Papal States, which lasted until 1870. Beginning with the Renaissance, almost all popes since Nicholas V (1447–1455) pursued a coherent architectural and urban programme over four hundred years, aimed at making the city the artistic and cultural centre of the world.[14] In this way, Rome first became one of the major centres of the Renaissance[15] and then became the birthplace of both the Baroque style and Neoclassicism. Famous artists, painters, sculptors, and architects made Rome the centre of their activity, creating masterpieces throughout the city. In 1871, Rome became the capital of the Kingdom of Italy, which, in 1946, became the Italian Republic. In 2019, Rome was the 14th most visited city in the world, with 8.6 million tourists, the third most visited city in the European Union, and the most popular tourist destination in Italy.[16] Its historic centre is listed by UNESCO as a World Heritage Site.[17] The host city for the 1960 Summer Olympics, Rome is also the seat of several specialised agencies of the United Nations, such as the Food and Agriculture Organization (FAO), the World Food Programme (WFP) and the International Fund for Agricultural Development (IFAD). The city also hosts the Secretariat of the Parliamentary Assembly of the Union for the Mediterranean[18] (UfM) as well as the headquarters of many multinational companies, such as Eni, Enel, TIM, Leonardo, and banks such as BNL. Numerous companies are based within Rome's EUR business district, such as the luxury fashion house Fendi located in the Palazzo della Civiltà Italiana. The presence of renowned international brands in the city has made Rome an important centre of fashion and design, and the Cinecittà Studios have been the set of many Academy Award–winning movies.[19] Name and symbol Etymology According to the Ancient Romans' founding myth,[20] the name Roma came from the city's founder and first king, Romulus.[1] However, it is possible that the name Romulus was actually derived from Rome itself.[21] As early as the 4th century, there have been alternative theories proposed on the origin of the name Roma. Several hypotheses have been advanced focusing on its linguistic roots which however remain uncertain:[22] From Rumon or Rumen, archaic name of the Tiber,