LLPhant: A PHP Generative AI Framework Inspired by LangChain

Last updated on by

LLPhant: A PHP Generative AI Framework Inspired by LangChain image

LLPhant is a PHP library for building Generative AI applications in PHP. Created by Maxime Thoonsen, it provides a unified interface for working with multiple LLM providers including OpenAI, Anthropic, Mistral, LM Studio, and Ollama. The library is inspired by LangChain and LLamaIndex, bringing similar patterns to PHP developers.

Multi-Provider LLM Support

LLPhant abstracts away provider differences so you can switch between AI services with minimal code changes. Whether you are using OpenAI's GPT models, Anthropic's Claude, Mistral, LM Studio, or running local models through Ollama, the interface remains consistent:

// OpenAI
$chat = new OpenAIChat();
$response = $chat->generateText('What is the capital of France?');
 
// Anthropic Claude
$chat = new AnthropicChat(new AnthropicConfig(AnthropicConfig::CLAUDE_3_5_SONNET));
$response = $chat->generateText('What is the capital of France?');
 
// Local models via Ollama
$config = new OllamaConfig();
$config->model = 'llama2';
$chat = new OllamaChat($config);

The library also supports streaming responses for real-time chat interfaces, token usage tracking for cost monitoring, and vision capabilities for image analysis.

Embeddings and Vector Storage

LLPhant includes a complete pipeline for building Retrieval-Augmented Generation (RAG) applications. You can read documents from various sources (PDF, Word, text files), split them into chunks, generate embeddings, and store them in your preferred vector database:

// Read and process documents
$reader = new FileDataReader(__DIR__.'/documents');
$documents = $reader->getDocuments();
 
// Split into chunks for embedding
$splitDocuments = DocumentSplitter::splitDocuments($documents, 800);
 
// Generate embeddings
$embeddingGenerator = new OpenAI3SmallEmbeddingGenerator();
$embeddedDocuments = $embeddingGenerator->embedDocuments($splitDocuments);
 
// Store in PostgreSQL with pgvector
$vectorStore = new DoctrineVectorStore($entityManager, Document::class);
$vectorStore->addDocuments($embeddedDocuments);
 
// Search for similar content
$embedding = $embeddingGenerator->embedText('search query');
$results = $vectorStore->similaritySearch($embedding, 5);

Vector store support includes Doctrine (PostgreSQL with pgvector), Redis, Elasticsearch, MongoDB, ChromaDB, Qdrant, Milvus, AstraDB, OpenSearch, Pinecone, and Typesense.

Question Answering with RAG

The QuestionAnswering class handles the entire RAG workflow: retrieving relevant documents from your vector store and generating contextualized responses:

use LLPhant\Query\SemanticSearch\QuestionAnswering;
 
$qa = new QuestionAnswering($vectorStore, $embeddingGenerator, $chat);
 
$response = $qa->answerQuestion('What are the main topics covered in the documentation?');

You can customize the system message template to control how the AI uses retrieved context, add guardrails for safety, and implement multi-query transformations to improve retrieval quality.

Function Calling and Tools

LLPhant supports function calling (tools), allowing your LLM to interact with external APIs and services. Define your tools as PHP classes and the LLM can decide when to invoke them based on the conversation context.

You can learn more about LLPhant and find detailed documentation at llphant.readthedocs.org and the GitHub repository.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media

The latest

View all →
Find the Security Vulnerabilities in Your Laravel App with Sensagraph image

Find the Security Vulnerabilities in Your Laravel App with Sensagraph

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
Passwordless Sign-In with Fortify Two-Factor Support in Laravel image

Passwordless Sign-In with Fortify Two-Factor Support in Laravel

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
AI Review for Laravel Upgrades image

AI Review for Laravel Upgrades

Read article
HTTP Query Method Support in Laravel 13.19 image

HTTP Query Method Support in Laravel 13.19

Read article