xingty / bingai2openai

An adapter to convert bing's api to openai's
23 stars 4 forks source link

This is an adapter layer to convert Bing's API to openai's(Websocket to SSE). Using this project, you can use BingAI in any WebUI that supports ChatGPT's API.

Requirements

Collect cookies

Setup

  1. Place 'cookies.json' in the same directory as the file 'flight.py'
  2. Set proxy in 'env.json' if necessary
  3. Run
    pip3 install -r requirements.txt
    python3 flight.py

Instructions

Some features such as search and gpt4-turbo are disabled by default. They can be enabled temporarily following the instructions below.

#enable_search

If you want to enable search for Bing, you can include this instruction in the content of your question, for examble:

Who is the winner of world cup 2022? #enable_search

hallucination hallucination

correct answer

#enable_gpt4_turbo

This insctuction will enable gpt4-turbo for Bing.

Environment

{
  "proxy": "the address of your proxy server(http or socks5)",
  "bind": "bind ip address",
  "port": 5000,
  "api_key": "api key",
  "search": false
}

Using with NextChat

FAQ

CAPTCHA or Authentication failed

  1. Check if the cookies have expired. If so, re-importing them.
  2. After making sure the cookies are valid, open Bing Web in your browser and sending a random message. You should see a CAPTCHA challenge. If not, verify that the current user matches the cookies.json file. Complete the CAPTCHA and go back to the software. It should work fine now.

API

[POST] /v1/chat/completions

Content-Type: application/json

Body:

{
  "model": "creative|precise|balanced|creative_turbo|precise_turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello!"
    }
  ]
}

Response:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "logprobs": null,
    "finish_reason": "stop"
  }]
}

[GET] /v1/models

Response:

{
  "object": "list",
  "data": [
    {
      "id": "gpt-4",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    },
    {
      "id": "creative",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    },
    {
      "id": "precise",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    },
    {
      "id": "creative_turbo",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    },
    {
      "id": "precise_turbo",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    },    
    {
      "id": "balanced",
      "object": "model",
      "created": 1686935002,
      "owned_by": "organization-owner"
    }
  ]
}