Mock clients for your favorite LLM APIs
Project description
MockAI
False LLM endpoints for testing
MockAI provides a local server that interops with multiple LLM SDKs, so you can call these APIs as normal but receive mock or pre-determined responses at no cost!
The package currently provides full support for OpenAI and Anthropic. It patches these libraries directly under the hood, so it will always be up to date.
Installation
# With pip
pip install ai-mock
# With poetry
poetry add ai-mock
# With uv
uv add ai-mock
Usage
Start the MockAI server
This is the server that the mock clients will communicate with, we'll see later how we can configure our own pre-determined responses :).
# After installing MockAI
$ mockai
Chat Completions
To use a mock version of these providers, you only have to change a single line of code (and just barely!):
- from openai import OpenAI # Real Client
+ from mockai.openai import OpenAI # Fake Client
# Rest of the code remains the exact same!
client = OpenAI()
response = client.chat.completions.create(
model="gpt-5", # Model can be whatever you want
messages=[
{
"role": "user",
"content": "Hi Mock!"
}
],
# All other kwargs are accepted, but ignored (except for stream ;))
temperate = 0.7,
top_k = 0.95
)
print(response.choices[0].message.content)
# >> "Hi Mock!"
# By default, the response will be a copy of the
# content of the last message in the conversation
Alternatively, you can use the real SDK and set the base url to the MockAI server address
from openai import OpenAI # Real Client
# The mockai server runs on port 8100 by default
client = OpenAI(api_key="not used but required", base_url="http://localhost:8100/openai")
response = client.chat.completions.create(
model="gpt-5",
messages=[
{
"role": "user",
"content": "Hi Mock!"
}
],
temperate = 0.7,
top_k = 0.95
)
print(response.choices[0].message.content)
# >> "Hi Mock!"
MockAI also provides clients for Anthropic:
# from anthropic import Anthropic
from mockai.anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
model="claude-3.5-opus",
messages=[{"role": "user", "content": "What's up!"}],
max_tokens=1024
)
print(response.content)
# >> "What's up!"
And of course the async versions of all clients are supported:
from mockai.openai import AsyncOpenAI
from mockai.anthropic import AsyncAnthropic
Streaming is supported as well:
from mockai.openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": "Hi mock!"}],
stream = True
)
# Streaming mock responses will yield one letter per chunk
for chunk in response:
if chunk.choices:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content)
# >> H
# >> i
# >>
# >> m
# >> o
# >> c
# >> k
# >> !
To learn more about the usage of each client, you can look at the docs of the respective provider, the mock clients are the exact same!
Tool Calling
All mock clients also work with tool calling! To trigger a tool call, you must specify it in a pre-determined response.
from mockai.openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": "Function!"}],
)
print(response.choices[0].message.tool_calls[0].function.name)
# >> "mock"
print(response.choices[0].message.tool_calls[0].function.arguments)
# >> "{"mock_arg": "mock_val"}"
Configure responses
The MockAI server takes an optional path to a JSON file were we can establish our responses for both completions and tool calls. The structure of the json is simple: Each object must have a "type" key of value "text" or "function", an input key with a value, which is what will be matched against, and an output key, which is what will be returned if the input key matches the user input.
// mock_responses.json
[
{
"type": "text",
"input": "How are ya?",
"output": "I'm fine, thank u 😊. How about you?"
},
{
"type": "function",
"input": "Where's my order?",
"output": {
"name": "get_delivery_date",
"arguments": {
"order_id": "1337"
}
}
}
]
When creating your .json file, please follow these rules:
- Each response must have a
type
key, whose value must be eithertext
orfunction
, this will determine the response object of the client. - Responses of type
text
must have aoutput
key with a string value. - Responses of type
function
must have aname
key with the name of the function, and aarguments
key with a dict of args and values (Example: {"weather": "42 degrees Fahrenheit"}). - Responses of type
function
can accept a list of objects, to simulate parallel tool calls.
Load the json file
To create a MockAI server with our json file, we just need to pass it to the mockai command.
$ mockai mock_responses.json
# The full file path can also be passed
$ mockai ~/home/foo/bar/mock_responses.json
With this, our mock clients will have access to our pre-determined responses!
from mockai.openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": "How are ya?"}],
)
print(response.choices[0].message.content)
# >> "I'm fine, thank u 😊. How about you?"
response = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": "Where's my order?"}],
)
print(response.choices[0].message.tool_calls[0].function.name)
# >> "get_delivery_date"
print(response.choices[0].message.tool_calls[0].function.arguments)
# >> "{'order_id': '1337'}"
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
File details
Details for the file ai_mock-0.2.4.tar.gz
.
File metadata
- Download URL: ai_mock-0.2.4.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-45-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78805299d28703044f2f12f74a2c72aa2185d7c30a3f3192347aac60d9d59178 |
|
MD5 | 121ea0bfc856663a789dcdbeeba970b6 |
|
BLAKE2b-256 | abc926d2e48b346d12dfdb31e613aa89539d8d5701d292757908c406e67a3554 |
File details
Details for the file ai_mock-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: ai_mock-0.2.4-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-45-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dbb5bc637c517cf8edba942100e75bb571fc0b65ba28c7346508f0e1943d187 |
|
MD5 | a89add4979a64f915a2e0361ce6afb19 |
|
BLAKE2b-256 | ac031a01962378e927daa8e4fb1f3c5994c9029ef271c6b1a648daed4d37defc |