echolabsdev/prism
Prism is a Laravel package that simplifies integrating LLMs into your app. Use a fluent API to generate text, manage multi-step conversations, and run tools across multiple AI providers—so you can build AI features without provider-specific complexity.
Installation:
composer require prism-php/prism
Publish the config:
php artisan prism:install
Configure Providers:
Edit config/prism.php to add your API keys (OpenAI, Anthropic, Gemini, etc.) under providers.
First Use Case: Generate a simple completion:
use Prism\Prism;
$response = Prism::make('openai')
->complete('What is Laravel?')
->get();
/docs/quickstart.md in the repo/docs/providers/openai.md, /docs/providers/anthropic.md, etc.// Simple text generation
$response = Prism::make('openai')
->complete('Summarize this: "The quick brown fox..."')
->get();
// With parameters
$response = Prism::make('openai')
->complete('Explain quantum computing')
->temperature(0.7)
->maxTokens(100)
->get();
$conversation = Prism::make('anthropic')
->conversation()
->say('Hello!');
// Add user message
$conversation->user('What is AI?');
// Get AI response
$response = $conversation->get();
// Define a tool
$tool = Prism::tool()
->name('getWeather')
->description('Fetch weather data')
->parameters([
'location' => 'string',
'unit' => ['type' => 'string', 'enum' => ['celsius', 'fahrenheit']]
])
->handler(function ($location, $unit) {
// Call external API or service
return ['temperature' => 22, 'unit' => $unit];
});
// Use in a conversation
$conversation = Prism::make('gemini')
->conversation()
->tools([$tool])
->say('What is the weather in Paris?');
Prism::make('openai')
->complete('Stream this response')
->stream()
->each(function ($chunk) {
echo $chunk->content;
});
$embeddings = Prism::make('openai')
->embeddings(['Your text here'])
->get();
Register Prism in AppServiceProvider:
public function boot()
{
Prism::extend('custom', function () {
return new \Prism\Providers\CustomProvider();
});
}
Extend Prism's base class:
Prism::macro('customMethod', function () {
return $this->complete('Custom logic here');
});
// Define a tool that dispatches a job
$tool = Prism::tool()
->name('sendEmail')
->handler(function ($to, $subject, $body) {
SendEmailJob::dispatch($to, $subject, $body);
return ['status' => 'queued'];
});
$response = Prism::make('openai')
->complete('Frequent question')
->remember(3600) // Cache for 1 hour
->get();
max_tokens is set for non-streaming requests (removed as default in v0.99.6).thoughts are included unintentionally (fixed in v0.99.0).start/end events).streamToolParams is enabled for real-time tool parameter streaming (v0.99.7+).ToolResultEvent with success: false for unhandled tool errors.tool()->artifacts() to attach files or metadata to tools (v0.99.11).config/prism.php:
'providers' => [
'openai' => [
'default_model' => 'gpt-4',
],
],
Prism::debug(true); // Logs raw API requests/responses
try {
$response = Prism::make('openai')->complete('Test')->get();
} catch (\Prism\Exceptions\PrismException $e) {
// Log or handle provider-specific errors
if ($e instanceof \Prism\Exceptions\PrismProviderOverloadedException) {
// Retry logic or fallback
}
}
Prism::tool()->validate() to check schema syntax before runtime.type in parameters.enum values.properties.Extend \Prism\Contracts\Provider:
class CustomProvider extends \Prism\Providers\BaseProvider
{
public function complete($prompt, array $options = [])
{
// Custom logic
return $this->response(['content' => 'Custom response']);
}
}
Listen for streaming events:
Prism::on('streaming', function ($event) {
if ($event->type === 'content') {
echo $event->content;
}
});
Add middleware to modify requests:
Prism::extend('openai', function () {
$provider = new \Prism\Providers\OpenAIProvider();
$provider->middleware(function ($request) {
$request->headers->set('Custom-Header', 'Value');
});
return $provider;
});
Use the PrismTestCase trait:
use Prism\Testing\PrismTestCase;
class MyTest extends PrismTestCase
{
public function testCompletion()
{
$this->mockPrism('openai', [
'content' => 'Mocked response'
]);
$response = Prism::make('openai')->complete('Test')->get();
$this->assertEquals('Mocked response', $response->content);
}
}
$embeddings = Prism::make('openai')
->embeddings(['text1', 'text2', 'text3'])
->batchSize(4) // If supported by provider
->get();
Prism::make('openai')
->complete('Frequent query')
->remember(86400) // 24 hours
->get();
stream()->each() for real-time processing to avoid memory buildup.| Provider | Key Features | Gotchas |
|---|---|---|
| OpenAI | Structured output, moderation | max_tokens required for non-streaming. |
| Anthropic | Structured output (GA in v0.100.0) | Citations unsupported in structured mode. |
| Gemini | Tool calls, file search | Thoughts may break tool calls. |
| Ollama | Local models, keep-alive | Tool maps require explicit arguments. |
| OpenRouter | Multi-provider routing | Error handling improved in v0.99.7+. |
How can I help you explore Laravel packages today?