fastrag.query.engine#

The QueryEngine: retrieval plus generation, one question at a time.

Classes

Answer(text, sources)

One generated answer, with its sources.

DoneChunk(answer)

The stream is finished; carries the assembled Answer.

QueryEngine(*, retriever, llm[, prompt])

Answers one question by retrieving, then generating.

SourcesChunk(sources)

The retrieved context, sent once before the first TokenChunk.

TokenChunk(token)

One text delta from the chat model.

class fastrag.query.engine.QueryEngine(*, retriever, llm, prompt=PromptTemplate(template='Documents:\n{context}\n\nQuestion: {question}\n\nAnswer:', system="Answer the question in its own language, using only the documents provided. If they do not contain the answer, say so.\n\nPrefer documents written in the language of the question. Use a document in another language only for what none of those say. When several documents say the same thing, such as one page in different languages, use only the one in the language of the question.\n\nDo not cite documents inside the text. End the answer with one line that lists the number of every document you used, in square brackets and each only once, like [1][3]. Never write a document's number anywhere else in the answer."))[source]#

Bases: object

Answers one question by retrieving, then generating.

Takes a Retriever, not a VectorStore — so every retrieval strategy (VectorRetriever, RerankingRetriever) is available to the engine without it knowing any of them exist, and there is exactly one place retrieval is configured.

import os

from fastrag.query import QueryEngine
from fastrag.query.llm import OpenAIChatModel

engine = QueryEngine(
    retriever=retriever,
    llm=OpenAIChatModel(
        model=os.environ["FASTRAG_CHAT_MODEL"],
        api_key=os.environ["FASTRAG_CHAT_API_KEY"],
        base_url=os.environ["FASTRAG_CHAT_BASE_URL"],
    ),
)

answer = await engine.ask("What is a data space?")
print(answer.text, [source.id for source in answer.sources])
Parameters:
  • retriever (Retriever) – Where the context for each question comes from.

  • llm (ChatModel) – Generates the answer from the rendered prompt.

  • prompt (PromptTemplate) – Turns a question and its retrieved context into messages.

async ask(question, history=())[source]#

Retrieve, then generate the whole answer at once.

history is inserted into the messages sent to llm as prior turns, so a follow-up question can resolve against earlier context. It is sent to the model only — retrieval embeds question alone.

Parameters:
Return type:

Answer

async stream(question, history=())[source]#

Retrieve, then yield the answer as it generates.

Sources first, then one TokenChunk per delta, then a DoneChunk.

history is inserted into the messages sent to llm as prior turns, so a follow-up question can resolve against earlier context. It is sent to the model only — retrieval embeds question alone.

Parameters:
Return type:

AsyncIterator[SourcesChunk | TokenChunk | DoneChunk]

class fastrag.query.engine.Answer(text, sources)[source]#

Bases: object

One generated answer, with its sources.

Parameters:
class fastrag.query.engine.SourcesChunk(sources)[source]#

Bases: object

The retrieved context, sent once before the first TokenChunk.

Parameters:

sources (list[Document])

class fastrag.query.engine.TokenChunk(token)[source]#

Bases: object

One text delta from the chat model.

Parameters:

token (str)

class fastrag.query.engine.DoneChunk(answer)[source]#

Bases: object

The stream is finished; carries the assembled Answer.

Parameters:

answer (Answer)