Skip to content

Commit 52e48d8

Browse files
authored
Merge pull request #4 from ruvnet/proxy
Proxy
2 parents d8a83cb + 70a457a commit 52e48d8

File tree

387 files changed

+57287
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

387 files changed

+57287
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ dist-ssr
2626
.aider*
2727
*cache*
2828
aider*
29+
venv/
30+
.api_keys.json
31+
synthlang_proxy.db
32+
*.db

proxy/.dockerignore

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Python
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
*.so
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# Virtual Environment
28+
venv/
29+
ENV/
30+
env/
31+
32+
# IDE
33+
.idea/
34+
.vscode/
35+
*.swp
36+
*.swo
37+
38+
# Docker
39+
Dockerfile
40+
docker-compose.yml
41+
.dockerignore
42+
43+
# Logs
44+
*.log
45+
logs/
46+
47+
# Database
48+
*.db
49+
*.db-journal
50+
*.db.backup
51+
52+
# Benchmark results
53+
benchmark_results/
54+
55+
# Documentation
56+
docs/
57+
58+
# Test files
59+
tests/
60+
test_scripts/
61+
62+
# Examples
63+
examples/
64+
65+
# Temporary files
66+
.pytest_cache/
67+
.coverage
68+
htmlcov/
69+
.tox/
70+
.nox/
71+
.hypothesis/
72+
.coverage.*
73+
coverage.xml
74+
*.cover

proxy/.env.sample

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# SynthLang Proxy Configuration
2+
# Copy this file to .env and modify as needed
3+
4+
# Server Configuration
5+
PORT=8000
6+
HOST=0.0.0.0
7+
DEBUG=false
8+
9+
# Security
10+
# Generate a secure random key for encryption (32 bytes, base64 encoded)
11+
# You can generate one with: python -c "import os, base64; print(base64.b64encode(os.urandom(32)).decode())"
12+
ENCRYPTION_KEY=
13+
14+
# Database Configuration
15+
# PostgreSQL connection URL (required)
16+
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/synthlang_proxy
17+
18+
# For development/testing, you can use SQLite instead
19+
USE_SQLITE=0
20+
SQLITE_PATH=sqlite+aiosqlite:///./synthlang_proxy.db
21+
22+
# Debug SQL queries (set to 1 to enable)
23+
DEBUG_SQL=0
24+
25+
# Rate Limiting
26+
# Default rate limits (requests per minute)
27+
DEFAULT_RATE_LIMIT_QPM=60
28+
PREMIUM_RATE_LIMIT=120
29+
30+
# SynthLang Integration
31+
# Enable/disable SynthLang compression (set to 0 to disable)
32+
USE_SYNTHLANG=1
33+
34+
# PII Masking Configuration
35+
# Mask PII before sending to LLM (set to 1 to enable)
36+
MASK_PII_BEFORE_LLM=0
37+
# Mask PII in logs and database (set to 0 to disable)
38+
MASK_PII_IN_LOGS=1
39+
40+
# LLM Provider Configuration
41+
# OpenAI API key (required)
42+
OPENAI_API_KEY=
43+
# Default model to use
44+
DEFAULT_MODEL=gpt-4o
45+
# Timeout for LLM API calls (in seconds)
46+
LLM_TIMEOUT=30
47+
48+
# Semantic Cache
49+
# Enable/disable semantic cache (set to 0 to disable)
50+
ENABLE_CACHE=1
51+
# Similarity threshold for cache hits (0.0-1.0)
52+
CACHE_SIMILARITY_THRESHOLD=0.95
53+
# Maximum number of items to keep in cache
54+
CACHE_MAX_ITEMS=1000
55+
56+
# Logging
57+
LOG_LEVEL=INFO
58+
LOG_FILE=proxy.log

