Skip to main content

Microsoft's Bing Chat AI

Project description

ReEdgeGPT

The reverse engineering the chat feature of the new version of Bing

Documentation Status

ReEdgeGPT Doc Click Here!

If you have any problem watch bottom Q&A first.

Setup

Install package

python3 -m pip install re_edge_gpt --upgrade
  • Notice! EdgeGPT is rename to re_edge_gpt

Requirements

  • python 3.9+
  • A Microsoft Account with access to https://bing.com/chat (Optional, depending on your region)
  • Required in a supported country or region with New Bing (Chinese mainland VPN required)

Authentication

!!! 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.

Collect cookies

  • a) (Easy) Install the latest version of Microsoft Edge
  • 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 and Firefox.
  1. Get a browser that looks like Microsoft Edge.
  2. Open bing.com/chat
  3. If you see a chat feature, you are good to continue...
  4. Install the cookie editor extension for Chrome or Firefox
  5. Go to bing.com
  6. Open the extension
  7. Click "Export" on the bottom right, then "Export as JSON" (This saves your cookies to clipboard)
  8. Paste your cookies into a file bing_cookies.json.
    • NOTE: The cookies file name MUST follow the regex pattern bing_cookies.json, so that they could be recognized by internal cookie processing mechanisms

Use cookies in code:

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

How to use Chatbot

Run from Command Line

 $ python3 -m EdgeGPT.EdgeGPT -h

        re_edge_gpt - A demo of reverse engineering the Bing GPT chatbot

        !help for help

        Type !exit to exit

usage: re_edge_gpt.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)

Run in Python

1. The Chatbot class and asyncio for more granular control

Use Async for the best experience, for example:

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())

How to generate image

Getting authentication

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
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)

Q&A


Origin repo (archived): https://github.com/acheong08/EdgeGPT


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

re_edge_gpt-0.0.16.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

re_edge_gpt-0.0.16-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file re_edge_gpt-0.0.16.tar.gz.

File metadata

  • Download URL: re_edge_gpt-0.0.16.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for re_edge_gpt-0.0.16.tar.gz
Algorithm Hash digest
SHA256 27820aeb1fc0e265b112f42ebe60fd5234e160d682899802a8c3eb0fd155f387
MD5 803eb6823f62475c23f1160854a2e79a
BLAKE2b-256 a58737946b94d8f9f0c05b9492bc62addc69c1243a0229c07606cde8f3afd42a

See more details on using hashes here.

File details

Details for the file re_edge_gpt-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: re_edge_gpt-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for re_edge_gpt-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 e41a72e5dd88a33f9cfa328c95f5986f86a2d62f634566ead51d265478bc0c93
MD5 5357e18c2b75e6903921f3b3fa8e42d0
BLAKE2b-256 3227deedab59b714f2ca85ef3c5f625647cb71c4c7fc865a56a90e278ffc6c92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page