Skip to main content

Caching and replaying man-in-the-middle proxy for OpenAI APIs

Project description

Rechat

Rechat is a caching and replaying man-in-the-middle proxy for OpenAI's APIs, it provides inspection and debugging layer, particularly useful for quick inspection of interactions of existing clients, developing multi-request workflows, and benchmarks.

Rechat is for you, if you ever wanted to:

  • speed up your code that makes repeated calls to OpenAI APIs
  • quickly inspect what is being sent to OpenAI APIs
  • emulate an endpoint with pre-recorded (or pre-defined) responses

Quickstart

  1. pip install rechat (dev: pip install git+https://gitlab-master.nvidia.com/dchichkov/rechat.git)
  2. Run rechat, it will listen on eight-nine-ten port (http://localhost:8910/v1) and use OpenAI's endpoint by default as upstream.
  3. Configure your OpenAI client to use it export OPENAI_BASE_URL=http://localhost:8910/v1 and run your requests as usual.

You can specify a different upstream endpoint by providing it as an argument, e.g. rechat https://api.openai.com/v1.

By default, rechat outputs intercepted chat content onto the console:

Rechat Console

And it records the session to flows_<timestamp>.dump file in the current directory. During subsequent runs, if a -f/--flow [dump_file] argument is provided, rechat would attempt to load flows_[timestamp].dump files, or the specified dump file. It always tries to use cached responses for any matching requests.

Inspection

Rechat provides http://localhost:8910 web UI for inspecting the current session, with search and filtering capabilities. By default, rechat will output chat content to the console. Use --quiet flag to reduce verbosity.

Additionally, a --diff mode would search for similar requests on cache misses and print difference between the current request and the closest cached request. This is useful for ensuring that different clients or implementations of the same functionality produce the same requests.

Recording and Replaying

Any markdown editor, for example VSCode or GitHub/GitLab web UI, can be used to view and edit the logs, and these modified logs can be loaded into rechat, to emulate model's responses.

Architecture

flowchart TB
    subgraph Client["Client Application"]
        APP[["OpenAI SDK / HTTP Client"]]
    end

    subgraph Rechat["Rechat Proxy (MITM)"]
        PROXY["Proxy Server"]
        INTERCEPT["Request Interceptor"]
        
        subgraph CacheLookup["Cache Lookup"]
            EXACT["Exact Match?"]
            FUZZY["Fuzzy Match\n(similarity search)"]
        end
        
        CACHE[("Cache Storage")]
        REPLAY["Replay Cached Response"]
        DIFF["Print Diff 📋"]
        STORE["Record Response"]
    end

    subgraph OpenAI["OpenAI API"]
        API[["api.openai.com"]]
    end

    APP -->|"HTTPS Request\n(via proxy)"| PROXY
    PROXY --> INTERCEPT
    INTERCEPT --> EXACT
    
    EXACT -->|"yes"| REPLAY
    REPLAY --> RETURN
    
    EXACT -->|"no"| FUZZY
    FUZZY -->|"found similar"| DIFF
    FUZZY -->|"no match"| FORWARD
    DIFF --> FORWARD
    
    CACHE <-->|"lookup"| EXACT
    CACHE <-->|"similarity search"| FUZZY
    
    FORWARD["Forward Request"] --> API
    API -->|"Response"| STORE
    STORE -->|"save"| CACHE
    STORE --> RETURN["Return Response"]
    
    RETURN --> APP

    style Rechat fill:#1a1a2e,stroke:#16213e,color:#eee
    style CacheLookup fill:#0f3460,stroke:#16213e,color:#eee
    style CACHE fill:#e94560,stroke:#16213e,color:#fff
    style PROXY fill:#533483,stroke:#16213e,color:#fff
    style DIFF fill:#f9a825,stroke:#16213e,color:#000
    style REPLAY fill:#4caf50,stroke:#16213e,color:#fff

Rechat Markdown Format

Example markdown snippet, in markdown format. Note <blockquote> tags. See more details in sample.md.

### user
<blockquote>
What is the capital of France?
</blockquote>

### assistant
<blockquote>
Paris.
</blockquote>

Intercepting traffic to existing OpenAI endpoints

Rechat can intercept traffic to existing endpoints, without changing the client code or configuring OPENAI_BASE_URL, by using mitmproxy as a transparent proxy. For example, to use mitmproxy local proxy mode and intercept traffic from a python script (for example python scripts/query.py), use the following command to intercept the traffic from python:

rechat --mode local:python

And run the python script as follows, specifying mitmproxy's CA certificate for SSL interception:

SSL_CERT_FILE=~/.mitmproxy/mitmproxy-ca-cert.pem python scripts/query.py

Note: SSL_CERT_FILE environment variable is required for Python clients to trust mitmproxy's root CA certificate. Please refer to mitmproxy's documentation for more details on installing and trusting mitmproxy's root CA certificate on your system.

Routing and Load Balancing

TODO: Rechat supports multiple endpoints, using <endpoint>:[local_port]:[model_name] arguments, e.g. https://api.openai.com/v1:8910:gpt-5. It would route the requests to the appropriate endpoint based on the model name in the request, and it'd balance the load between endpoints for the same model.

Miscellaneous

  • Replaying queries against an endpoint
  • Multiple responses for the same request

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

rechat-0.1.6.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

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

rechat-0.1.6-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file rechat-0.1.6.tar.gz.

File metadata

  • Download URL: rechat-0.1.6.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for rechat-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1ed4553dadaa1b217fab0d06b673967745512fe49e295bc70727d6c29f90f6b9
MD5 e0dc113ac5aaffd9b65b10ecf8c7177b
BLAKE2b-256 42f06cdc654838d95be3d4e73c369c1001fdde6993f1102d75003847e058b761

See more details on using hashes here.

File details

Details for the file rechat-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: rechat-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for rechat-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4c505f7295cf879b28943e51dc522c04bf79d6e71f73765bdbb1ee69d6c9ce48
MD5 5deb5f2ad121be52fbfc7ceb117c324f
BLAKE2b-256 89f77f262461e9f6e684fb927b5cce74347eb3782e7640805baf9e37c662f853

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