Introduction
Prism PHP is a Laravel package that helps developers interact with large language models (LLMs) efficiently. It wraps popular AI providers like OpenAI, Anthropic, and Google Gemini into simple Laravel-friendly methods, letting you focus on building AI-powered features rather than low-level API calls. With Prism PHP, you can easily integrate text generation, embeddings, and intelligent workflows directly into your Laravel applications, making it a go-to solution for developers who want to build modern, AI-driven tools without dealing with complex setup or scattered APIs.
In this blog post, we’ll explore how Prism PHP works, its key features, and why it’s quickly becoming an essential tool for Laravel developers working with artificial intelligence.
Understanding Providers in Prism PHP
Providers determine the AI engine behind your application. Choosing the right provider ensures the AI delivers responses suited to your use case.
- Anthropic – Claude models for concise, safe, and helpful responses.
- OpenAI – GPT family for versatile text generation and conversations.
- Gemini – Multi-modal models capable of handling text, images, audio, and video.
Example: Using a Provider
use Prism\Prism\Prism; use Prism\Prism\Enums\Provider; $response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt('Explain microservices architecture in simple words.') ->asText();
Text Requests Using Prism PHP
Prism PHP allows straightforward text generation:
use Prism\Prism\Prism; use Prism\Prism\Enums\Provider; $response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt('Explain machine learning in simple words.') ->asText();
Concepts:
text()
: Starts a text-generation request.withPrompt()
: Provides instructions for the AI.asText()
: Returns the AI response as a single text block.
System Prompts and Context in Laravel
System prompts set the behavior or persona for the AI:
$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withSystemPrompt('You are an expert AI tutor who explains topics concisely.') ->withPrompt('Explain the difference between supervised and unsupervised learning.') ->asText();
Multi-Modal Input with Prism PHP
Prism PHP supports images, documents, audio, and video, making it a true multi-modal AI package.
Analyzing Images
$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt( 'Describe the objects in this image.', [Image::fromLocalPath('/path/to/image.jpg')] ) ->asText();
Processing Documents
$response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withPrompt( 'Summarize this research paper.', [Document::fromLocalPath('/path/to/paper.pdf')] ) ->asText();
Handling Audio & Video
$response = Prism::text() ->using(Provider::Gemini, 'gemini-1.5-flash') ->withPrompt( 'Summarize the discussion in this audio clip.', [Audio::fromLocalPath('/path/to/audio.mp3')] ) ->asText(); $response = Prism::text() ->using(Provider::Gemini, 'gemini-1.5-flash') ->withPrompt( 'Explain what happens in this video.', [Video::fromUrl('https://www.youtube.com/watch?v=dQw4w9WgXcQ')] ) ->asText();
Multiple Media Types in One Prompt
$response = Prism::text() ->using(Provider::Gemini, 'gemini-1.5-flash') ->withPrompt( 'Compare this chart with the report document.', [ Image::fromLocalPath('/path/to/chart.png'), Document::fromLocalPath('/path/to/report.pdf') ] ) ->asText();
Message Chains and Conversations
use Prism\Prism\ValueObjects\Messages\UserMessage; use Prism\Prism\ValueObjects\Messages\AssistantMessage; $response = Prism::text() ->using(Provider::Anthropic, 'claude-3-5-sonnet-20241022') ->withMessages([ new UserMessage('What is JSON?'), new AssistantMessage('JSON is a lightweight data format for storing data.'), new UserMessage('Can you give an example in PHP?') ]) ->asText();
Structured Output for APIs and Forms
use Prism\Prism\Schema\ObjectSchema; use Prism\Prism\Schema\StringSchema; $schema = new ObjectSchema( name: 'book_review', description: 'A structured book review', properties: [ new StringSchema('title', 'Book title'), new StringSchema('rating', 'Rating out of 5'), new StringSchema('summary', 'Brief review') ], requiredFields: ['title', 'rating', 'summary'] ); $response = Prism::structured() ->using(Provider::OpenAI, 'gpt-4o') ->withSchema($schema) ->withPrompt('Review the book Dune') ->asStructured(); // Access structured data $review = $response->structured; echo $review['title']; echo $review['rating']; echo $review['summary'];
Streaming Output in Laravel with Prism PHP
$response = Prism::text() ->using('openai', 'gpt-4') ->withPrompt('Write a short story about a courageous cat.') ->asStream(); foreach ($response as $chunk) { echo $chunk->text; ob_flush(); flush(); }
Handling Streaming in Web Applications
Laravel Controller
use Illuminate\Http\Response; use Prism\Prism\Prism; public function streamStory() { return response()->stream(function () { $stream = Prism::text() ->using('openai', 'gpt-4') ->withPrompt('Explain blockchain technology step by step.') ->asStream(); foreach ($stream as $chunk) { echo $chunk->text; ob_flush(); flush(); } }, 200, [ 'Cache-Control' => 'no-cache', 'Content-Type' => 'text/event-stream', 'X-Accel-Buffering' => 'no', ]); }
Vue Frontend
const evtSource = new EventSource('/story/stream'); evtSource.onmessage = (event) => { if (event.data === '[DONE]') { evtSource.close(); return; } storyContent.value += event.data; };
Conclusion
Prism PHP is a Laravel AI package that makes building AI-powered applications simple and elegant. From text generation, structured outputs, multi-modal input, message chains, to live streaming, Prism PHP abstracts the complexity of LLM integrations, enabling developers to focus on creating value.
Whether you’re building chatbots, document analyzers, or content generators, this package accelerates development with clean Laravel-first APIs.
External Resources
For more insightful tutorials, visit our Tech Blogs and explore the latest in Laravel, AI, and Vue.js development!