subhashladumor1 / larachain
LaraChain - LangChain-inspired AI orchestration framework for PHP and Laravel
Requires
- php: ^8.0
- illuminate/contracts: ^11.20|^12.0
- illuminate/support: ^11.20|^12.0
- laravel/ai: ^0.1.0
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.0|^10.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
README
LaraChain is a LangChain-inspired AI orchestration framework built specifically for Laravel 12. In 2026, building AI apps is no longer just about calling an APIβit's about building Stateful Workflows, Agentic Tools, and Modern RAG pipelines. LaraChain provides the primitives to build these with professional-grade elegance and type safety.
πΊοΈ How LaraChain Works
LaraChain follows a "Runnable" architecture where every componentβprompts, models, retrievers, and parsersβcan be piped together.
graph LR
A[Input Variables] --> B[PromptTemplate]
B -->|Pipe| C[ChatModel]
C -->|Pipe| D[OutputParser]
D --> E[Final PHP Object]
subgraph "The RAG Loop"
F[PDF/Web/CSV] --> G[TextSplitter]
G --> H[EmbeddingModel]
H --> I[VectorStore]
I -->|Retrieve| B
end
Loading
π Key Features
| Feature | Description |
|---|---|
| LCEL-style Piping | Use the .pipe() pattern to chain components like a pro. |
| Smart Agents | ReAct (Reasoning + Acting) agents that use tools and make decisions. |
| Advanced RAG | Document loaders, recursive text splitting, and vector retrieval. |
| Postgres Support | Native pgvector integration for production-ready storage. |
| Memory Drivers | Stateful conversation buffers to maintain context. |
| Laravel 12 Native | Deeply integrated with the Laravel AI SDK and Service Container. |
π Folder Structure
larachain/
βββ src/
β βββ Agents/ # ReAct and Agentic logic
β βββ Chains/ # Pipeline orchestration (Sequential, Router)
β βββ Contracts/ # Interfaces for all components
β βββ DocumentLoaders/ # Reading PDF, Web, CSV contents
β βββ Embeddings/ # Vector generation logic
β βββ Laravel/ # Service Providers and Facades
β βββ Memory/ # Conversation state management
β βββ Messages/ # Message objects (User, Assistant, System)
β βββ Models/ # AI Model wrappers (ChatModel)
β βββ Parsers/ # Output formatting (JSON, XML)
β βββ Prompts/ # Template management
β βββ Retrieval/ # Document retrieval logic
β βββ Support/ # Traits and Helpers (HasPipe)
β βββ TextSplitters/ # Document chunking logic
β βββ Toolkits/ # Grouped tools (File, Database)
β βββ Tools/ # Individual tool implementations
β βββ VectorStores/ # Storage drivers (In-Memory, Postgres)
π Functional API Guide
1. The Pipe Pattern (Recommended)
The hallmark of LaraChain 2026 is the ability to chain components elegantly.
use LaraChain\Prompts\PromptTemplate; use LaraChain\Models\ChatModel; use LaraChain\Parsers\JsonParser; $chain = PromptTemplate::make('Extract data from this text: {text} into JSON format.') ->pipe(new ChatModel('gpt-4o')) ->pipe(new JsonParser()); $output = $chain->invoke(['text' => 'My name is John and I live in London.']); // Returns: ['name' => 'John', 'location' => 'London']
2. Intelligent Agents
An agent can use specialized tools to complete complex tasks.
use LaraChain\Agents\AgentExecutor; use LaraChain\Toolkits\FileToolkit; $agent = AgentExecutor::make() ->tools((new FileToolkit())->getTools()); $response = $agent->run("Read config.json and summarize it in readme.md");
3. RAG (Postgres + Recursive Chunking)
Handle large documents with state-of-the-art chunking and production storage.
use LaraChain\TextSplitters\RecursiveCharacterTextSplitter; use LaraChain\VectorStores\PostgresVectorStore; use LaraChain\Embeddings\EmbeddingModel; $splitter = new RecursiveCharacterTextSplitter(chunkSize: 1000, chunkOverlap: 200); $chunks = $splitter->splitText($largePdfContent); $store = new PostgresVectorStore(new EmbeddingModel()); $store->addTexts($chunks);
βοΈ LaraChain vs. LangChain (For Laravel)
| Feature | LangChain (Python/JS) | LaraChain (PHP/Laravel) |
|---|---|---|
| Syntax | Pipe Operator (|) |
Fluent .pipe() Method |
| Integration | Ad-hoc | Native Service Providers / Facades |
| I/O | General | Laravel FileSystem / DB Facades |
| Agents | LangGraph | ReAct / Future LaraGraph |
| Models | Custom Drivers | Laravel AI SDK (Native) |
π Use Cases
- Semantic Document Search: Build a "Chat with your PDF" app in minutes using
RecursiveSplitterandPostgresVectorStore. - Autonomous Code Auditor: Use the
FileToolkitandAgentExecutorto scan your repository for security flaws. - Structured Data Extraction: Pipe raw OCR text through a
ChatModelandJsonParserto ingest invoices into your database.
π οΈ Installation & Setup
composer require subhashladumor1/larachain
php artisan vendor:publish --tag="larachain-config"
Refer to LARACHAIN_VERIFICATION_2026.md for detailed verification of all 2026 market features.
π οΈ Multi-Provider Management
LaraChain uses a Driver-based Architecture (similar to Laravel's Database or Mail systems). You can configure and switch between providers at runtime.
1. Configuration (config/larachain.php)
Define multiple LLM, Vector, and Embedding providers:
'default' => [ 'llm' => 'openai', 'vectorstore' => 'postgres', ], 'llms' => [ 'openai' => ['model' => 'gpt-4o'], 'anthropic' => ['model' => 'claude-3-5-sonnet'], ],
2. Switching Providers at Runtime
Use the LaraChain facade to swap drivers dynamically:
// Use Anthropic instead of the default OpenAI $model = LaraChain::model('anthropic'); // Use a specific vector store $vectorStore = LaraChain::vectors()->driver('memory'); // Chain them together $chain = PromptTemplate::make('Hello {name}') ->pipe($model) ->pipe(new JsonParser());
βοΈ Configuration
Contributions are welcome! Pull requests for new Vector Drivers (Pinecone, Qdrant) are prioritized.
π License
The MIT License (MIT). See License File.