Prism Documentation

repository·main·Indexed 25 days ago

https://github.com/prism-php/prism

A Laravel-focused integration layer for Large Language Models (LLMs) providing a unified, fluent API for multiple AI providers. Prism supports text generation, structured output, embeddings, image generation, and conversational state, abstracting the technical complexities of different AI APIs.

Tokens
69.1K
Snippets
195
Records
238
Agent score
81%

What's inside Prism

  1. Overview of Prism features

    main

    Prism is a Laravel package designed for integrating Large Language Models (LLMs) into applications. Key capabilities include:

    • Elegant Provider Integrations: Switch between providers like OpenAI, Anthropic, and Ollama using a clean syntax.
    • Fluent Text Generation API: An intuitive, chainable API for crafting AI-powered text.
    • Seamless Tool Integration: Extend AI capabilities by providing custom tools and external APIs.
    • Structured Output Handling: Transform AI responses into strongly-typed data using schema validation and object mapping.
    • First-Class Testing Support: Includes utilities for response faking and detailed assertion helpers for unit testing.
    • Multi-Modal Capabilities: Support for text, images, and audio through a unified API.
  2. Overview of Prism

    main
    Prism is a Laravel package designed to integrate Large Language Models (LLMs) into your applications. It offers a fluent interface to simplify common AI tasks such as text generation, managing multi-step conversations, and utilizing tools across various AI providers. It is intended to abstract the technical complexities of different AI APIs, allowing developers to focus on application logic.
  3. Overview of Prism key features

    main

    Prism is designed to simplify LLM integration in Laravel projects through several core capabilities:

    • Unified Provider Interface: Switch between different AI providers (e.g., OpenAI, Anthropic, Ollama) without modifying your core application logic.
    • Tool System: Define custom tools that allow AI models to interact directly with your application's business logic.
    • Image Support: Support for multi-modal models capable of processing both text and image inputs.
  4. Use Prism Relay for Model Context Protocol (MCP) integration

    main

    Prism Relay allows you to expose Prism-powered AI models as Model Context Protocol (MCP) servers. This enables integration with MCP-compatible clients and tools, allowing you to build AI agent workflows using the Model Context Protocol.

    For the implementation details and source code, refer to the dedicated repository: https://github.com/prism-php/relay.

  5. Moderate images and mixed content

    main

    Prism supports image moderation using models like omni-moderation-latest. You can provide images via the Image value object.

    Important: Mixed Text and Image Behavior When mixing text and images in a single withInput() call:

    • Multiple text inputs alone return multiple results (one per text).
    • Multiple images alone return multiple results (one per image).
    • Text + Image combinations return one result per image, where the text is treated as context/description for that specific image, not as a separate moderation target.

    To get separate results for text and images, perform separate API calls.

    use Prism\Prism\Facades\Prism;
    use Prism\Prism\Enums\Provider;
    use Prism\Prism\ValueObjects\Media\Image;
    
    // Mixed text and images as variadic arguments
    $response = Prism::moderation()
        ->using(Provider::OpenAI, 'omni-moderation-latest')
        ->withInput(
            'Check this text',
            Image::fromUrl('https://example.com/image.png'),
            'Another text to check',
            Image::fromLocalPath('/path/to/image1.jpg')
        )
        ->asModeration();
    use Prism\Prism\Facades\Prism;
    use Prism\Prism\Enums\Provider;
    use Prism\Prism\ValueObjects\Media\Image;
    
    // Mix text and images as variadic arguments
    $response = Prism::moderation()
        ->using(Provider::OpenAI, 'omni-moderation-latest')
        ->withInput(
            'Check this text',
            Image::fromUrl('https://example.com/image.png'),
            'Another text to check',
            Image::fromLocalPath('/path/to/image1.jpg')
        )
        ->asModeration();
  6. Configure Nullable and Required fields

    main

    Prism distinguishes between whether a field must be present in the data structure and whether it can contain a null value.

    Key Concepts

    • Required Fields: Specified at the ObjectSchema level using requiredFields. A required field must be present in the data.
    • Nullable Fields: Specified at the individual field level using nullable: true. A nullable field can contain a null value.

    Common Patterns

    PatternImplementationDescription
    Required & Non-nullablenew StringSchema(..., nullable: false); + requiredFields: ['field']Must be present and cannot be null.
    Required but Nullablenew StringSchema(..., nullable: true); + requiredFields: ['field']Must be present, but can be null.
    Optional & Non-nullablenew StringSchema(..., nullable: false); + requiredFields: []Can be omitted; if present, cannot be null.
    Optional & Nullablenew StringSchema(..., nullable: true); + requiredFields: []Can be omitted or can be null.

    OpenAI Strict Mode Requirement

    When using OpenAI in strict mode, all fields must be marked as required. To make a field optional in this mode, you must mark it as both required in the ObjectSchema and nullable: true in the field schema.

    // For OpenAI strict mode: 
    // - All fields should be required
    // - Use nullable: true for optional fields
    $userSchema = new ObjectSchema(
        name: 'user',
        description: 'User profile',
        properties: [
            new StringSchema('email', 'Required email address'),
            new StringSchema('bio', 'Optional biography', nullable: true),
        ],
        requiredFields: ['email', 'bio'] // Note: bio is required but nullable
    );
  7. Combine Structured Output with Tools

    main

    You can combine structured output with tools to allow the AI to gather information (via function calls) before formatting the final result into your schema.

    Critical Requirement: When using tools with structured output, you must set withMaxSteps() to at least 2. The AI requires one step to call the tools and a subsequent step to return the final structured result.

    Response Lifecycle: Only the final step in the sequence contains the structured data. Intermediate steps contain toolCalls and toolResults but no structured output.

    use Prism\Prism\Facades\Prism;
    use Prism\Prism\Schema\ObjectSchema;
    use Prism\Prism\Schema\StringSchema;
    use Prism\Prism\Tool;
    
    $schema = new ObjectSchema(
        name: 'weather_analysis',
        description: 'Analysis of weather conditions',
        properties: [
            new StringSchema('summary', 'Summary of the weather'),
            new StringSchema('recommendation', 'Recommendation based on weather'),
        ],
        requiredFields: ['summary', 'recommendation']
    );
    
    $weatherTool = Tool::as('get_weather')
        ->for('Get current weather for a location')
        ->withStringParameter('location', 'The city and state')
        ->using(fn (string $location): string =>
            "Weather in {$location}: 72°F, sunny"
        );
    
    $response = Prism::structured()
        ->using('anthropic', 'claude-3-5-sonnet-latest')
        ->withSchema($schema)
        ->withTools([$weatherTool])
        ->withMaxSteps(3)
        ->withPrompt('What is the weather in San Francisco and should I wear a coat?')
        ->asStructured();
    
    // Access structured output
    dump($response->structured);
    // ['summary' => '...', 'recommendation' => '...']
  8. Document transfer and provider limitations

    main

    Prism attempts to normalize how documents are sent to different providers, but behavior varies based on provider capabilities:

    Transfer Mediums

    • URLs: If a provider does not support URLs, Prism will fetch the URL and convert it to base64 or rawContent.
    • Files/Base64/Raw: Prism will automatically switch between base64 and rawContent depending on what the specific provider accepts.

    Limitations

    • URL-only Providers: If a provider only supports URLs, Prism will not create a URL for you if you provide a local path, raw content, base64, or chunks. In this case, the request will fail for security reasons.
    • Chunks: Document chunks cannot be passed between different providers because they may be in incompatible formats. Currently, only Anthropic supports chunks.
  9. Use Native Structured Outputs with Anthropic

    main

    Prism uses Anthropic's native structured outputs by default. This provides guaranteed schema compliance through constrained decoding without requiring a beta header.

    Benefits:

    • Always valid JSON
    • Type safe

    Limitations:

    • Only available on Claude Sonnet 4.5+ and Claude Opus 4.1+
    • Cannot be used with citations
    • Certain JSON Schema features are not supported (e.g., recursive schemas, numerical/string constraints, complex regex).

    Supported Schema Features:

    • Basic types: object, array, string, integer, number, boolean, null
    • enum for simple types
    • anyOf and allOf (with limitations)
    • required and additionalProperties: false
    • String formats: date-time, email, uri, uuid, etc.
    use Prism\Prism\Enums\Provider;
    use Prism\Prism\Facades\Prism;
    use Prism\Prism\Schema\ObjectSchema;
    use Prism\Prism\Schema\StringSchema;
    
    $response = Prism::structured()
        ->withSchema(new ObjectSchema(
            'weather_report',
            'Weather forecast with recommendations',
            [
                new StringSchema('forecast', 'The weather forecast'),
                new StringSchema('recommendation', 'Clothing recommendation')
            ],
            ['forecast', 'recommendation']
        ))
        ->using(Provider::Anthropic, 'claude-sonnet-4-5-20250929')
        ->withPrompt('What\'s the weather like and what should I wear?')
        ->asStructured();
  10. Understand image transfer mediums and limitations

    main

    Prism attempts to normalize image data formats across different providers, but behavior depends on what the provider supports:

    Supported Conversions

    • Missing URL support: If a provider does not support URLs, Prism will automatically fetch the URL and convert it to base64 or rawContent.
    • File/Base64/Raw input: If you provide a file, base64, or raw content, Prism will automatically switch between base64 and rawContent based on the provider's requirements.

    Limitations

    • URL-only providers: If a provider only supports URLs, Prism will not create a URL for you if you provide a file path, raw content, or base64. In these cases, the request will fail for security reasons.
  11. Understand Structured vs JSON output modes

    main

    AI providers generally handle structured output in two ways:

    1. Structured Mode: The provider supports strict schema validation, ensuring the response perfectly matches your defined structure.
    2. JSON Mode: The provider guarantees valid JSON output that approximately matches your schema but may not strictly adhere to every detail.

    Always verify your specific model and provider's capabilities to determine which mode is supported.