Rerank

The POST /v2/rerank endpoint is a Cohere-compatible reranking mock. Register fixture patterns on the LLMock instance and the request query is matched against them — first match wins, no match returns empty results.

Endpoint

Method Path Request Body Response
POST /v2/rerank { "query": "...", "model": "..." } { "id": "...", "results": [...] }

How It Works

Unit Test: Rerank Fixture

rerank-fixture.test.ts ts
const mock = new LLMock();
mock.onRerank("machine learning", [
  { index: 0, relevance_score: 0.99 },
  { index: 2, relevance_score: 0.85 },
]);
await mock.start();

const res = await fetch(`${mock.url}/v2/rerank`, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ query: "machine learning basics" }),
});

const body = await res.json();
expect(body.results).toHaveLength(2);
expect(body.results[0].relevance_score).toBe(0.99);

Response Format

The response follows the Cohere rerank v2 shape — each result carries only index and relevance_score:

Response shape json
{
  "id": "rerank-...",
  "results": [
    { "index": 0, "relevance_score": 0.99 },
    { "index": 2, "relevance_score": 0.85 }
  ],
  "meta": { "billed_units": { "search_units": 0 } }
}

Validation

Journal Integration

Rerank requests are recorded in the LLMock journal with service: "rerank":

Journal entries typescript
const entries = mock.getRequests();
const rerankRequests = entries.filter((e) => e.service === "rerank");

See also the Services overview, plus the sibling pages Web Search and Moderation.