prism-php/prism
Prism is a Laravel package for integrating LLMs with a fluent API. Generate text, run multi-step conversations, and call tools across multiple AI providers, so you can build AI features in your app without wrestling with provider-specific details.
Installation:
composer require prism-php/prism
Publish the config:
php artisan prism:install
Configure Providers:
Edit .env with your preferred LLM provider (e.g., OpenAI, Anthropic, Mistral) and API keys:
PRISM_PROVIDERS=openai
OPENAI_API_KEY=your_key_here
First Use Case: Generate text via a controller:
use Prism\Prism;
public function generateText()
{
$response = Prism::make('openai')
->model('gpt-4')
->text('What is Laravel?')
->get();
return response()->json($response);
}
config/prism.php for enabled providers and their capabilities.$response = Prism::make('openai')
->model('gpt-4')
->text('Summarize this: ' . $longText)
->temperature(0.7)
->get();
$conversation = Prism::make('anthropic')
->model('claude-3')
->startConversation();
$conversation->say('Hello!');
$response = $conversation->ask('How are you?');
Define tools in a Tool class:
use Prism\Tool;
class WeatherTool extends Tool
{
public function handle(string $query): string
{
return "Weather for $query: Sunny";
}
}
Register and use:
$response = Prism::make('openai')
->model('gpt-4')
->tools([new WeatherTool()])
->text('What is the weather in Paris?')
->get();
Prism::make('openai')
->model('gpt-4')
->text('Explain Laravel')
->stream()
->each(function ($chunk) {
echo $chunk->content;
});
$embeddings = Prism::make('openai')
->model('text-embedding-ada-002')
->embeddings(['Your text here'])
->get();
$this->app->singleton(PrismManager::class, function ($app) {
return new PrismManager(config('prism'));
});
ToolCallEvent, StreamEvent, etc., for real-time processing:
Prism::make('openai')->listen(function ($event) {
// Handle tool calls or streams
});
Prism\Skill for reusable AI logic:
Prism::skill('summarize')->run($longText);
Provider-Specific Quirks:
TypeError (fixed in v0.99.22).Streaming Artifacts:
artifact to data-artifact:
- artifacts.set(data.toolCallId, data.artifact);
+ artifacts.set(data.data.toolCallId, data.data.artifact);
Tool Call Loops:
ToolChoice::Any. Use required mapping to enforce tool selection:
->tools([new WeatherTool()])
->toolChoiceMap(['required' => [WeatherTool::class]])
Rate Limits:
PrismProviderOverloadedException for 503 errors (v0.100.0).Enable Debugging:
PRISM_DEBUG=true
Logs raw API responses and errors to storage/logs/prism.log.
Tool Debugging: Expose raw tool call arguments:
$response = Prism::make('openai')
->tools([new WeatherTool()])
->text('...')
->withDebug()
->get();
Streaming Issues:
StreamEndEvent is emitted when tools reach maxSteps (fixed in v0.99.14).->each() for granular control over chunks.Custom Providers:
Extend Prism\Providers\Provider or use PrismManager::extend():
PrismManager::extend('custom', function () {
return new CustomProvider();
});
Event Customization:
Override default events (e.g., ToolCallEvent) in EventServiceProvider:
Prism::make('openai')->listen(function ($event) {
// Custom logic
});
Skills System:
Create modular AI skills in app/Skills/:
namespace App\Skills;
use Prism\Skill;
class SummarizeSkill extends Skill
{
public function handle(string $text): string
{
return Prism::make('openai')
->model('gpt-4')
->text("Summarize: $text")
->get()
->content;
}
}
Register in config/prism.php:
'skills' => [
App\Skills\SummarizeSkill::class,
],
Caching: Enable automatic caching for Anthropic (v0.99.21):
Prism::make('anthropic')->withCache();
->batch() for bulk embedding generation (supported by OpenAI, Gemini, etc.).->tools([$tool1, $tool2])
->concurrent()
How can I help you explore Laravel packages today?