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.1.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-0.1.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rag_module-0.1.1.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-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8265c03770943cf37e39835caf9eeefdbfd0fa48982ee2b96fce65dd102d19b9
MD5 fe3deb89856d60def893fd269f7c9f37
BLAKE2b-256 a8f223e4a91aa209f70fd83207b5bda703d2d91e96c8811a118f5def288006dd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rag_module-0.1.1-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-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66cfd5f1e86666470e1cdf8b94109d617717e3e3cd92ac6c379a1518cc60a143
MD5 00f425981c3f0a30d2ea17c374884769
BLAKE2b-256 2b65919de5db17a79be39404f3f6831b2372d02ae5adbbf5146f3b874ad9be06

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