How prism-php/prism works
Project Type & Purpose
• This is a Laravel package (“Prism”) that abstracts integration with multiple AI/LLM providers.
• Its purpose is to let developers easily generate text, manage tool-based interactions, and handle multi-step conversations within Laravel apps.
• The README emphasizes it as a “powerful Laravel package” that simplifies working with LLMs while hiding the complexities of interacting with different providers.
Main Components
a. Core Package Components:
• Core service classes (e.g., src/Prism.php, src/PrismManager.php, src/PrismServer.php)
• Service Provider integration (src/PrismServiceProvider.php) for bootstrapping within the Laravel ecosystem.
• Configuration Files (config/prism.php) that set up defaults for provider integrations and behaviors.
b. Client-Facing Layers:
• HTTP Controllers (src/Http/Controllers/PrismChatController.php and PrismModelController.php) that expose endpoints for interactions.
• Facades (src/Facades/PrismServer.php, src/Facades/Tool.php) which provide a fluent interface.
c. AI Provider Integrations:
• A collection of provider-specific implementations under src/Providers (e.g., Anthropic, DeepSeek, Gemini, Groq, Mistral, Ollama, OpenAI, VoyageAI, XAI).
• Within each provider folder, the pattern is similar: there are Concerns, Handlers, Maps, and ValueObjects. Illustrate that the package uses a common interface (in src/Contracts) to interact with all providers.
d. Business Logic Components:
• Text, Structured, and Embeddings logic are separated into their own directories (src/Text, src/Structured, src/Embeddings). Each handles requests, responses, steps, and builders for their respective functionalities.
• Use of Schemas for structured validation (src/Schema classes).
e. Support & Utilities:
• Contracts defining interfaces (src/Contracts/Message.php, PrismRequest.php, Provider.php, Schema.php).
• Concerns and Traits contained in src/Concerns for shared functionality.
• Exceptions (src/Exceptions) for robust error handling across providers.
• Enums (src/Enums) and Value Objects (src/ValueObjects) to enforce consistency in message formatting and metadata.
Relationships and Data Flow
• The Prism Manager interacts with the configured provider implementations (selected from the Providers folder) via common contracts.
• Data flows from the controllers → core logic (Prism/Manager) → provider-specific handlers → external API calls.
• Results propagate back following the reverse path. Error handling occurs at both the provider level (via Exceptions) and within core logic.
• Dependency and invocation relationships among core components, providers, and helper classes (Concerns, ValueObjects, Schema).
• There is also a testing suite verifying the integration between these pieces, as seen in the tests folder.
Architectural Patterns & Design Principles
• The package follows a modular design, using the Strategy Pattern to manage multiple providers—each provider implementation conforms to a common interface.
• Laravel’s Service Container is used for dependency injection (as indicated by the Service Provider and facades).
• There’s a clear separation of concerns: HTTP layer, business/service layer, provider-specific logic, and utility components are clearly segmented.
• Layered architecture: configuration layer (config/prism.php), business logic layer (src/*), and presentation/HTTP layer.
Additional Elements
• External Services: Represent external LLM or AI provider services (OpenAI, Anthropic, etc.) as external nodes that the package communicates with.
• Development & Testing Tools: Optionally show resources for tests, CI configurations (under .github/workflows), and documentation (docs folder) as supporting elements.
• Configuration & Environment: Show the interplay between configuration files (config/prism.php) and the runtime environment.
• That each block/component has a clear label.
Happy diagramming!