Overview
The context-window library provides a simple, type-safe API for building RAG (Retrieval-Augmented Generation) applications. The API is designed to be intuitive while offering powerful customization options.Installation
Basic Import
All main functions are exported from the root package:Quick Start
The simplest way to use context-window:API Patterns
context-window offers two main patterns for managing context windows:Direct Pattern
Create and use a context window directly:Best for: Single-use scenarios, scripts, simple applications
Registry Pattern
Create, register, and retrieve context windows by name:Best for: Multiple context windows, complex applications, modular architectures
Core Concepts
Context Window
A Context Window is an instance that manages:- Document ingestion and chunking
- Vector embeddings and storage
- Semantic search and retrieval
- Answer generation with source citations
Index Name
Each context window requires a unique index name that:- Identifies the Pinecone namespace for data isolation
- Serves as the key for the registry pattern
- Should be descriptive (e.g.,
"user-manual","api-docs")
Idempotency
context-window is idempotent by design:- Same files → same chunk IDs → updates instead of duplicates
- Safe to re-run ingestion without creating redundant data
- Content-based hashing ensures consistency
API Organization
Core Functions
Main functions for creating and managing context windows
Configuration
Detailed configuration options for AI, storage, chunking, and limits
Utility Functions
Helper functions for the registry pattern
ask() Method
Query your context window for answers
Type Safety
context-window is written in TypeScript with full type definitions:Environment Variables
Required environment variables:| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY | Yes | OpenAI API key for embeddings and completions |
PINECONE_API_KEY | Yes | Pinecone API key for vector storage |
PINECONE_INDEX | No | Default Pinecone index name (default: "context-window") |
PINECONE_ENVIRONMENT | No | Pinecone region (default: "us-east-1") |
Error Handling
All functions use standard error handling patterns:- Invalid API keys: Check your
.envconfiguration - Index not found: Ensure your Pinecone index exists
- Incorrect dimensions: Pinecone index must have 1536 dimensions
- File not found: Verify file paths in the
dataparameter