rag¶
duallens_analytics.rag
¶
RAG pipeline – retrieval + LLM generation.
This module implements the core Retrieval-Augmented Generation loop:
- Accept a user question.
- Retrieve the top-k relevant document chunks from the ChromaDB vector store.
- Assemble a prompt that injects the retrieved context.
- Invoke the Chat LLM and return the generated answer together with the source excerpts.
QNA_SYSTEM_MESSAGE = "You are an assistant specialised in reviewing AI initiatives of companies and providing accurate answers based on the provided context.\n\nUser input will include all the context you need to answer their question.\nThis context will always begin with the token: ###Context.\nThe context contains references to specific AI initiatives, projects, or programmes of companies relevant to the user's question.\n\nInstructions:\n- Answer ONLY based on the context provided.\n- If the context is insufficient, clearly state that.\n- Your response should be well-structured and concise.\n"
module-attribute
¶
System message instructing the LLM to answer only from provided context.
QNA_USER_TEMPLATE = '###Context\nHere are some documents that are relevant to the question mentioned below.\n{context}\n\n###Question\n{question}\n'
module-attribute
¶
User-message template with {context} and {question} placeholders.
get_llm(settings)
¶
Return a configured :class:~langchain_openai.ChatOpenAI instance.
All LLM hyper-parameters (model, temperature, max_tokens, etc.) are
read from the supplied :class:~duallens_analytics.config.Settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings
|
Application settings. |
required |
Returns:
| Type | Description |
|---|---|
ChatOpenAI
|
A ready-to-invoke |
Source code in src/duallens_analytics/rag.py
query_rag(question, retriever, settings)
¶
Execute the full RAG loop: retrieve context then generate an answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
Natural-language question from the user. |
required |
retriever
|
VectorStoreRetriever
|
A LangChain retriever backed by the ChromaDB vector
store (see :func: |
required |
settings
|
Settings
|
Application settings forwarded to :func: |
required |
Returns:
| Type | Description |
|---|---|
str
|
A tuple of |
list[str]
|
source_excerpts is a list of the raw |
tuple[str, list[str]]
|
from the retrieved document chunks. |