|
| 1 | +# ReEdgeGPT |
| 2 | + |
| 3 | +_The reverse engineering the chat feature of the new version of Bing_ |
| 4 | + |
| 5 | +<summary> |
| 6 | + |
| 7 | +# Setup |
| 8 | + |
| 9 | +</summary> |
| 10 | + |
| 11 | +<details open> |
| 12 | + |
| 13 | +## Install package |
| 14 | + |
| 15 | +```bash |
| 16 | +python3 -m pip install re_edge_gpt --upgrade |
| 17 | +``` |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- python 3.8+ |
| 22 | +- A Microsoft Account with access to <https://bing.com/chat> (Optional, depending on your region) |
| 23 | +- Required in a supported country or region with New Bing (Chinese mainland VPN required) |
| 24 | + |
| 25 | +## Authentication |
| 26 | + |
| 27 | +!!! POSSIBLY NOT REQUIRED ANYMORE !!! |
| 28 | + |
| 29 | +**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**. |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +If you receive the following error, you can try **providing a cookie** and see if it works then: |
| 34 | + |
| 35 | +`Exception: Authentication failed. You have not been accepted into the beta.` |
| 36 | + |
| 37 | +### Collect cookies |
| 38 | + |
| 39 | +- a) (Easy) Install the latest version of Microsoft Edge |
| 40 | +- b) (Advanced) Alternatively, you can use any browser and set the user-agent to look like you're using Edge (e.g., `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](https://chrome.google.com/webstore/detail/user-agent-switcher-and-m/bhchdcejhohfmigjafbampogmaanbfkg) and [Firefox](https://addons.mozilla.org/en-US/firefox/addon/user-agent-string-switcher/). |
| 41 | + |
| 42 | +1. Get a browser that looks like Microsoft Edge. |
| 43 | +2. Open [bing.com/chat](https://bing.com/chat) |
| 44 | +3. If you see a chat feature, you are good to continue... |
| 45 | +4. Install the cookie editor extension for [Chrome](https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm) or [Firefox](https://addons.mozilla.org/en-US/firefox/addon/cookie-editor/) |
| 46 | +5. Go to [bing.com](https://bing.com) |
| 47 | +6. Open the extension |
| 48 | +7. Click "Export" on the bottom right, then "Export as JSON" (This saves your cookies to clipboard) |
| 49 | +8. Paste your cookies into a file `bing_cookies_*.json`. |
| 50 | + - NOTE: The **cookies file name MUST follow the regex pattern `bing_cookies_*.json`**, so that they could be recognized by internal cookie processing mechanisms |
| 51 | + |
| 52 | +### Use cookies in code: |
| 53 | + |
| 54 | +```python |
| 55 | +import json |
| 56 | +from re_edge_gpt import Chatbot |
| 57 | + |
| 58 | +cookies = json.loads(open("./path/to/bing_cookies.json", encoding="utf-8").read()) # might omit cookies option |
| 59 | +bot = await Chatbot.create(cookies=cookies) |
| 60 | +``` |
| 61 | + |
| 62 | +</details> |
| 63 | + |
| 64 | +<summary> |
| 65 | + |
| 66 | +# How to use Chatbot |
| 67 | + |
| 68 | +</summary> |
| 69 | + |
| 70 | +<details open> |
| 71 | + |
| 72 | +## Run from Command Line |
| 73 | + |
| 74 | +``` |
| 75 | + $ python3 -m EdgeGPT.EdgeGPT -h |
| 76 | +
|
| 77 | + EdgeGPT - A demo of reverse engineering the Bing GPT chatbot |
| 78 | + Repo: github.com/acheong08/EdgeGPT |
| 79 | + By: Antonio Cheong |
| 80 | +
|
| 81 | + !help for help |
| 82 | +
|
| 83 | + Type !exit to exit |
| 84 | +
|
| 85 | +usage: EdgeGPT.py [-h] [--enter-once] [--search-result] [--no-stream] [--rich] [--proxy PROXY] [--wss-link WSS_LINK] |
| 86 | + [--style {creative,balanced,precise}] [--prompt PROMPT] [--cookie-file COOKIE_FILE] |
| 87 | + [--history-file HISTORY_FILE] [--locale LOCALE] |
| 88 | +
|
| 89 | +options: |
| 90 | + -h, --help show this help message and exit |
| 91 | + --enter-once |
| 92 | + --search-result |
| 93 | + --no-stream |
| 94 | + --rich |
| 95 | + --proxy PROXY Proxy URL (e.g. socks5://127.0.0.1:1080) |
| 96 | + --wss-link WSS_LINK WSS URL(e.g. wss://sydney.bing.com/sydney/ChatHub) |
| 97 | + --style {creative,balanced,precise} |
| 98 | + --prompt PROMPT prompt to start with |
| 99 | + --cookie-file COOKIE_FILE |
| 100 | + path to cookie file |
| 101 | + --history-file HISTORY_FILE |
| 102 | + path to history file |
| 103 | + --locale LOCALE your locale (e.g. en-US, zh-CN, en-IE, en-GB) |
| 104 | +``` |
| 105 | + |
| 106 | +(China/US/UK/Norway has enhanced support for locale) |
| 107 | + |
| 108 | +## Run in Python |
| 109 | + |
| 110 | +### 1. The `Chatbot` class and `asyncio` for more granular control |
| 111 | + |
| 112 | +Use Async for the best experience, for example: |
| 113 | + |
| 114 | +```python |
| 115 | +import asyncio, json |
| 116 | + |
| 117 | +from pathlib import Path |
| 118 | +from re_edge_gpt import Chatbot, ConversationStyle |
| 119 | + |
| 120 | +cookies = json.loads(open(str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read()) |
| 121 | + |
| 122 | +async def main(): |
| 123 | + bot = await Chatbot.create(cookies=cookies) # Passing cookies is "optional", as explained above |
| 124 | + response = await bot.ask(prompt="Hello world", conversation_style=ConversationStyle.creative, simplify_response=True) |
| 125 | + print(json.dumps(response, indent=2)) # Returns |
| 126 | + """ |
| 127 | +{ |
| 128 | + "text": str, |
| 129 | + "author": str, |
| 130 | + "sources": list[dict], |
| 131 | + "sources_text": str, |
| 132 | + "suggestions": list[str], |
| 133 | + "messages_left": int |
| 134 | +} |
| 135 | + """ |
| 136 | + await bot.close() |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + asyncio.run(main()) |
| 140 | +``` |
| 141 | + |
| 142 | +</details> |
0 commit comments