A robust HTTP proxy server built with FastAPI designed specifically for collecting fine-tuning data at the LLM level, especially for CLINE and Roo coder. This proxy provides request/response logging, error handling, and supports both streaming and non-streaming responses.
This project facilitates the collection of high-quality data for fine-tuning large language models. It works by capturing the raw API requests and responses, which can then be processed into training data formats.
- Full Request/Response Logging: All requests and responses are logged to files for debugging and analysis
- Streaming Support: Handles streaming responses (like SSE) efficiently
- Robust Error Handling: Comprehensive error handling with detailed logging
- JSON Pretty Printing: Logs JSON responses in a readable format
- Configurable: Simple configuration options for host, port and logging directory
- Python 3.7+
- FastAPI
- Uvicorn
- httpx
-
Clone the repository:
git clone https://github.com/JensCaasen/llm-proxy.git cd llm-proxy
-
Install dependencies:
pip install -r requirements.txt
To use this proxy for data collection:
- Go to your application's Settings
- Use Custom Base URLs
- Add
http://localhost:5002?p=(existing URL here)
to the URL - Keep all other credentials and configurations the same
For example, if your current API endpoint is https://api.openai.com
, you would change it to:
http://localhost:5002?p=https://api.openai.com
Run the proxy server:
./run_llm_proxy.sh
or
python proxy_server.py
By default, the server runs on localhost:5002
and logs to the logs/
directory.
The process consists of two phases:
- Data Collection Phase: The proxy intercepts and logs all API requests and responses when you use your application.
- Post-Processing Phase: The logged data is processed into a usable format for fine-tuning.
Use the parse_log.py
script to process the raw logged data:
python parse_log.py logs/
This script parses the raw data into a usable format, supporting:
- OpenAI and Anthropic API formats
- Both streaming and non-streaming responses
Each log file is processed and a corresponding -parsed.txt
file is created containing the extracted text content.
- The proxy receives an incoming request with the
p
parameter specifying the upstream URL - It logs the request details to a uniquely numbered request file
- The request is forwarded to the upstream URL
- The response is streamed back to the client and simultaneously logged
- All logs are stored in the configured logs directory
The proxy logs each request/response pair to the logs/
directory with the following naming convention:
{request_num}-request.json
: The request body{request_num}-response.json
: The response body (if it's valid JSON){request_num}-response.txt
: The response body (if it's not valid JSON){request_num}-response-error-*.txt
: Error information if the request fails
You can modify the following variables in proxy_server.py
:
LOGS_DIR = "logs" # Directory for logs
HOST = "localhost" # Host to bind to
PORT = 5002 # Port to listen on