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-1.0.0.tar.gz (15.7 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-1.0.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for rag_module-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ed130940bb14dae6046552741e7e16ecae4059d3d8d9ddb3a7ce46163e778adc
MD5 37b27fdf563d3108fc9b4aa39b9d1225
BLAKE2b-256 066a3c2547bed28ad767bd7fb636a9cc37c89f7386f297b070ea2d4b9155548c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rag_module-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f4798218ce23f6e78c5ba5a3417f72a6f9d67dea6a6327c1c50ba23779fe7bd
MD5 78d6679dc96820ec15fb53cb3826189b
BLAKE2b-256 4e931f1c2d75fb3c5ca6ea2165d2e8b4f8d67b6f6b069430721d78469696d89b

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