Package org.mockserver.llm.codec
Class OllamaCodec
java.lang.Object
org.mockserver.llm.codec.OllamaCodec
- All Implemented Interfaces:
ProviderCodec
Codec for Ollama
/api/chat endpoint (version ollama-2025).
Ollama uses a distinct JSON shape with a top-level message object
(not an array of choices). Token counts are exposed as prompt_eval_count
and eval_count.
Streaming format: Ollama's native wire format is NDJSON
(newline-delimited JSON, application/x-ndjson), not SSE. Each
streaming chunk is a single JSON object followed by a newline character
(<json>\n). This codec internally represents chunks as
SseEvent objects (reusing the existing data/delay model), but
declares StreamingFormat.NDJSON via streamingFormat() so
that the downstream write handler emits raw NDJSON framing instead of SSE
data: prefixes.
Tool calls use Ollama 0.3+ format where arguments is a JSON object
(not a JSON-as-string like OpenAI).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondecode(HttpRequest request) encode(Completion completion, String model) encodeEmbedding(EmbeddingResponse embedding, String input) Encodes an Ollama embedding response.encodeEmbedding(EmbeddingResponse embedding, String input, String model) Model-aware embedding encode.encodeStreaming(Completion completion, String model, StreamingPhysics physics) provider()The wire format this provider uses for streaming responses.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.mockserver.llm.ProviderCodec
encodeRerank
-
Constructor Details
-
OllamaCodec
public OllamaCodec()
-
-
Method Details
-
provider
- Specified by:
providerin interfaceProviderCodec
-
apiVersion
- Specified by:
apiVersionin interfaceProviderCodec
-
streamingFormat
Description copied from interface:ProviderCodecThe wire format this provider uses for streaming responses. Defaults toStreamingFormat.SSE; override for providers that use a different format (e.g. Ollama uses NDJSON).- Specified by:
streamingFormatin interfaceProviderCodec
-
encode
- Specified by:
encodein interfaceProviderCodec
-
encodeStreaming
public List<SseEvent> encodeStreaming(Completion completion, String model, StreamingPhysics physics) - Specified by:
encodeStreamingin interfaceProviderCodec
-
decode
- Specified by:
decodein interfaceProviderCodec
-
encodeEmbedding
Encodes an Ollama embedding response. Ollama exposes two endpoints:- the newer
POST /api/embedreturning a batch shape ({"embeddings":[[...]]}, plusmodelandprompt_eval_count), and - the legacy
POST /api/embeddingsreturning a single-vector shape ({"embedding":[...]}).
/api/embedbatch shape with a single embedding (the modern, recommended endpoint), which also satisfies clients that read theembeddingsarray. The Ollama default dimensionality fornomic-embed-textis 768.- Specified by:
encodeEmbeddingin interfaceProviderCodec
- the newer
-
encodeEmbedding
Description copied from interface:ProviderCodecModel-aware embedding encode. Most providers have a single embedding wire shape and ignore the model, so the default delegates toProviderCodec.encodeEmbedding(EmbeddingResponse, String). Providers whose embedding shape varies by model family (e.g. Bedrock Titan vs Cohere) override this to branch onmodel.- Specified by:
encodeEmbeddingin interfaceProviderCodec
-