Skip to main content

RAG Module

A simple RAG (Retrieval-Augmented Generation) module with an in-memory vector store and OpenAI integration.

Installation

pip install rag-module

Requirements

  • Python 3.12+
  • OpenAI API key

Quick Start

from rag_module import RAG

# Initialize with your OpenAI API key
rag = RAG(api_key="your-api-key")

# Add documents
rag.add_text("Python is a programming language created by Guido van Rossum.")
rag.add_markdown("# Machine Learning\n\nML is a subset of artificial intelligence.")

# Load files
rag.load_file("docs/guide.md")
rag.load_directory("docs/", extensions=[".md", ".txt"])

# Query
response = rag.query("What is Python?")
print(response.answer)
print(response.sources)

Components

RAG

The main class that orchestrates document loading, embedding, and querying.

from rag_module import RAG

rag = RAG(
    api_key="your-api-key",           # Or set OPENAI_API_KEY env var
    embedding_model="text-embedding-3-small",
    chat_model="gpt-4o-mini",
    chunk_size=1000,
    chunk_overlap=200,
)

Document

Represents a piece of text with metadata.

from rag_module import Document

doc = Document(
    content="Hello world",
    metadata={"source": "example.txt"},
    id="doc-1"
)

VectorStore

In-memory vector store using cosine similarity.

from rag_module import VectorStore

store = VectorStore()
store.add(documents, embeddings)
results = store.search(query_embedding, top_k=5)

TextChunker

Splits documents into smaller chunks.

from rag_module import TextChunker

chunker = TextChunker(chunk_size=1000, chunk_overlap=200)
chunks = chunker.chunk(document)

Loaders

Load documents from files or strings.

from rag_module import TextLoader, MarkdownLoader

# Text files
loader = TextLoader()
doc = loader.load("file.txt")
doc = loader.load_from_string("content", source="inline")

# Markdown files
md_loader = MarkdownLoader(split_by_headers=True)
docs = md_loader.load("file.md")

OpenAIEmbeddings

Generate embeddings using OpenAI.

from rag_module import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector = embeddings.embed("Hello world")
vectors = embeddings.embed_batch(["Hello", "World"])

Development

# Install dev dependencies
make install-dev

# Run tests
make test

# Format code
make format

# Type check
make typecheck

# Build package
make build

License

MIT

Metadata

Release files for rag-module 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rag-module 1.0.0
File Size Uploaded
rag_module-1.0.0.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rag-module 1.0.0
File Interpreter ABI Platform
rag_module-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / rag_module-1.0.0.tar.gz

Download URL rag_module-1.0.0.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ed130940bb14dae6046552741e7e16ecae4059d3d8d9ddb3a7ce46163e778adc
BLAKE2b-256 checksum
How to use checksums
066a3c2547bed28ad767bd7fb636a9cc37c89f7386f297b070ea2d4b9155548c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / rag_module-1.0.0-py3-none-any.whl

Download URL rag_module-1.0.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f4798218ce23f6e78c5ba5a3417f72a6f9d67dea6a6327c1c50ba23779fe7bd
BLAKE2b-256 checksum
How to use checksums
4e931f1c2d75fb3c5ca6ea2165d2e8b4f8d67b6f6b069430721d78469696d89b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page