ExLLM.Adapters.Mock (ex_llm v0.5.0)
View SourceMock adapter for testing ExLLM integrations.
This adapter provides predictable responses for testing without making actual API calls. It supports all ExLLM features including streaming, function calling, and error simulation.
Configuration
The mock adapter can be configured with predefined responses or response generators:
# Static response
ExLLM.Adapters.Mock.set_response(%{
content: "Mock response",
model: "mock-model",
usage: %{input_tokens: 10, output_tokens: 20}
})
# Dynamic response based on input
ExLLM.Adapters.Mock.set_response_handler(fn messages, options ->
%{content: "You said: #{List.last(messages).content}"}
end)
# Simulate errors
ExLLM.Adapters.Mock.set_error({:api_error, %{status: 500, body: "Server error"}})
Testing Features
- Capture and inspect requests
- Simulate various response types
- Test error handling
- Validate function calling
- Test streaming behavior
Summary
Functions
Returns a specification to start this module under a supervisor.
Returns the currently configured cached provider, if any.
Gets the last captured request.
Gets all captured requests.
Lists available cached providers.
Clears all captured requests and resets to defaults.
Sets an error response.
Sets a function call response.
Sets a static response for all requests.
Sets a dynamic response handler.
Sets stream chunks for streaming responses.
Starts the mock adapter agent.
Configures the mock adapter to use cached responses from a specific provider.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Returns the currently configured cached provider, if any.
Gets the last captured request.
Gets all captured requests.
Lists available cached providers.
Clears all captured requests and resets to defaults.
Sets an error response.
Sets a function call response.
Sets a static response for all requests.
Sets a dynamic response handler.
Sets stream chunks for streaming responses.
Starts the mock adapter agent.
Configures the mock adapter to use cached responses from a specific provider.
This allows the mock adapter to mimic the behavior of real providers using previously cached responses.
Examples
# Use cached OpenAI responses
ExLLM.Adapters.Mock.use_cached_responses(:openai)
# Use cached Anthropic responses
ExLLM.Adapters.Mock.use_cached_responses("anthropic")
# Now mock calls will return realistic responses
{:ok, response} = ExLLM.chat(messages, provider: :mock)