fastrag.query.engine#
The QueryEngine: retrieval plus generation, one question at a time.
Classes
|
One generated answer, with its sources. |
|
The stream is finished; carries the assembled |
|
Answers one question by retrieving, then generating. |
|
The retrieved context, sent once before the first |
|
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:
objectAnswers one question by retrieving, then generating.
Takes a
Retriever, not aVectorStore— 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.
historyis inserted into the messages sent tollmas prior turns, so a follow-up question can resolve against earlier context. It is sent to the model only — retrieval embedsquestionalone.
- async stream(question, history=())[source]#
Retrieve, then yield the answer as it generates.
Sources first, then one
TokenChunkper delta, then aDoneChunk.historyis inserted into the messages sent tollmas prior turns, so a follow-up question can resolve against earlier context. It is sent to the model only — retrieval embedsquestionalone.- Parameters:
- Return type:
- class fastrag.query.engine.Answer(text, sources)[source]#
Bases:
objectOne generated answer, with its sources.
- class fastrag.query.engine.SourcesChunk(sources)[source]#
Bases:
objectThe retrieved context, sent once before the first
TokenChunk.