Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Prism Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strengths:

    • Abstraction Layer: Prism provides a clean, provider-agnostic abstraction for LLM interactions (OpenAI, Anthropic, Gemini, etc.), reducing vendor lock-in and simplifying future migrations.
    • Tooling & Workflows: Supports multi-step conversations, tool calls, streaming, and structured outputs (e.g., JSON schemas), aligning with modern AI workflows (e.g., agents, RAG, or hybrid search).
    • Laravel Integration: Leverages Laravel’s service container, events, and macros (e.g., macroable support), enabling seamless integration with existing Laravel patterns (e.g., commands, queues, or Livewire).
    • Extensibility: Backed enums, provider-specific features (e.g., Gemini’s thinkingLevel), and custom tool definitions allow tailored configurations for niche use cases.
    • Observability: Events (e.g., ToolResultEvent, CompletionEvent) and callbacks (e.g., onComplete) enable logging, monitoring, or real-time UI updates (e.g., streaming responses).
  • Gaps:

    • Multi-Provider Sync: While Prism supports multiple providers, ensuring consistency across provider-specific behaviors (e.g., token limits, tool call formats) may require additional validation or middleware.
    • State Management: Conversational state (e.g., memory, context windows) is handled per-instance; scaling to multi-user or multi-threaded workflows may need external persistence (e.g., Redis).
    • Cost Controls: No built-in budgeting or rate-limiting; reliance on provider APIs for quotas (e.g., OpenAI’s max_tokens).

Integration Feasibility

  • Laravel Ecosystem:
    • Native Fit: Works out-of-the-box with Laravel’s HTTP client, queues, and event system. Example:
      use Prism\Prism;
      Prism::make('openai')
           ->complete('Generate a blog post about Laravel 11')
           ->onComplete(fn ($response) => Log::info($response->content));
      
    • Queueable: Async operations can be dispatched via Laravel queues (e.g., Prism::make()->stream()->toQueue('llm-jobs')).
    • Livewire/Inertia: Streaming responses can be piped to frontend frameworks for real-time UIs.
  • Third-Party Dependencies:
    • Requires guzzlehttp/guzzle (for HTTP calls) and symfony/http-client (optional). No major conflicts with Laravel’s default stack.
    • Provider SDKs (e.g., openai-php) are optional if using Prism’s direct HTTP layer.

Technical Risk

  • Breaking Changes:
    • v0.100.0: Structured output mode shifted from prompt-based JSON to native API support, requiring updates to existing code using structuredOutput().
    • Callback System: Changes to onComplete handlers (e.g., removal of onComplete in favor of CompletionEvent) may need refactoring.
  • Provider-Specific Quirks:
    • Gemini/Anthropic: Tool call handling (e.g., thought_signature, thinkingLevel) differs from OpenAI; edge cases may arise in multi-provider setups.
    • Streaming: Provider-specific streaming formats (e.g., Gemini’s streamEnd events) require careful event listener setup.
  • Performance:
    • Token Limits: Removed default token limits (v0.99.6) may lead to unexpected costs if not explicitly managed.
    • Payload Size: Large tool calls or embeddings (e.g., image support) could hit provider rate limits or Laravel’s max_execution_time.

Key Questions

  1. Provider Strategy:
    • Will you use a single provider or multi-provider fallback? If the latter, how will you handle provider-specific inconsistencies (e.g., tool schemas)?
  2. Cost Management:
    • How will you monitor token usage and enforce budgets? Consider integrating with tools like OpenAI’s Usage API or custom middleware.
  3. State Persistence:
    • For conversational workflows, will you store state in-memory (e.g., Laravel cache) or a database? If the latter, design a schema for messages/tools.
  4. Error Handling:
    • How will you handle provider-specific errors (e.g., PrismProviderOverloadedException)? Example:
      Prism::make()->catch(fn (Exception $e) => notify($e->getMessage()));
      
  5. Scaling:
    • Will you use queues for async operations? If so, design a retry strategy for failed jobs (e.g., PrismJob::failed()).
  6. Testing:
    • How will you mock LLM responses for unit tests? Prism supports mocking via Prism::fake(), but integration tests may need provider API keys.

Integration Approach

Stack Fit

  • Core Laravel Components:
    • Service Container: Prism registers providers as bindings (e.g., Prism::make('openai')). Extend via:
      $this->app->bind('prism.openai', fn () => new CustomOpenAIProvider());
      
    • Events: Leverage built-in events (e.g., ToolResultEvent) or create custom ones for domain-specific logic.
    • Commands: Dispatch LLM tasks via Artisan commands:
      Artisan::call('prism:complete', [
          'provider' => 'anthropic',
          'prompt' => 'Summarize this document',
          '--tools' => json_encode([...]),
      ]);
      
    - **Queues**: Offload heavy operations (e.g., embeddings, multi-turn chats) to queues:
      ```php
      Prism::make()->stream()->toQueue('llm-queue');
    
  • Frontend Integration:
    • Livewire: Stream responses to a Livewire component:
      public function mount() {
          Prism::make()->stream()->onChunk(fn ($chunk) => $this->emit('llm-chunk', $chunk));
      }
      
    • Inertia/Vue: Use Laravel Echo or server-sent events (SSE) for real-time updates.

Migration Path

  1. Evaluation Phase:
    • Start with a single provider (e.g., OpenAI) and test core workflows (completions, tools, streaming).
    • Use Prism::fake() to mock responses in tests:
      Prism::fake([
          'openai.completions' => fn () => 'Mocked response',
      ]);
      
  2. Incremental Rollout:
    • Phase 1: Replace direct API calls with Prism wrappers (e.g., swap OpenAI::complete() for Prism::make('openai')->complete()).
    • Phase 2: Add tooling (e.g., custom tools for internal APIs) and test multi-turn conversations.
    • Phase 3: Implement streaming for real-time features (e.g., chat UIs).
  3. Provider Abstraction:
    • Use feature flags to toggle providers (e.g., config('prism.default_provider')).
    • Example config:
      'providers' => [
          'openai' => [
              'key' => env('OPENAI_KEY'),
              'model' => 'gpt-4',
          ],
          'anthropic' => [
              'key' => env('ANTHROPIC_KEY'),
              'model' => 'claude-3',
          ],
      ],
      

Compatibility

  • Laravel Versions:
    • Officially supports Laravel 10+ (tested via CI in v0.100.0). For Laravel 11, use the latest dev branch.
    • Deprecations: Monitor Laravel’s upgrade guide for changes to service containers or events.
  • PHP Versions:
    • Requires PHP 8.1+. Test compatibility if using older versions (e.g., 8.0).
  • Provider SDKs:
    • Prism can work without provider SDKs (direct HTTP calls), but some features (e.g., Anthropic’s structured output) may require SDKs for full parity.

Sequencing

  1. Setup:
    • Install via Composer:
      composer require prism-php/prism
      
    • Publish config:
      php artisan vendor:publish --provider="Prism\PrismServiceProvider"
      
    • Configure providers in .env and config/prism.php.
  2. Core Integration:
    • Replace direct API calls with Prism methods (e.g., Prism::make('openai')->chat()).
    • Example migration:
      // Before
      $response = OpenAI::chat()->create([...]);
      
      // After
      $response = Prism::make('openai')->chat()->create([...]);
      
  3. Advanced Features:
    • Implement tool calls and event listeners:
      Prism::make()->tool('summarize')->onResult(fn ($result) => $this->storeSummary($result));
      
    • Add streaming to frontend components.
  4. Monitoring:
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky