xbe.at

LLMs and generative AI / RAG and retrieval

The Limitations of Retrieval-Augmented Generation (RAG) Systems

8 min read, 14 sections

Contents
  1. The Reality of RAG Systems
  2. Data Processing Challenges
  3. The Art of Chunking
  4. Retrieval Techniques: Beyond Vector Search
  5. Query Rewriting for Improved Retrieval
  6. Hypothetical Document Embedding (HyDE)
  7. LLM-based Irrelevant Chunk Removal
  8. Optimizing Chunk Quality
  9. Real-life Example: Question Answering System
  10. Real-life Example: Content Recommendation
  11. Performance Considerations in Production
  12. Cost Considerations and Optimization
  13. Continuous Improvement and Monitoring
  14. Additional Resources

The Reality of RAG Systems

Retrieval-Augmented Generation (RAG) systems are not the plug-and-play solution many expect. While they offer exciting possibilities for utilizing Large Language Models (LLMs) with external knowledge, implementing them effectively requires careful consideration and optimization at every step.

import numpy as np
import matplotlib.pyplot as plt

# Simulating RAG system components
components = ['Data Processing', 'Chunking', 'Retrieval', 'Generation']
complexity = np.array([0.7, 0.8, 0.9, 0.85])

plt.figure(figsize=(10, 6))
plt.bar(components, complexity)
plt.title('Complexity of RAG System Components')
plt.ylabel('Relative Complexity')
plt.ylim(0, 1)
plt.show()

Data Processing Challenges

Effective data processing is crucial for RAG systems. It involves cleaning, structuring, and preparing data for efficient retrieval. Poorly processed data can lead to irrelevant or inaccurate responses.

import pandas as pd

# Sample data processing function
def process_data(raw_data):
    # Remove duplicates
    cleaned_data = raw_data.drop_duplicates()
    
    # Handle missing values
    cleaned_data = cleaned_data.fillna('Unknown')
    
    # Normalize text (lowercase)
    cleaned_data['text'] = cleaned_data['text'].str.lower()
    
    return cleaned_data

# Example usage
raw_data = pd.DataFrame({'text': ['Example 1', 'EXAMPLE 2', 'example 1', None]})
processed_data = process_data(raw_data)
print(processed_data)

The Art of Chunking

Chunking involves breaking down large documents into smaller, manageable pieces. The challenge lies in determining the optimal chunk size and ensuring that each chunk maintains coherence and context.

def chunk_text(text, chunk_size=100, overlap=20):
    words = text.split()
    chunks = []
    for i in range(0, len(words), chunk_size - overlap):
        chunk = ' '.join(words[i:i + chunk_size])
        chunks.append(chunk)
    return chunks

# Example usage
text = "This is a long document that needs to be chunked into smaller pieces for efficient retrieval and processing in a RAG system."
chunked_text = chunk_text(text)
for i, chunk in enumerate(chunked_text):
    print(f"Chunk {i + 1}: {chunk}")

Retrieval Techniques: Beyond Vector Search

While vector search is popular, it's not the only retrieval technique available. Consider exploring other methods like BM25, TF-IDF, or hybrid approaches combining multiple techniques for more robust retrieval.

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

def tfidf_retrieval(query, documents):
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform(documents + [query])
    cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
    most_similar_idx = cosine_similarities.argsort()[0][-1]
    return documents[most_similar_idx]

# Example usage
documents = [
    "The quick brown fox jumps over the lazy dog.",
    "A journey of a thousand miles begins with a single step.",
    "To be or not to be, that is the question."
]
query = "What animal is quick and brown?"
result = tfidf_retrieval(query, documents)
print(f"Most relevant document: {result}")

Query Rewriting for Improved Retrieval

Query rewriting can significantly enhance retrieval performance by expanding or refining the original query. This technique helps bridge the gap between user intent and available information.

import openai

def rewrite_query(query):
    prompt = f"Rewrite the following query to improve retrieval results: '{query}'"
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=50
    )
    return response.choices[0].text.strip()

# Example usage
original_query = "laptop battery life"
rewritten_query = rewrite_query(original_query)
print(f"Original query: {original_query}")
print(f"Rewritten query: {rewritten_query}")

Hypothetical Document Embedding (HyDE)

HyDE is an innovative technique that generates a hypothetical perfect document for a given query, which is then used to improve retrieval. This approach can lead to more relevant and context-aware results.

import openai
from sentence_transformers import SentenceTransformer

def generate_hyde_document(query):
    prompt = f"Generate a hypothetical document that perfectly answers the query: '{query}'"
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=200
    )
    return response.choices[0].text.strip()

def hyde_retrieval(query, documents):
    model = SentenceTransformer('all-MiniLM-L6-v2')
    hyde_doc = generate_hyde_document(query)
    
    embeddings = model.encode(documents + [hyde_doc])
    similarities = embeddings[:-1] @ embeddings[-1].T
    most_similar_idx = similarities.argsort()[-1]
    
    return documents[most_similar_idx]

# Example usage
query = "What are the benefits of regular exercise?"
documents = [
    "Regular exercise improves cardiovascular health and muscle strength.",
    "A balanced diet is essential for maintaining good health.",
    "Sleep is crucial for mental and physical well-being."
]
result = hyde_retrieval(query, documents)
print(f"Most relevant document using HyDE: {result}")

LLM-based Irrelevant Chunk Removal

Leveraging LLMs to filter out irrelevant chunks can significantly improve the quality of retrieved information. This step helps reduce noise and focuses on the most pertinent content.

import openai

def is_chunk_relevant(chunk, query):
    prompt = f"Is the following chunk relevant to the query '{query}'? Respond with 'Yes' or 'No':\n\nChunk: {chunk}"
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=1
    )
    return response.choices[0].text.strip().lower() == 'yes'

def filter_chunks(chunks, query):
    return [chunk for chunk in chunks if is_chunk_relevant(chunk, query)]

# Example usage
chunks = [
    "The Earth revolves around the Sun.",
    "Photosynthesis is the process by which plants make food.",
    "The capital of France is Paris."
]
query = "Tell me about the solar system"
filtered_chunks = filter_chunks(chunks, query)
print("Relevant chunks:")
for chunk in filtered_chunks:
    print(f"- {chunk}")

Optimizing Chunk Quality

Improving chunk quality involves considering factors such as size, coherence, and relevance. Properly optimized chunks can lead to more accurate and contextually appropriate responses from the RAG system.

import nltk
from nltk.tokenize import sent_tokenize

nltk.download('punkt')

def create_semantic_chunks(text, max_chunk_size=150):
    sentences = sent_tokenize(text)
    chunks = []
    current_chunk = ""
    
    for sentence in sentences:
        if len(current_chunk) + len(sentence) <= max_chunk_size:
            current_chunk += " " + sentence
        else:
            if current_chunk:
                chunks.append(current_chunk.strip())
            current_chunk = sentence
    
    if current_chunk:
        chunks.append(current_chunk.strip())
    
    return chunks

# Example usage
text = "RAG systems combine retrieval and generation. They use external knowledge to enhance LLM responses. Proper implementation requires careful optimization. Each component plays a crucial role in the system's performance."
semantic_chunks = create_semantic_chunks(text)
for i, chunk in enumerate(semantic_chunks):
    print(f"Chunk {i + 1}: {chunk}")

Real-life Example: Question Answering System

Implementing a RAG-based question answering system demonstrates the practical application of these concepts. This example shows how to combine retrieval and generation for accurate answers.

import openai
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

def retrieve_relevant_chunk(query, chunks):
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform(chunks + [query])
    cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
    most_similar_idx = cosine_similarities.argsort()[0][-1]
    return chunks[most_similar_idx]

def generate_answer(query, context):
    prompt = f"Answer the following question based on the given context:\n\nContext: {context}\n\nQuestion: {query}\n\nAnswer:"
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt,
        max_tokens=100
    )
    return response.choices[0].text.strip()

# Example usage
chunks = [
    "The Great Wall of China is an ancient fortification system in northern China.",
    "The Eiffel Tower is a wrought-iron lattice tower in Paris, France.",
    "The Taj Mahal is an ivory-white marble mausoleum in Agra, India."
]
query = "Where is the Eiffel Tower located?"
relevant_chunk = retrieve_relevant_chunk(query, chunks)
answer = generate_answer(query, relevant_chunk)
print(f"Query: {query}")
print(f"Answer: {answer}")

Real-life Example: Content Recommendation

A content recommendation system using RAG principles can enhance user experience by suggesting relevant articles or products based on user preferences and behavior.

import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

def create_user_profile(user_interactions):
    return " ".join(user_interactions)

def recommend_content(user_profile, content_library):
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform(content_library + [user_profile])
    cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
    top_indices = cosine_similarities.argsort()[0][-3:][::-1]
    return [content_library[i] for i in top_indices]

# Example usage
content_library = [
    "Exploring the wonders of deep-sea marine life",
    "The art of landscape photography: Capturing nature's beauty",
    "Understanding the basics of quantum computing",
    "Sustainable gardening techniques for urban environments",
    "The impact of artificial intelligence on modern healthcare"
]

user_interactions = [
    "underwater photography tips",
    "marine biology research",
    "scuba diving destinations"
]

user_profile = create_user_profile(user_interactions)
recommendations = recommend_content(user_profile, content_library)

print("Recommended content:")
for i, content in enumerate(recommendations, 1):
    print(f"{i}. {content}")

Performance Considerations in Production

Implementing RAG systems in production requires careful attention to performance optimization. Factors such as response time, scalability, and resource utilization must be considered to ensure a smooth user experience.

import time
import concurrent.futures

def simulate_retrieval(query):
    time.sleep(0.1)  # Simulating network latency
    return f"Retrieved result for: {query}"

def simulate_generation(context):
    time.sleep(0.2)  # Simulating LLM processing time
    return f"Generated response based on: {context}"

def process_query(query):
    retrieved_result = simulate_retrieval(query)
    generated_response = simulate_generation(retrieved_result)
    return generated_response

