org.llm4s.reranker

Members list

Type members

Classlikes

Cohere Rerank API implementation.

Cohere Rerank API implementation.

Calls POST https://api.cohere.ai/v1/rerank with:

  • model: reranking model name (e.g., "rerank-english-v3.0")
  • query: search query
  • documents: array of document strings
  • top_n: number of results to return
  • return_documents: whether to include document text

Attributes

See also
Companion
object
Supertypes
trait Reranker
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
class LLMReranker(client: LLMClient, batchSize: Int, systemPrompt: Option[String]) extends Reranker

LLM-based reranker using a language model to score documents.

LLM-based reranker using a language model to score documents.

Uses a structured prompt to ask the LLM to rate document relevance on a scale of 0 to 1. This is slower and more expensive than cross-encoder rerankers (like Cohere) but works with any LLM.

Usage:

val llmClient = /* build LLMClient with your ProviderConfig */
val reranker  = LLMReranker(llmClient)

val request = RerankRequest(
 query = "What is Scala?",
 documents = Seq("Scala is a programming language", "Python is popular"),
 topK = Some(5)
)

val response = reranker.rerank(request)

Value parameters

batchSize

Number of documents to score per LLM call

client

LLM client for generating scores

systemPrompt

Custom system prompt (optional)

Attributes

Companion
object
Supertypes
trait Reranker
class Object
trait Matchable
class Any
object LLMReranker

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
final case class RerankError(code: Option[String], message: String, provider: String) extends LLMError

Error from a reranking operation.

Error from a reranking operation.

Value parameters

code

Error code (HTTP status or provider-specific)

message

Human-readable error message

provider

Provider name (cohere, llm, etc.)

Attributes

Supertypes
trait LLMError
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class RerankProviderConfig(baseUrl: String, apiKey: String, model: String)

Configuration for reranker providers.

Configuration for reranker providers.

Value parameters

apiKey

API key for authentication

baseUrl

API endpoint

model

Model name (e.g., "rerank-english-v3.0")

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class RerankRequest(query: String, documents: Seq[String], topK: Option[Int], returnDocuments: Boolean)

Request to rerank documents against a query.

Request to rerank documents against a query.

Value parameters

documents

The documents to rerank

query

The query to rank documents against

returnDocuments

Whether to return document content in results

topK

Maximum results to return (None = all)

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class RerankResponse(results: Seq[RerankResult], metadata: Map[String, String])

Response from a reranking operation.

Response from a reranking operation.

Value parameters

metadata

Provider-specific metadata (model, latency, etc.)

results

Reranked results sorted by score (descending)

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class RerankResult(index: Int, score: Double, document: String)

Result of reranking a single document.

Result of reranking a single document.

Value parameters

document

The original document content

index

Original index in the input documents

score

Relevance score from reranker (higher is better, typically 0-1)

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
trait Reranker

Cross-encoder reranker interface.

Cross-encoder reranker interface.

Rerankers improve search quality by re-scoring candidate documents using a cross-encoder model that sees both query and document together. This produces more accurate relevance scores than bi-encoder (embedding) similarity alone.

Usage:

val reranker = RerankerFactory.cohere(config)
val request = RerankRequest(
 query = "What is Scala?",
 documents = Seq("Scala is a programming language", "Python is popular"),
 topK = Some(5)
)
val response = reranker.rerank(request)
response.foreach { r =>
 r.results.foreach(rr => println(s"Score $${rr.score}: $${rr.document.take(50)}..."))
}

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes

Factory for creating Reranker instances.

Factory for creating Reranker instances.

Supports multiple backends:

  • Cohere: Cloud-based cross-encoder reranking API
  • LLM: Use existing LLMClient for reranking with structured prompts
  • None: Passthrough that preserves original order

Usage:

// Cohere reranker from explicit config
val reranker = RerankerFactory.cohere(config)

// LLM-based reranker
val llmReranker = RerankerFactory.llm(llmClient)

// From provided config
val reranker = RerankerFactory.fromConfig(Some(config))

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type