chat2api Documentation

repository·main·Indexed 25 days ago

https://github.com/lanqian528/chat2api

A proxy service that converts ChatGPT web sessions into an OpenAI-compatible API. It supports free GPT-3.5 access and premium models (GPT-4, O1, O3-mini) using AccessTokens or RefreshTokens. Key features include multi-account rotation, a web mirror mode for the official ChatGPT interface, and support for Team accounts. The service is built with FastAPI and can be deployed via Python or Docker.

Tokens
2K
Snippets
3
Records
15
Agent score
86%

What's inside chat2api

  1. Manage and poll multiple Tokens

    main

    You can manage a pool of tokens for multi-account rotation and polling:

    1. Set Authorization Key: Configure the AUTHORIZATION environment variable with one or more authorization codes (comma-separated). This acts as your personal API key.
    2. Upload Tokens: Access the /tokens or /{api_prefix}/tokens endpoint to upload new AccessToken or RefreshToken values, view the current count, or clear the pool.
    3. Requesting: When making API calls, pass the value configured in AUTHORIZATION as the Bearer token in the Authorization header. The system will automatically rotate through the available tokens on request failure.
  2. Install and Deploy CHAT2API

    main

    Choose one of the following deployment methods:

    Direct Deployment (Python):

    git clone https://github.com/LanQian528/chat2api
    cd chat2api
    pip install -r requirements.txt
    python app.py

    Docker Deployment:

    docker run -d \
      --name chat2api \
      -p 5005:5005 \
      lanqian528/chat2api:latest

    Docker Compose (Recommended for PLUS accounts):

    1. Create a directory and enter it.
    2. Download the compose file: wget https://raw.githubusercontent.com/LanQian528/chat2api/main/docker-compose-warp.yml
    3. Edit the file to set your environment variables.
    4. Run: docker-compose up -d
  3. Configure the Official Website Mirror

    main

    To run a mirror of the official ChatGPT web interface:

    1. Enable Gateway: Set the environment variable ENABLE_GATEWAY to true.
    2. Upload Tokens: Use the /tokens management page to upload RefreshToken or AccessToken values.
    3. Access Login: Navigate to the /login page to access the mirror interface.
    4. Direct Login: You can also log in directly using the URL format /?token=xxx, where xxx is a RefreshToken, AccessToken, or SeedToken (random seed).
  4. Run the CHAT2API server with Uvicorn

    main
    The application is built using FastAPI and can be started using uvicorn. By default, it runs on 0.0.0.0:5005. You can also configure it to run with SSL using key.pem and cert.pem files.
  5. Deploy chat2api using Docker Compose

    main

    You can deploy the chat2api service using Docker Compose. The setup includes the main application and a watchtower service to automatically update the container.

    By default, the application maps port 5005 on the host to port 5005 in the container. Data is persisted in a local ./data directory.

    version: '3'
    
    services:
      chat2api:
        image: lanqian528/chat2api:latest
        container_name: chat2api
        restart: unless-stopped
        ports:
          - '5005:5005'
        volumes:
          - ./data:/app/data
        environment:
          - TZ=Asia/Shanghai
          - AUTHORIZATION=sk-xxx
    
    watchtower:
        image: containrrr/watchtower
        container_name: watchtower
        restart: unless-stopped
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
        command: --cleanup --interval 300 chat2api
  6. Troubleshoot API Error Codes

    main

    If you encounter errors while using the API, check the following codes:

    • 401: IP does not support login-free access (try using a US IP or setting PROXY_URL) or authentication failed.
    • 403: Check the application logs for specific error details.
    • 429: Rate limit exceeded for the current IP. Wait or change IP.
    • 500: Internal server error; the request failed.
    • 502: Gateway error or network unavailability.
  7. Use the Reverse API for ChatGPT models

    main

    CHAT2API provides an OpenAI-compatible API for accessing various ChatGPT models. You can use AccessToken or RefreshToken for premium models, or use the service without a token for free GPT-3.5 (which defaults to text-davinci-002-render-sha if the model name does not contain gpt-4).

    Supported Models:

    • gpt-3.5-turbo (Free, no token required)
    • gpt-4, gpt-4o, gpt-4o-mini, gpt-4-mobile (Requires AccessToken)
    • o1-preview, o1-mini (Requires AccessToken)
    • o3-mini, o3-mini-high (Requires AccessToken)
    • GPTs (Use model name format gpt-4-gizmo-g-*)

    Team Accounts: If using a Team account, pass the ChatGPT-Account-ID via:

    1. A custom header: ChatGPT-Account-ID: <value>
    2. A comma-separated value in the Authorization header: Authorization: Bearer <AccessToken or RefreshToken>,<ChatGPT-Account-ID>
    curl --location 'http://127.0.0.1:5005/v1/chat/completions' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer {{Token}}' \
    --data '{
         "model": "gpt-3.5-turbo",
         "messages": [{"role": "user", "content": "Say this is a test!"}],
         "stream": true
       }'
  8. Configure Gateway Mode

    main

    The application behavior changes based on the enable_gateway configuration setting:

    • When enable_gateway is True: The application imports and serves various gateway modules (e.g., gateway.share, gateway.login, gateway.chatgpt, etc.).
    • When enable_gateway is False: The application enters a restricted mode where any request to a path not explicitly defined will return a 404 Not Found error with the detail "Gateway is disabled".
  9. Initialize the Client class

    main

    The Client class provides an asynchronous interface for making HTTP requests using curl_cffi.requests.AsyncSession. It supports proxy configuration, custom timeouts, SSL verification, and TLS impersonation to mimic specific browser fingerprints.

    Parameters

    • proxy (str, optional): A proxy URL for both HTTP and HTTPS traffic.
    • timeout (int, optional): Request timeout in seconds. Defaults to 15.
    • verify (bool, optional): Whether to verify SSL certificates. Defaults to True.
    • impersonate (str, optional): The TLS fingerprint to impersonate (e.g., 'safari15_3'). Defaults to 'safari15_3'.