proxy/DOCKER_README.md

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Docker Setup for SynthLang Proxy
2+
3+
This document provides instructions for running the SynthLang Proxy using Docker.
4+
5+
## Prerequisites
6+
7+
- Docker installed on your system
8+
- Docker Compose installed on your system
9+
- OpenAI API key (or other compatible LLM provider)
10+
11+
## Quick Start
12+
13+
1. Clone the repository and navigate to the proxy directory:
14+
```bash
15+
git clone https://github.com/yourusername/synthlang-proxy.git
16+
cd synthlang-proxy/proxy
17+
```
18+
19+
2. Create a `.env` file with your configuration:
20+
```bash
21+
cp .env.sample .env
22+
# Edit .env with your OpenAI API key and other settings
23+
```
24+
25+
3. Build and start the containers:
26+
```bash
27+
docker-compose up -d
28+
```
29+
30+
4. The proxy will be available at http://localhost:8000
31+
32+
## Configuration Options
33+
34+
You can configure the proxy by setting environment variables in your `.env` file or directly in the `docker-compose.yml` file.
35+
36+
### Essential Configuration
37+
38+
- `OPENAI_API_KEY`: Your OpenAI API key (required)
39+
- `API_KEY`: API key for accessing the proxy (will be generated if not provided)
40+
- `ADMIN_API_KEY`: API key for admin access (will be generated if not provided)
41+
42+
### Database Configuration
43+
44+
By default, the proxy uses SQLite, but you can configure it to use PostgreSQL:
45+
46+
- `USE_SQLITE`: Set to 1 to use SQLite, 0 to use PostgreSQL
47+
- `DATABASE_URL`: PostgreSQL connection URL (used when USE_SQLITE=0)
48+
49+
### Advanced Configuration
50+
51+
- `DEFAULT_MODEL`: Default LLM model to use (default: gpt-4o-mini)
52+
- `ENABLE_CACHE`: Enable semantic caching (1=enabled, 0=disabled)
53+
- `USE_SYNTHLANG`: Enable SynthLang compression (1=enabled, 0=disabled)
54+
- `ENCRYPTION_KEY`: Key for encrypting sensitive data (will be generated if not provided)
55+
56+
## Using PostgreSQL
57+
58+
The Docker Compose setup includes a PostgreSQL database. To use it:
59+
60+
1. Set `USE_SQLITE=0` in your `.env` file
61+
2. Set `DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/synthlang_proxy`
62+
63+
## Volumes
64+
65+
The Docker Compose setup creates the following volumes:
66+
67+
- `postgres_data`: Persistent storage for the PostgreSQL database
68+
- `./synthlang_proxy.db:/app/synthlang_proxy.db`: Maps the SQLite database file to your local filesystem
69+
- `./.api_keys.json:/app/.api_keys.json`: Maps the API keys file to your local filesystem
70+
71+
## Health Checks
72+
73+
The Docker Compose setup includes health checks for both the proxy and the database. You can monitor the health of the services using:
74+
75+
```bash
76+
docker-compose ps
77+
```
78+
79+
## Logs
80+
81+
To view the logs:
82+
83+
```bash
84+
docker-compose logs -f proxy
85+
```
86+
87+
## Stopping the Services
88+
89+
To stop the services:
90+
91+
```bash
92+
docker-compose down
93+
```
94+
95+
To stop the services and remove the volumes:
96+
97+
```bash
98+
docker-compose down -v
99+
```
100+
101+
## Rebuilding the Image
102+
103+
If you make changes to the code, you'll need to rebuild the image:
104+
105+
```bash
106+
docker-compose build
107+
```
108+
109+
## Troubleshooting
110+
111+
### Database Initialization Issues
112+
113+
If you encounter issues with database initialization, you can run the initialization script manually:
114+
115+
```bash
116+
docker-compose exec proxy python init_db_roles.py
117+
```
118+
119+
### API Key Issues
120+
121+
If you need to create a new API key:
122+
123+
```bash
124+
docker-compose exec proxy python -m src.cli.api_keys create --user-id "your_username" --rate-limit 100
125+
```
126+
127+
### Connection Issues
128+
129+
If you can't connect to the proxy, check that the container is running:
130+
131+
```bash
132+
docker-compose ps
133+
```
134+
135+
And check the logs for any errors:
136+
137+
```bash
138+
docker-compose logs proxy

proxy/Dockerfile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
FROM python:3.10-slim
2+
3+
# Set working directory
4+
WORKDIR /app
5+
6+
# Set environment variables
7+
ENV PYTHONDONTWRITEBYTECODE=1 \
8+
PYTHONUNBUFFERED=1 \
9+
PORT=8000 \
10+
HOST=0.0.0.0 \
11+
DEBUG=false \
12+
USE_SQLITE=1 \
13+
SQLITE_PATH=sqlite+aiosqlite:///./synthlang_proxy.db
14+
15+
# Install system dependencies
16+
RUN apt-get update && apt-get install -y --no-install-recommends \
17+
gcc \
18+
python3-dev \
19+
openssl \
20+
&& apt-get clean \
21+
&& rm -rf /var/lib/apt/lists/*
22+
23+
# Copy requirements file
24+
COPY requirements.txt .
25+
26+
# Install Python dependencies
27+
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
28+
pip install --no-cache-dir -r requirements.txt
29+
30+
# Copy the application code
31+
COPY . .
32+
33+
# Generate encryption key if not provided
34+
RUN if [ -z "$ENCRYPTION_KEY" ]; then \
35+
export ENCRYPTION_KEY=$(openssl rand -hex 16); \
36+
echo "ENCRYPTION_KEY=$ENCRYPTION_KEY" >> .env; \
37+
fi
38+
39+
# Create a non-root user to run the application
40+
RUN useradd -m appuser && \
41+
chown -R appuser:appuser /app
42+
43+
# Switch to non-root user
44+
USER appuser
45+
46+
# Initialize the database and set up roles
47+
RUN python init_db_roles.py
48+
49+
# Expose the port
50+
EXPOSE 8000
51+
52+
# Start the application
53+
CMD ["uvicorn", "src.app.main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)