Skip to main content

A mock server that mimics OpenAI and Anthropic API formats for testing

Project description

Mock LLM Server

CI PyPI version License

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
  • JSON logging
  • Error handling
  • Mock token counting

Installation

From PyPI

pip install mockllm

From Source

  1. Clone the repository:
git clone https://github.com/stacklok/mockllm.git
cd mockllm
  1. Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 -
  1. Install dependencies:
poetry install  # Install with all dependencies
# or
poetry install --without dev  # Install without development dependencies

Usage

  1. Set up the responses.yml
cp example.responses.yml responses.yml
  1. 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

  1. 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 two main sections:

  1. responses: Maps input prompts to predefined responses
  2. defaults: Contains default configurations like the unknown response message

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."

Hot Reloading

The server automatically detects changes to responses.yml and reloads the configuration without requiring a restart.

Development

The project uses Poetry for dependency management and includes a Makefile to help with common development tasks:

# Set up development environment
make setup

# Run all checks (setup, lint, test)
make all

# Run tests
make test

# Format code
make format

# Run all linting and type checking
make lint

# Clean up build artifacts
make clean

# See all available commands
make help

Development Commands

  • make setup: Install all development dependencies with Poetry
  • make test: Run the test suite
  • make format: Format code with black and isort
  • make lint: Run all code quality checks (format, lint, type)
  • make build: Build the package with Poetry
  • make clean: Remove build artifacts and cache files
  • make install-dev: Install package with development dependencies

For more details on available commands, run make help.

Error Handling

The server includes comprehensive error handling:

  • Invalid requests return 400 status codes with descriptive messages
  • Server errors return 500 status codes with error details
  • All errors are logged using JSON format

Logging

The server uses JSON-formatted logging for:

  • Incoming request details
  • Response configuration loading
  • Error messages and stack traces

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

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

mockllm-0.0.5.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

mockllm-0.0.5-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file mockllm-0.0.5.tar.gz.

File metadata

  • Download URL: mockllm-0.0.5.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mockllm-0.0.5.tar.gz
Algorithm Hash digest
SHA256 3dfbc70bf25de4517fc5d54ba84da53baaff8b6495bd8af6e575e2482969c653
MD5 a58a800fa3d1f23b093ff8aaf5512ae5
BLAKE2b-256 ae2c56ed1e826f7174357a2b3bdb891f00da571a7110dd039ad8bca951af6887

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockllm-0.0.5.tar.gz:

Publisher: publish.yml on stacklok/mockllm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mockllm-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: mockllm-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for mockllm-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0f5b84d782d47c1cac2dde0ab0505dda1b7a1ce0e568e3965a6f5d8f0d222053
MD5 7d9cb2fb3340f0cdb15ea565c7c03112
BLAKE2b-256 ba1cce774ee4beccccd2cd3b757656b2644dc782fbae5cbd76ac9eb308c8a7c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockllm-0.0.5-py3-none-any.whl:

Publisher: publish.yml on stacklok/mockllm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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