A mock server that mimics OpenAI and Anthropic API formats for testing
Project description
Mock LLM Server
A FastAPI-based mock LLM server that mimics OpenAI and Anthropic API formats. Instead of calling actual language models, it uses predefined responses from a YAML configuration file.
This is made for when you want a deterministic response for testing or development purposes.
Check out the CodeGate project when you're done here!
Features
- OpenAI and Anthropic compatible API endpoints
- Streaming support (character-by-character response streaming)
- Configurable responses via YAML file
- Hot-reloading of response configurations
- Mock token counting
Installation
From PyPI
pip install mockllm
From Source
- Clone the repository:
git clone https://github.com/stacklok/mockllm.git
cd mockllm
- Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 -
- Install dependencies:
poetry install # Install with all dependencies
# or
poetry install --without dev # Install without development dependencies
Usage
- Set up the responses.yml
cp example.responses.yml responses.yml
- Start the server:
poetry run python -m mockllm
Or using uvicorn directly:
poetry run uvicorn mockllm.server:app --reload
The server will start on http://localhost:8000
- Send requests to the API endpoints:
OpenAI Format
Regular request:
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mock-llm",
"messages": [
{"role": "user", "content": "what colour is the sky?"}
]
}'
Streaming request:
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mock-llm",
"messages": [
{"role": "user", "content": "what colour is the sky?"}
],
"stream": true
}'
Anthropic Format
Regular request:
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-sonnet-20240229",
"messages": [
{"role": "user", "content": "what colour is the sky?"}
]
}'
Streaming request:
curl -X POST http://localhost:8000/v1/messages \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-sonnet-20240229",
"messages": [
{"role": "user", "content": "what colour is the sky?"}
],
"stream": true
}'
Configuration
Response Configuration
Responses are configured in responses.yml. The file has three main sections:
responses: Maps input prompts to predefined responsesdefaults: Contains default configurations like the unknown response messagesettings: Contains server behavior settings like network lag simulation
Example responses.yml:
responses:
"what colour is the sky?": "The sky is blue during a clear day due to a phenomenon called Rayleigh scattering."
"what is 2+2?": "2+2 equals 9."
defaults:
unknown_response: "I don't know the answer to that. This is a mock response."
settings:
lag_enabled: true
lag_factor: 10 # Higher values = faster responses (10 = fast, 1 = slow)
Network Lag Simulation
The server can simulate network latency for more realistic testing scenarios. This is controlled by two settings:
lag_enabled: When true, enables artificial network laglag_factor: Controls the speed of responses- Higher values (e.g., 10) result in faster responses
- Lower values (e.g., 1) result in slower responses
- Affects both streaming and non-streaming responses
For streaming responses, the lag is applied per-character with slight random variations to simulate realistic network conditions.
Hot Reloading
The server automatically detects changes to responses.yml and reloads the configuration without restarting the server.
Testing
To run the tests:
poetry run pytest
Contributing
Contributions are welcome! Please open an issue or submit a PR.
License
This project is licensed under the Apache 2.0 License.
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
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 mockllm-0.0.7.tar.gz.
File metadata
- Download URL: mockllm-0.0.7.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56c108ab0beaa3f2c777a8b37c41cf2bcd40074bfa52ec70b74b1d4ba25bf3f2
|
|
| MD5 |
8c42db0e632887e5f6b4ceb21f4e0c36
|
|
| BLAKE2b-256 |
53103fd290efb510188da0e18d295bb5bac1565cdcb681b2d75f4a1fb6ef91a4
|
Provenance
The following attestation bundles were made for mockllm-0.0.7.tar.gz:
Publisher:
publish.yml on stacklok/mockllm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mockllm-0.0.7.tar.gz -
Subject digest:
56c108ab0beaa3f2c777a8b37c41cf2bcd40074bfa52ec70b74b1d4ba25bf3f2 - Sigstore transparency entry: 171705609
- Sigstore integration time:
-
Permalink:
stacklok/mockllm@6dc916b2cc8d2347a4b4130deb28b65632906187 -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/stacklok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6dc916b2cc8d2347a4b4130deb28b65632906187 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mockllm-0.0.7-py3-none-any.whl.
File metadata
- Download URL: mockllm-0.0.7-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b0334d5e3d2fd1e678f798c3e944999bb77e326d413f88a82ebc81a6b99a2ce
|
|
| MD5 |
ffe02aaad907aaa0fda1631adbca13d2
|
|
| BLAKE2b-256 |
8da06ae6bc25a53c85c17a3b3d6cffb5f8f41d0ab290b68e8549e312e8df6b0b
|
Provenance
The following attestation bundles were made for mockllm-0.0.7-py3-none-any.whl:
Publisher:
publish.yml on stacklok/mockllm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mockllm-0.0.7-py3-none-any.whl -
Subject digest:
5b0334d5e3d2fd1e678f798c3e944999bb77e326d413f88a82ebc81a6b99a2ce - Sigstore transparency entry: 171705610
- Sigstore integration time:
-
Permalink:
stacklok/mockllm@6dc916b2cc8d2347a4b4130deb28b65632906187 -
Branch / Tag:
refs/tags/v0.0.7 - Owner: https://github.com/stacklok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6dc916b2cc8d2347a4b4130deb28b65632906187 -
Trigger Event:
release
-
Statement type: