Modern PHP applications can easily “plug in” AI services via REST/HTTP APIs. For example, PHP “plays well” with APIs like OpenAI or Hugging Face. In practice developers use community or official SDKs that wrap these AI APIs. Popular PHP AI libraries include specialized SDKs for OpenAI, Hugging Face, Cohere, Google’s Gemini, Mistral, etc. Many are MIT-licensed, well-maintained packages with composer support. For each we describe key features, supported providers, installation and usage examples. We also compare license, development activity, and community support (stars/downloads). Finally, we give recommendations by use-case and skill level.
Table of Contents
Wait, PHP Can Do AI?
Think of AI integration like ordering a pizza 🍕. You don’t need to build the oven, grow the wheat, or milk the cow yourself. You just need a phone to call the pizza shop and tell them what you want.
In the world of coding, Best PHP Libraries for AI are like that phone. They allow your website to “call” super-smart computers (like OpenAI or Google) and ask them to write text, translate languages, or even describe an image.
The coolest part? You don’t need a PhD in Math. If you can use PHP, you can build an AI app. Many of these tools use simple “wrappers” that handle all the scary technical stuff behind the scenes.
OpenAI PHP Clients (GPT, DALL·E, Whisper, etc.)
openai-php/client (community “official” SDK): A widely used PHP client for OpenAI’s API. It supports all endpoints (Chat Completions, Images, Audio, Edits, etc.), typed request/response DTOs, streaming, function-calling, and even Azure endpoints. Installation is via Composer (composer require openai-php/client), and usage is straightforward. For example:
use OpenAI;
$client = OpenAI::client(getenv('OPENAI_API_KEY'));
$response = $client->responses()->create([
'model' => 'gpt-4o',
'input' => 'Hello!',
]);
echo $response->outputText;This package (PHP 8.2+) has ~5.7k GitHub stars, is MIT-licensed, and actively maintained by the openai-php GitHub org. Its popularity is comparable to Orhan Erday’s client.
orhanerday/open-ai (popular SDK): A mature, community-driven OpenAI PHP SDK (MIT) requiring PHP 7.4+. It’s extremely popular (4M+ downloads) and feature-rich, supporting GPT-4, DALL·E, Whisper, embeddings, fine-tuning, etc. For example:
composer require orhanerday/open-ai$client = new \Orhanerday\OpenAi\OpenAi(getenv('OPENAI_API_KEY'));
$complete = $client->completion([
'model' => 'text-davinci-003',
'prompt' => 'Hello, world!',
]);
echo $complete;Packagist calls it the “most downloaded… huge community supported” OpenAI SDK. It’s well-documented with plenty of examples. Active development and a Discord support channel are maintained by the author.
Tectalic/public-openai-client (typed DTOs): This SDK (PHP >=7.2, MIT) provides fully typed request/response classes for all OpenAI endpoints. It features autocomplete and static-analysis friendliness (DTOs for each field). Usage is similar but with strong typing. Example from their docs:
use Tectalic\OpenAi\Manager;
use Tectalic\OpenAi\Models\ChatCompletions\CreateRequest;
$openai = Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
$response = $openai->chatCompletions()->create(new CreateRequest([
'model' => 'gpt-4',
'messages' => [['role'=>'user','content'=>'Say hi.']],
]))->toModel();
echo $response->choices[0]->message->content;Note: This package was archived by its author (last update Oct 2023) and is no longer maintained. Use it only if you need its typed-model approach.
Other OpenAI-related PHP SDKs: There are other community libraries (e.g. webboy/open-ai-api-client, hiddenhatpress/openai-assistants for the new Assistants API, or unofficial extensions). However, for most use-cases the above two cover general needs.
Hugging Face API Integration
kambo-1st/huggingface-php: A lightweight PHP client for Hugging Face’s Inference API (and other endpoints). It requires PHP 8.1+, uses PSR-18, and is MIT-licensed. It supports all inference types (text generation, fill-mask, summarization, etc.), plus tokenizers and audio. Installation:
composer require kambo/huggingface-phpUsage example:
use Kambo\Huggingface\Huggingface;
use Kambo\Huggingface\Enums\Type;
$client = Huggingface::client(getenv('HUGGINGFACE_API_KEY'));
$result = $client->inference()->create([
'model' => 'gpt2',
'inputs' => 'The goal of life is?',
'type' => Type::TEXT_GENERATION,
]);
echo $result['generated_text'] . "\n";The library is described as a “community-maintained PHP API client” for the Hugging Face API. It’s relatively new (dozens of stars) and has few dependents, but it covers the basics in a simple way. (Alternatively, one can call Hugging Face via generic HTTP clients or use their official Python/JS SDK via a separate service.)
Google Gemini (PaLM) Integration
google-gemini-php/client: A community-supported PHP client for Google’s Gemini (PaLM) models. It requires PHP 8.1+, PSR-18, and is MIT-licensed. It provides access to Gemini’s generative models (text, image, audio, video) and features like function-calling, grounding, streaming, etc. Install:
composer require google-gemini-php/clientA simple usage example:
use Gemini;
$client = Gemini::client(getenv('GOOGLE_GEMINI_API_KEY'));
$result = $client->generativeModel(model: 'gemini-2.0-flash')->generateContent('Hello');
echo $result->text(); // Hello! How can I assist you today?You can also do multi-turn chats, send images/video for interpretation, generate images (Imagen), and more, all via this one client. GoogleGeminiPHP has ~400 GitHub stars and is actively maintained (v2.0 with new PaLM features). It’s the go-to for Google Cloud AI models in PHP.
Cohere API Integration
clementtalleu/cohere-php-client: A PHP SDK (PHP 8.2+, MIT) for Cohere’s NLP API (text generation, embeddings, classification, tokenization, etc.). It’s PSR-18 compatible and fully covers Cohere’s v1/v2 endpoints. Install with:
composer require talleu/cohere-php-clientUsage example – embeddings or chat:
use Talleu\CohereClient\Cohere;
$client = Cohere::client(getenv('COHERE_API_KEY'));
$embeds = $client->embed()->create([
'Cohere is amazing!',
'Let’s try embedding some text.'
]);
var_dump($embeds);CohereClient returns data-transfer objects for responses. It’s a niche library (few stars, ~15 installs), but with growing support for Cohere’s evolving API (v2 chat, etc).
Mistral AI Integration
SoftCreatR/php-mistral-ai-sdk: A PHP SDK for the Mistral AI API (European open-source LLM service). Requires PHP 8.1+, ISC-licensed. It supports all Mistral endpoints: chat completions, embeddings, fine-tuning, transcription, etc., plus streaming. Install:
composer require softcreatr/php-mistral-ai-sdkUsage example – chat completion:
use SoftCreatR\MistralAI\MistralAI;
$mistral = new MistralAI($reqFactory, $streamFactory, $uriFactory, $httpClient, 'YOUR_API_KEY');
$response = $mistral->createChatCompletion([], [
'model' => 'mistral-tiny',
'messages' => [['role'=>'user','content'=>'Who is the most renowned French painter?']],
]);
if ($response->getStatusCode()===200) {
$data = json_decode($response->getBody()->getContents(), true);
print_r($data);
}You must provide PSR-17/18 HTTP client/factories. The library’s docs show streaming as well. MistralAI-PHP is a young project (tens of stars), but it’s the official way to call Mistral’s models in PHP.
Other Model Providers
- Anthropic Claude: While no official PHP SDK from Anthropic is widely known, tools like Prism or generic HTTP can connect to Claude (Anthropic’s API). Prism includes an Anthropic driver. For simple needs, you can use any PSR-18 client to call Anthropic’s endpoints directly.
- Ollama (Local LLM server): ArdaGnsrn/ollama-php (PHP 8.1+, MIT) is a client for Ollama (self-hosted LLMs). It provides completions, chat, and model management over a local Ollama API. E.g.:
$client = \ArdaGnsrn\Ollama\Ollama::client();
$response = $client->completions()->create(['model'=>'llama3.1','prompt'=>'Once upon a time']);
echo $response->response;You need an Ollama daemon running. Useful for on-prem or offline LLMs.
- Clarifai (Vision/AI): Clarifai offers an official gRPC PHP client (clarifai-php-grpc) for image & text recognition. It’s a specialized case (vision models), not covered here in detail.
- IBM Watson: IBM has Watson APIs (Assistant, NLU, etc). Integration is typically done via generic REST calls or by using IBM’s “Watson Libraries” in other languages. There’s no prominent PHP-first Watson SDK nowadays; you’d call the HTTP endpoints or use IBM’s Node/Python SDK via a service wrapper.
- Google Cloud AI (Vision, Translation, etc): Google provides the google/cloud-php meta-package covering Vision, Translate, NLP, Speech, etc. These are more general Cloud client libs rather than LLM-specific. For example
Google\Cloud\Translate\V3\TranslationServiceClientis a PHP client for Google Translate. For “AI services” like Vision, one uses those generic Google Cloud clients. - AWS AI Services: AWS’s AWS SDK for PHP lets you call services like Amazon Comprehend (NLP), Rekognition (vision), Lex (chatbots), etc. Use like
$client = new Aws\Comprehend\ComprehendClient(...);. No dedicated LLM SDK there, except through AWS Bedrock (which could be accessed via Prism’s Bedrock provider, see Framework below). - Other emerging: Some “universal” PHP libraries (e.g. skito/aipi-php) aim to unify multiple model providers behind one interface. These are experimental but attempt to standardize calls to OpenAI, Gemini, etc.
Framework Integrations
For building web apps, dedicated frameworks or bundles can streamline AI integration:
- Prism (Laravel): echolabsdev/prism (2.3k stars) is a Laravel package providing a unified interface to multiple LLM providers. Prism uses a “driver” pattern – out-of-the-box it supports Anthropic (Claude), OpenAI, Ollama, AWS Bedrock, etc, and you can add custom drivers. It offers a fluent API: e.g.,
use Prism;
$response = Prism::text()
->using('openai','gpt-4o')
->withSystemPrompt('You are a friendly assistant.')
->withPrompt('Explain quantum computing to a 5-year-old.')
->run();
echo $response->text;Prism handles API keys, request formatting, and even function-calling. It’s ideal for Laravel developers who want to “swap” LLM backends easily. Prism also has plugins for AWS Bedrock and others. Installation is via Composer and a service provider registration. Laravel News calls Prism “a powerful Laravel package” for LLMs with built-in providers for OpenAI, Anthropic, Ollama, etc.
- Symfony AI Bundle: symfony/ai-bundle is Symfony’s experimental AI component bundle. It integrates AI “Platforms” (OpenAI, etc) with Symfony’s dependency injection and config. For example, after
composer require symfony/ai-bundle, you can configure OpenAI inai.yamland use services to call models. It also provides “Agents” and “Tools” for building chatbots and RAG within Symfony apps. See Symfony docs for examples. - LangChain PHP: Projects like kambo-1st/langchain-php and neuroncore/neuron-ai offer chain/agent abstractions (prompt templates, memory, tools). These are still maturing but let advanced users build agentic workflows in PHP.
- RAG and Vector Tools: PHP has clients for vector stores (pgvector, RedisAI, Pinecone via HTTP) and RAG helpers (mzarnecki/php-rag). Libraries like LLPhant/LLPhant combine LLM clients with embeddings and search. These are more specialized to retrieval-augmented generation.
- Other Frameworks: In any PHP app, you can also use generic HTTP/PSR clients (Guzzle, Symfony HttpClient) to call AI APIs directly. Many of the above SDKs simply abstract these calls.
Usage Examples and Installation
Across all libraries, installation is via Composer. Usage generally follows the pattern: create a client with your API key, then call an endpoint. For instance, installing and using the official OpenAI client looked like:
composer require openai-php/client guzzlehttp/guzzleOpenAI::client(getenv('OPENAI_API_KEY'))
->responses()->create([
'model' => 'gpt-4o',
'input' => 'Hello!',
]);Similarly, Hugging Face, Cohere, and Mistral clients all use Client::client($apiKey) and then method chains for each API. We gave examples above. Usually you pass an array of parameters (model name, prompt/messages, etc.) and get JSON-like results or objects back. Check each library’s GitHub README for full examples and endpoint lists.
Licensing, Maintenance, and Community
Most AI SDKs in PHP are open-source, permissive-licensed (MIT is very common). For example, openai-php/client, kambo/huggingface-php, clementtalleu/cohere-php-client, google-gemini-php, and ArdaGnsrn/ollama-php are all MIT-licensed. The Mistral SDK uses an ISC license (also permissive).
In terms of activity and support:
- OpenAI clients: Both the official community client and Orhan’s SDK are very active. Orhan’s package has millions of downloads and a large Discord support community. The openai-php/client repo has thousands of stars and regular releases.
- Prism (Laravel): 2.3k stars, hundreds of commits, active development.
- Google Gemini PHP: ~395 stars, regularly updated alongside Google AI releases.
- Huggingface PHP: modest (tens of stars), but straightforward to extend and use.
- Cohere PHP: only a few stars/installs as of 2026, still under active development with v2 endpoints.
- Mistral PHP: new (teens of stars) but updated for Mistral’s 2024 API.
- Symfony/AI and other bundles: early-stage but backed by core Symfony team (docs updated 2024).
- AWS/Google Cloud Clients: These are official Google/AWS packages with enterprise-level support (though generic across many services).
Generally, all the above libraries can be installed via Composer and have documentation (either GitHub or official docs). Community Q&A (StackOverflow, Discord) exist for the big ones (OpenAI, Prism, AWS PHP SDK, etc.). Always check the library’s GitHub “Issues” and “Last commit” to gauge freshness before choosing.
Recommendations
- Beginners/General use: For simple AI calls without framework lock-in, Orhanerday/OpenAI PHP or openai-php/client are great for OpenAI GPT-style tasks. They have the easiest learning curve and strong community support. For Hugging Face or Cohere, their respective SDKs are straightforward (see examples above) and suffice for typical text tasks.
- Laravel developers: Use Prism. Its unified syntax hides the complexity of multiple providers. You can switch between GPT-4, Claude, or an Ollama model by changing a driver name. Prism’s “fluent” chainable API is very user-friendly for text generation and tool calls.
- Symfony developers: The Symfony AI components offer the most “Symfony-native” solution. They integrate with service containers and YAML config. If you prefer Symfony style DI and want pre-built agents/tools, this is ideal.
- Multi-Provider/RAG Experts: If you plan to combine APIs (RAG, vector search, multi-modal), look into LangChain-style PHP libraries (e.g. langchain-php) and vector database clients. The awesome-php-ml list (GitHub) is a good resource for pipelines. For retrieval flows, consider packages like LLPhant or custom orchestration.
- On-Prem/Offline: Use Ollama PHP or run models via AWS Bedrock with a provider like Prism’s Bedrock driver. This avoids API calls to external servers if needed.
In summary, the “best” PHP AI library depends on your target service and workflow. For cloud AI APIs, the official or popular community SDKs above (OpenAI, Hugging Face, Cohere, Google) cover most needs. Framework packages (Prism, Symfony AI) add glue for app developers. Always review the latest GitHub activity and license before adopting. Overall, these PHP libraries make it surprisingly easy to “give your app a brain” without leaving the PHP ecosystem.
Which One Should You Choose?
Selecting the right tool depends on your “vibe”:
- The Explorer: Go with OpenAI. It has the most tutorials and a huge community to help you.
- The Laravel Pro: Prism is a no-brainer. It saves time and keeps your code clean.
- The Privacy Ninja: Ollama is your best friend.
- The Enterprise Builder: Google Gemini or AWS SDK will give you the scale you need.
Summary Checklist
- Install Composer: You’ll need this to download the libraries.
- Get an API Key: Sign up at OpenAI or Google AI Studio.
- Start Small: Try making a simple “Hello World” chat first.
Adding AI to your PHP projects isn’t just a trend; it’s like giving your app a superpower. Whether you’re summarizing long articles or building a friendly chatbot, these libraries make the journey fun and easy.
What kind of AI app are you dreaming of building? Let me know in the comments—I’d love to help you brainstorm the first step! 😊