Skip to main content

A simple RAG module with in-memory vector store and OpenAI integration

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rag_module-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rag_module-0.1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file rag_module-0.1.0.tar.gz.

File metadata

  • Download URL: rag_module-0.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rag_module-0.1.0.tar.gz
Algorithm Hash digest
SHA256 96421e25f6a736baddf107f99d56ea92eccd3e99438b053139a3d47e049b5217
MD5 72b8585c2492f93aa2b56d649b9bdcf7
BLAKE2b-256 d06c55f34cd815e8052fbef5451e18e945ffee1b47c04078d5dd7e1c5bb23895

See more details on using hashes here.

File details

Details for the file rag_module-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rag_module-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rag_module-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb9e236f5badb1ef3dcbdeb8f0be04035980d09ef804d07a3337d8276070e963
MD5 b63f8aed1d195a5c11192f8346d2ce5b
BLAKE2b-256 384407ddf2b8b5d586586747e514f287ab05f7203a0f635422b8c791fdf4bfd4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page