def process_queries_parallel(queries):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        results = list(executor.map(process_query, queries))
    return results

# Example usage
queries = ["Query 1", "Query 2", "Query 3", "Query 4", "Query 5"]

start_time = time.time()
results = process_queries_parallel(queries)
end_time = time.time()

print(f"Processed {len(queries)} queries in {end_time - start_time:.2f} seconds")
for query, result in zip(queries, results):
    print(f"{query}: {result}")

Cost Considerations and Optimization

Implementing RAG systems can be cost-intensive, especially when using commercial LLMs and cloud-based infrastructure. Optimizing costs while maintaining performance is crucial for sustainable deployment.

import random

def simulate_api_call(tokens):
    # Simulating API costs: $0.02 per 1K tokens
    return (tokens / 1000) * 0.02

def optimize_chunk_size(text, max_chunk_size):
    words = text.split()
    total_cost = 0
    chunks = []
    
    for i in range(0, len(words), max_chunk_size):
        chunk = ' '.join(words[i:i + max_chunk_size])
        chunks.append(chunk)
        total_cost += simulate_api_call(len(chunk))
    
    return len(chunks), total_cost

# Example usage
text = " ".join(["word" for _ in range(10000)])  # Simulating a large document

chunk_sizes = [50, 100, 200, 500]
results = []

for size in chunk_sizes:
    num_chunks, cost = optimize_chunk_size(text, size)
    results.append((size, num_chunks, cost))

for size, num_chunks, cost in results:
    print(f"Chunk size: {size}, Number of chunks: {num_chunks}, Estimated cost: ${cost:.2f}")

Continuous Improvement and Monitoring

RAG systems require ongoing monitoring and improvement to maintain their effectiveness. Implementing feedback loops and performance metrics helps identify areas for enhancement and ensures the system remains reliable over time.

import random
import matplotlib.pyplot as plt

def simulate_rag_performance(days):
    base_accuracy = 0.8
    accuracies = []
    
    for day in range(days):
        daily_accuracy = base_accuracy + random.uniform(-0.05, 0.05)
        accuracies.append(daily_accuracy)
        
        # Simulating system improvements
        if day % 7 == 0 and base_accuracy < 0.95:
            base_accuracy += 0.01
    
    return accuracies

def plot_performance(accuracies):
    plt.figure(figsize=(10, 6))
    plt.plot(accuracies)
    plt.title('RAG System Performance Over Time')
    plt.xlabel('Days')
    plt.ylabel('Accuracy')
    plt.ylim(0.7, 1.0)
    plt.show()

# Simulate performance for 30 days
performance_data = simulate_rag_performance(30)
plot_performance(performance_data)

# Calculate improvement
initial_accuracy = performance_data[0]
final_accuracy = performance_data[-1]
improvement = (final_accuracy - initial_accuracy) / initial_accuracy * 100

print(f"Initial accuracy: {initial_accuracy:.2f}")
print(f"Final accuracy: {final_accuracy:.2f}")
print(f"Improvement: {improvement:.2f}%")

Additional Resources

For those interested in diving deeper into RAG systems and their implementation, consider exploring the following resources:

  1. "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" (arXiv:2005.11401) https://arxiv.org/abs/2005.11401
  2. "In-Context Retrieval-Augmented Language Models" (arXiv:2302.00083) https://arxiv.org/abs/2302.00083
  3. "Self-RAG: Learning to Retrieve, Generate, and Critique through Self-Reflection" (arXiv:2310.11511) https://arxiv.org/abs/2310.11511
  4. "Benchmarking Large Language Models for News Summarization" (arXiv:2301.13848) https://arxiv.org/abs/2301.13848

These papers provide in-depth analyses of RAG systems, their applications, and potential improvements. They offer valuable insights for both researchers and practitioners in the field of natural language processing and information retrieval.

More in RAG and retrieval

  1. Tree-RAG (T-RAG), an enhanced RAG technique with Python10 min

    Tree-RAG (T-RAG) is an enhanced Retrieval-Augmented Generation (RAG) technique that leverages tree structures to improve information retrieval and...

  2. Retrieval-Augmented Generation (RAG) vs. HyDE Revolutionizing Text Generation19 min

    A foundational implementation of Retrieval-Augmented Generation using vector embeddings and cosine similarity for document retrieval. This base...

  3. Understanding RAG vs. Agentic RAG with Python11 min

    Retrieval-Augmented Generation (RAG) is a powerful technique that combines the strengths of large language models with external knowledge retrieval. It...

  4. Retrieval Techniques in RAG Sparse and Dense Vector Tools9 min

    Retrieval-Augmented Generation (RAG) combines information retrieval with text generation. This presentation focuses on two key retrieval techniques: sparse...

  5. Unveiling Inconsistencies in Retrieval-Augmented Language Models with EoR Using Python9 min

    Retrieval-Augmented Language Models (RALMs) combine the power of large language models with external knowledge retrieval. This approach allows models to...

  6. Retrieval Augmented Generation (RAG) in Large Language Models16 min

    A comprehensive implementation of core RAG system components including document loading, text chunking, and embedding generation using modern Python...