Add your description here
Project description
Simple AI Gateway
A lightweight AI API Gateway built with Python and FastAPI. It follows the OpenAI-compatible request format and can be configured to either echo back prompts or forward them to a real AI inference backend.
🚀 Quick Start
1. Prerequisites
Ensure you have uv installed. uv is an extremely fast Python package manager that replaces pip and venv.
# If you don't have uv yet (macOS)
brew install uv
2. Installation & Environment Setup
uv will automatically manage your virtual environment and dependencies based on pyproject.toml.
# Clone the repository
git clone <your-repo-url>
cd simple-ai-gateway/src/simple_ai_gateway
# Sync dependencies and create a virtual environment automatically
uv sync
3. Create a .env file in the root directory:
PORT=8080
BACKEND_URL= # Leave empty to enable "Echo Mode"
4. Run the Server
Start the server.
uv run main.py
5. Testing the Gateway
Start the server at 8080:
uv run uvicorn main:app --host 0.0.0.0 --port 8080
Once the server is at http://localhost:8080, you can verify it using the following methods:
Method 1: Basic Echo Test (via cURL) Test if the gateway correctly extracts your message and echoes it back:
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-H "X-Request-ID: my-custom-id-123" \
-d '{
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}'
What to look for:
- The response should contain "content": "Echo: Hello, world!".
- The "id" field should match "my-custom-id-123".
Method 2: Auto-ID Generation Test If you don't provide an X-Request-ID header, the gateway will generate a unique UUID for you:
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Hi, baby!"}]
}'
What to look for: A valid UUID in the "id" field (e.g., 550e8400-e29b-...).
Method 3: Streaming Test Test the Server-Sent Events (SSE) streaming functionality. Use the -N flag to disable buffering and see the "typewriter" effect:
curl -N -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"stream": true,
"messages": [{"role": "user", "content": "This is a streaming test."}]
}'
What to look for: The response should arrive in chunks (prefixes of data: {...}) rather than all at once.
Method 4: Rate Limiting Test The gateway is configured to allow 5 requests per minute per IP. You can test this by running a quick loop:
for i in {1..6}; do
curl -s -o /dev/null -w "Request $i: %{http_code}\n" -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "ping"}]}';
done
What to look for: The first 5 requests should return 200, and the 6th request should return 429 (Too Many Requests).
Method 5: Interactive API Docs FastAPI automatically generates a Swagger UI. You can test the API directly from your browser: http://localhost:8080/docs
6. Features
- Format Validation: Strict Pydantic models for OpenAI compatibility.
- Streaming: Supports SSE-based streaming responses.
- Rate Limiting: Built-in memory-based sliding window protection.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_ai_gateway-0.1.0.tar.gz.
File metadata
- Download URL: simple_ai_gateway-0.1.0.tar.gz
- Upload date:
- Size: 52.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c618fa63d3c21131049c4614665bb9a73f98425545a74af731e4197a70231f86
|
|
| MD5 |
f2580f0a5efa49632ec29033ff978160
|
|
| BLAKE2b-256 |
4df7ba3524ca9383a81904e1e5373011dba5ff09926c625b772595eab4dd3881
|
Provenance
The following attestation bundles were made for simple_ai_gateway-0.1.0.tar.gz:
Publisher:
release.yml on miaomiaoxu99/simple-ai-gateway
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_ai_gateway-0.1.0.tar.gz -
Subject digest:
c618fa63d3c21131049c4614665bb9a73f98425545a74af731e4197a70231f86 - Sigstore transparency entry: 1002486312
- Sigstore integration time:
-
Permalink:
miaomiaoxu99/simple-ai-gateway@8ce437dac3baa58c99fc8491ed4697cdcbd0bd16 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/miaomiaoxu99
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8ce437dac3baa58c99fc8491ed4697cdcbd0bd16 -
Trigger Event:
release
-
Statement type:
File details
Details for the file simple_ai_gateway-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_ai_gateway-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b61348f7d54be82d87cffa9faa36a65237c571c2d35ec70b26764e9850508297
|
|
| MD5 |
19d9f24989ea914a8e96d8adb095ca06
|
|
| BLAKE2b-256 |
b4bafe6bbca8015fe9f30ac773f81e6d83732fc26445778cbe6eba479041ee57
|
Provenance
The following attestation bundles were made for simple_ai_gateway-0.1.0-py3-none-any.whl:
Publisher:
release.yml on miaomiaoxu99/simple-ai-gateway
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_ai_gateway-0.1.0-py3-none-any.whl -
Subject digest:
b61348f7d54be82d87cffa9faa36a65237c571c2d35ec70b26764e9850508297 - Sigstore transparency entry: 1002486387
- Sigstore integration time:
-
Permalink:
miaomiaoxu99/simple-ai-gateway@8ce437dac3baa58c99fc8491ed4697cdcbd0bd16 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/miaomiaoxu99
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8ce437dac3baa58c99fc8491ed4697cdcbd0bd16 -
Trigger Event:
release
-
Statement type: