RAGAnswerContext

org.llm4s.rag.RAGAnswerContext
See theRAGAnswerContext companion object
@Experimental
final case class RAGAnswerContext

How RAG turns the retrieved chunks into the prompt an answer is generated from (#3024): which near-duplicates it drops, how many tokens the prompt may take, how many it counts with, and how each chunk is written.

In order, for queryWithAnswer, queryWithPermissionsAndAnswer and their streaming forms:

  1. deduplication drops a chunk whose text repeats a higher-ranked one's (RAGAnswerResult.droppedAsDuplicates);
  2. the formatter writes each remaining chunk, and the pipeline fences what it writes as data;
  3. the budget drops chunks, lowest-ranked first, until the system prompt, the history, the question and the chunks fit (RAGAnswerResult.droppedForBudget). When not even the highest-ranked chunk fits, the answer is a ContextError (tokenBudget) and the model is not called.
val config = RAGConfig.default.withAnswerContext(
 RAGAnswerContext.default
   .withTokenBudget(ContextTokenBudget.Tokens(6000))
   .withDeduplication(ContextDeduplication.ExactText)
)

Value parameters

deduplication

which repeated chunks are dropped before budgeting

formatter

how each chunk is written into the prompt

tokenBudget

how many tokens the prompt may take; by default the model's context window less the reply

tokenizer

the vocabulary the prompt's tokens are counted in: cl100k_base by default, the common approximation for a model whose tokenizer is not public. The budget's headroom absorbs the difference from the model's own count.

Attributes

Companion
object
Source
RAGAnswerContext.scala
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

Which repeated chunks are dropped.

Which repeated chunks are dropped.

Attributes

Source
RAGAnswerContext.scala

How each chunk is written into the prompt; the pipeline fences what it returns.

How each chunk is written into the prompt; the pipeline fences what it returns.

Attributes

Source
RAGAnswerContext.scala

The prompt's token budget.

The prompt's token budget.

Attributes

Source
RAGAnswerContext.scala

The vocabulary tokens are counted in, one org.llm4s.context.tokens.Tokenizer bundles.

The vocabulary tokens are counted in, one org.llm4s.context.tokens.Tokenizer bundles.

Attributes

Source
RAGAnswerContext.scala

Inherited methods

Attributes

Inherited from:
Product

Attributes

Inherited from:
Product