rag.core.query

Query / chat engine over the papers Chroma index.

Attributes

Classes

ProseNodePostprocessor

Keep retrieved chunks that look like readable paper prose.

QueryResult

Answer plus grounding citations.

Functions

require_openai_api_key(→ str)

Load .env if present and return OPENAI_API_KEY.

build_llm(→ llama_index.llms.openai.OpenAI)

Construct the OpenAI chat LLM.

build_query_engine(...)

Build a retrieval-augmented query engine.

ask_llamaindex(→ QueryResult)

Ask a paper-level question using the LlamaIndex query engine.

ask(→ QueryResult)

Ask a question against the papers index or the full corpus catalog.

Module Contents

rag.core.query.RETRIEVAL_OVERFETCH: Final = 3[source]
rag.core.query.PAPERS_TEXT_QA_TEMPLATE[source]
rag.core.query.PAPERS_REFINE_TEMPLATE[source]
class rag.core.query.ProseNodePostprocessor(/, **data: Any)[source]

Bases: llama_index.core.postprocessor.types.BaseNodePostprocessor

Keep retrieved chunks that look like readable paper prose.

Author questions drop acknowledgement / thanks chunks and prefer earlier pages (title-page author lists over end-matter).

Variables:

keep (int) – Maximum number of prose nodes to retain after filtering.

keep: int = None[source]
class rag.core.query.QueryResult[source]

Answer plus grounding citations.

Variables:
  • answer (str) – Model response text.

  • citations (list of Citation) – Retrieved sources.

  • citations_markdown (str) – Pre-formatted markdown for UIs.

answer: str[source]
citations: list[rag.core.citations.Citation][source]
citations_markdown: str[source]
rag.core.query.require_openai_api_key() str[source]

Load .env if present and return OPENAI_API_KEY.

Returns:

API key value.

Return type:

str

Raises:

EnvironmentError – If the key is missing or empty.

rag.core.query.build_llm(*, config: rag.core.config.RagConfig, api_key: str | None = None) llama_index.llms.openai.OpenAI[source]

Construct the OpenAI chat LLM.

Parameters:
  • config (RagConfig) – Provides llm_model_name.

  • api_key (str or None) – Explicit key; otherwise read from the environment.

Returns:

LlamaIndex OpenAI LLM wrapper.

Return type:

OpenAI

rag.core.query.build_query_engine(*, config: rag.core.config.RagConfig, embed_model: llama_index.core.embeddings.BaseEmbedding | None = None, llm: Any | None = None, index: llama_index.core.VectorStoreIndex | None = None) llama_index.core.query_engine.BaseQueryEngine[source]

Build a retrieval-augmented query engine.

Parameters:
  • config (RagConfig) – Retrieval and model settings.

  • embed_model (BaseEmbedding or None) – Defaults to the HuggingFace model from config.

  • llm (Any or None) – Defaults to OpenAI from config + env key.

  • index (VectorStoreIndex or None) – Defaults to loading the persistent Chroma index.

Returns:

Configured LlamaIndex query engine.

Return type:

BaseQueryEngine

rag.core.query.CORPUS_SYNTHESIS_PROMPT: Final = Multiline-String[source]
Show Value
"""You are summarizing a researcher's full PDF library.
The catalog below is the COMPLETE set of papers ({paper_count} files), not a retrieval sample.
---------------------
{catalog_markdown}
---------------------
If the files span multiple application domains (medical imaging, networks, environment, finance, news, engineering, …) but share supervised learning, deep learning, graph ML, or similar methods, the unifying theme is applied machine learning. State that umbrella first, then name several distinct domains from the filenames/titles. Do not treat one paper as the whole corpus.
Question: {query_str}
Answer: """
rag.core.query.AUTHOR_SYNTHESIS_PROMPT: Final = Multiline-String[source]
Show Value
"""You are identifying authors from a researcher's PDF library.
The catalog below is the COMPLETE set of papers ({paper_count} files).
Each line is a filename plus opening-page text (title and usually authors).
---------------------
{catalog_markdown}
---------------------
SPIE footers like 'edited by' name volume editors, not the paper's authors. People thanked for revising a manuscript are not authors. If one person is first author or appears on most papers, they are the main author of this library — say so, then name frequent co-authors. If the question names a specific paper or topic, answer for that paper only. Do not invent names that are not in the catalog.
Question: {query_str}
Answer: """
rag.core.query.ask_llamaindex(*, question: str, config: rag.core.config.RagConfig, query_engine: llama_index.core.query_engine.BaseQueryEngine | None = None) QueryResult[source]

Ask a paper-level question using the LlamaIndex query engine.

Parameters:
  • question (str) – User question.

  • config (RagConfig) – Used when constructing a default engine.

  • query_engine (BaseQueryEngine or None) – Injected engine (tests / Streamlit cache).

Returns:

Answer and citations.

Return type:

QueryResult

rag.core.query.ask(*, question: str, config: rag.core.config.RagConfig, query_engine: llama_index.core.query_engine.BaseQueryEngine | None = None, catalog: rag.core.catalog.PaperCatalog | None = None, llm: Any | None = None) QueryResult[source]

Ask a question against the papers index or the full corpus catalog.

Corpus-level questions (for example “common topic among all papers”) skip vector retrieval and synthesize from the paper catalog. Author questions use the same catalog (opening-page title/author snippets) instead of similarity hits, which often match SPIE volume editors.

Parameters:
  • question (str) – User question.

  • config (RagConfig) – Used when constructing a default engine or loading the catalog.

  • query_engine (BaseQueryEngine or None) – Injected LlamaIndex engine (tests / Streamlit cache).

  • catalog (PaperCatalog or None) – Injected catalog; otherwise loaded from disk or papers_dir.

  • llm (Any or None) – Injected chat LLM for corpus synthesis.

Returns:

Answer and citations.

Return type:

QueryResult