$ show patterns-ai-core/langchainrb
Langchain.rb is a Ruby gem for building LLM-powered applications with a unified interface to multiple LLM providers and vector databases. It provides a modular architecture supporting Retrieval Augmented Generation (RAG), AI agents with tool usage, document processing, and prompt templating. The gem includes a Rails engine for deep Rails integration and is actively maintained with frequent updates. The system enables developers to create interactive AI applications by combining LLMs, external tools, and vector search capabilities.
The LLM identified 5 files worth highlighting for reuse:
Manages optional dependencies for LLM providers and vector stores, loading them only when needed. Reduces gem footprint and avoids version conflicts. Self-contained and reusable in any project requiring conditional dependency loading.
Parses and validates LLM output against a JSON schema, with automatic correction attempts for invalid outputs. Provides a reliable way to extract structured data from LLMs. Isolated, demonstrates skill in handling LLM unreliability, and reusable in any structured output scenario.
Chunks text based on semantic similarity using embeddings, creating coherent chunks that preserve meaning. More effective than fixed-size or sentence-based chunking for RAG. Self-contained, depends only on embedder and cosine similarity, and reusable in text processing pipelines.
Defines the tool interface for AI agents, requiring a name, description, JSON schema parameters, and an execute method. Enables agents to use external tools like calculators or APIs. Demonstrates a clean, extensible pattern for agent-tool interaction and is reusable in any agent framework.
Provides a common interface for all vector search implementations (Chroma, Pinecone, Qdrant, etc.) with methods like add_texts and similarity_search. Enables switching vector stores without changing application code. Exemplifies the strategy pattern and is highly reusable.