function basic_rag_example
Demonstrates a basic RAG (Retrieval-Augmented Generation) workflow by initializing a DocChatRAG engine, executing a sample query about document topics, and displaying the response with metadata.
/tf/active/vicechatdev/docchat/example_usage.py
46 - 73
simple
Purpose
This function serves as a demonstration/example of how to use the DocChatRAG system in basic mode. It shows the complete workflow: initializing the RAG engine, formulating a query, executing the chat method with specific parameters (basic mode, top_k=5, reranking enabled), and displaying both the response and metadata. This is intended as a reference implementation for developers learning to use the RAG system.
Source Code
def basic_rag_example():
"""Example: Use Basic RAG mode"""
print("\n=== Basic RAG Example ===\n")
# Initialize RAG engine
rag = DocChatRAG()
# Example query
query = "What are the main topics in the documents?"
print(f"Query: {query}\n")
# Run basic RAG
result = rag.chat(
query=query,
mode="basic",
top_k=5,
use_reranking=True
)
print("Response:")
print("-" * 80)
print(result['response'])
print("-" * 80)
print(f"\nMetadata:")
print(f" Mode: {result['mode']}")
print(f" Chunks used: {result.get('num_chunks', 0)}")
Return Value
This function does not return any value (implicitly returns None). It performs side effects by printing output to the console, including the query, the RAG response, and metadata about the operation (mode used and number of chunks processed).
Dependencies
pathlibdocument_indexerrag_engineconfig
Required Imports
from pathlib import Path
from document_indexer import DocumentIndexer
from rag_engine import DocChatRAG
import config
Usage Example
from pathlib import Path
from document_indexer import DocumentIndexer
from rag_engine import DocChatRAG
import config
# Ensure documents are indexed first
# indexer = DocumentIndexer()
# indexer.index_documents()
# Run the basic RAG example
basic_rag_example()
# Output will be printed to console showing:
# - The query being asked
# - The RAG system's response
# - Metadata including mode and number of chunks used
Best Practices
- This function is intended as a demonstration only and should not be used in production code
- Ensure documents are indexed before running this example
- The function uses hardcoded query and parameters - modify these for different use cases
- Check that the DocChatRAG engine is properly initialized with required configurations
- Consider error handling when adapting this code for production use
- The function prints to console - redirect or capture output if needed in automated environments
- Verify that the config module contains all necessary settings for the RAG engine
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DocChatRAG 81.0% similar
-
function extensive_mode_example 77.2% similar
-
function full_reading_example 75.8% similar
-
function conversation_example 73.6% similar
-
function process_chat_background 70.9% similar