Skip to main content

Overview

The Simple Semantic RAG module implements a basic RAG (Retrieval-Augmented Generation) pipeline. It uses a semantic retriever to find relevant documents in a ChromaDB vector store and then uses a language model to generate an answer based on the retrieved context. Module: src.rag.simple Source: src/rag/simple.py

Configuration

Default Models

Vector Store

Retriever Configuration

Retrieves the top 5 most similar documents using semantic search.

Prompt Template

The module uses a medical-focused prompt template:

Functions

format_docs

Formats the retrieved documents to be included in the final prompt.
List[Document]
required
A list of retrieved LangChain Document objects
str
A formatted string containing the content of the documents with source and page metadata
Example Output Format:

process_semantic_query

Processes a query using the simple semantic RAG pipeline.
str
required
The user’s question
ChatOpenAI
default:"None"
Custom LLM to use. If None, uses default llm (gpt-4o)
Dict[str, Any]
A dictionary containing:
  • answer (str): The generated answer
  • contexts (List[str]): List of retrieved document contents
  • retrieved_documents (List[Document]): Full Document objects
  • metrics (dict): Token usage and cost metrics
    • input_tokens (int): Number of input tokens
    • output_tokens (int): Number of output tokens
    • total_tokens (int): Total tokens used
    • usage_source (str): Source of usage data
    • cost (float): Total cost in USD
    • cost_source (str): Source of cost calculation

query_for_evaluation

A wrapper function for RAG evaluation frameworks like Ragas. This function processes a question and returns a dictionary structured for easy integration with evaluation tools.
str
required
The question to process
str
default:"None"
Model name to use. If None, uses default “gpt-4o”
BaseChatModel
default:"None"
Pre-configured language model. Takes precedence over llm_model
dict
A dictionary containing:
  • question (str): The original question
  • answer (str): The generated answer
  • contexts (List[str]): Retrieved document contents
  • source_documents (List[Document]): Full retrieved documents
  • metadata (dict): Comprehensive metadata including:
    • num_contexts (int): Number of retrieved contexts
    • retrieval_method (str): “semantic_only”
    • llm_model (str): Model name used
    • provider (str): Provider (e.g., “openai”)
    • model_id (str): Full model identifier
    • embedding_model (str): “text-embedding-3-small”
    • execution_time (float): Total execution time in seconds
    • input_tokens (int): Input tokens used
    • output_tokens (int): Output tokens generated
    • total_cost (float): Total cost in USD
    • tokens_used (int): Total tokens (input + output)
    • usage_source (str): Source of usage metrics
    • cost_source (str): Source of cost calculation

Usage Example

Pipeline Flow

  1. Retrieve: Uses semantic search to find the top 5 most relevant documents from ChromaDB
  2. Format: Formats documents with source and page metadata
  3. Generate: Uses the LLM to generate an answer based on the retrieved context
  4. Track: Captures token usage and cost metrics

Key Features

  • Simple and straightforward semantic search
  • Automatic cost and token tracking
  • Support for custom LLMs
  • Medical domain-specific prompting
  • Structured output for evaluation frameworks