Skip to main content

Reverse proxy for OpenAI chatgpt website API

Project description

ChatGPT-Proxy

Python version of OpenAI's ChatGPT web API proxy
Python alternative of ChatGPT-Proxy-V4
Use cookie cf_clearance to pass Cloudflare browser check

_puid no longer works

Requirements

  • Access to chat.openai.com

Install

pip install chatgpt-proxy

Usage

For how to get a usable cf_clearance cookie, checkout issue #1 (Chinese only sorry)

Run as a service

Set these environment variables:

  • CF_CLEARANCE: (Optional) Cookie cf_clearance
  • USER_AGENT: (Optional) User-agent of your browser when you get the cookie cf_clearance
  • ACCESS_TOKEN: (Optional) Obtains from here
  • PUID: (Optional) Cookie _puid, still needed to start a conversation for plus account (?) When set a plus account's access_token, this can be automatically refresh
  • PROXY_TRUST_CLIENT: (Optional) Trust requests from any client.
    When set to True, any requests without an access_token will be given the above access_token.
    Default to False, which will only use for refresh puid.
  • HOST: (Optional) Listen on host, default to 127.0.0.1
  • PORT: (Optional) Listen on port, default to 7800
  • MOD_ACCESS_TOKEN: (Optional) Update info like cf_clearance through http request requires you set this access_token in Authorization Header

Or create a .env file with your environment variables at where you want to run the proxy:

cf_clearance=YOUR_CF_CLEARANCE
user_agent=YOUR_USER_AGENT
access_token=YOUR_ACCESS_TOKEN
puid=ANY_VALID_PUID
proxy_trust_client=False
host=127.0.0.1
port=7800
mod_access_token=YOUR_MOD_ACCESS_TOKEN

Note that environment variables will override the values in .env file.

Then run: python -m chatgpt_proxy

Success

If you see this in console: 2023-01-01 00:00:00,000 - chatgpt_proxy.proxy - INFO - puid: user-xxxxxx You are ready to go

The proxy is avaliable at http://host:port/backend-api/

Integrate into your FastAPI app

Check out __main__.py

from chatgpt_proxy import WebChatGPTProxy
proxy = WebChatGPTProxy(cf_clearance=CF_CLEARANCE, user_agent=USER_AGENT, access_token=ACCESS_TOKEN, trust=False)
app = FastAPI()
proxy.attach(app, path="/backend-api")

class WebChatGPTProxy:

  • cf_clearance: Cookie cf_clearance
  • user_agent: User-agent of your browser when you get the cookie cf_clearance
  • access_token: (Optional)
  • puid: (Optional)
  • trust: (Optional) Trust requests from any client. When set to True, any requests without an access_token will be given the above access_token. Default to False, which will only use for refresh puid.

Behind a http proxy

You need to use the same ip address you used to get the cookie cf_clearance

Set HTTP_PROXY and HTTPS_PROXY or ALL_PROXY environment variables.
This cannot be set in .env file becauce httpx (the package we used to send request) reads from environment variables only. See also httpx#Proxies.

Credits

License

This work is licensed under the GNU Affero General Public License v3.0 or later, with the "CHATGPT-PROXY" exception.

"CHATGPT-PROXY" EXCEPTION TO THE AGPL

As a special exception, using this work in the following ways does not cause your program to be covered by the AGPL:

  1. Bundling the unaltered code or binary of this work in your program; or
  2. Interacting with this work through the provided inter-process communication interface, such as the HTTP API.

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

chatgpt-proxy-0.2.5.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

chatgpt_proxy-0.2.5-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file chatgpt-proxy-0.2.5.tar.gz.

File metadata

  • Download URL: chatgpt-proxy-0.2.5.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for chatgpt-proxy-0.2.5.tar.gz
Algorithm Hash digest
SHA256 5a5bbf68acd90690124c226047b04eb2afa4974210c36fca7fb65f39fb6b50f2
MD5 c32e294bfc54e01c1b9629b6005efbff
BLAKE2b-256 42325c28e7b308219d2ddd9626040fa62f58a0ccb39d44a9f077635518f4a4ca

See more details on using hashes here.

File details

Details for the file chatgpt_proxy-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for chatgpt_proxy-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 09429535a399ebb485ad651af66d24ddf3e90e149c7c1b1687aca8b562084fb6
MD5 94337adc9f8799c472452e3b10e33743
BLAKE2b-256 5baa5e12422e07a9a0cffe8f3a78cbc7a1aee74eb83c4d9c2fdbf8356c8bc739

See more details on using hashes here.

Supported by

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