This module provides an abstraction layer and Artisan commands to integrate different AI providers (such as OpenAI, Anthropic, etc.) into Laravel applications, with initial focus on Assistant, Threads and Vector Stores functionalities for RAG (Retrieval Augmented Generation).
The AiHub Module was designed with a flexible architecture to allow easy addition of new AI providers. It acts as a central hub, decoupling your application from each provider's specific implementation.
The module offers commands to manage common AI functionalities, initially implemented for OpenAI:
- Assistants: Create, list, update and delete AI assistants.
- Vector Stores: Manage knowledge bases for RAG (create, list, link, remove).
- Threads: Manage conversations with assistants (create, list messages, send messages).
All commands and service structure follow a consistent design pattern, with clear separation of responsibilities and proper encapsulation.
The strength of this module lies in its architecture, which facilitates the integration of new AI providers:
- Contracts: PHP interfaces that define common operations (Assistant, Thread, VectorStore, File, Ai). Your application interacts only with these contracts.
- Clients: Specific implementations of contracts for each AI provider (ex:
Modules\AiHub\Ai\Clients\OpenAi\OpenAi
). - Factory: The
AiFactory
class is responsible for creating the correct AI client instance based on configuration or requested provider. - Service: The
AiService
class acts as a facade, using the Factory to get the correct client and expose services (assistant(), thread(), etc.) to your application.
To add a new AI provider, you would typically need to:
- Create Client classes for the new provider, implementing existing contracts.
- Add a new entry in
AiFactory
to instantiate the new client. - Configure API keys and defaults for the new provider in the module's configuration file.
Command | Description |
---|---|
ai:assistant-create [name] [instructions] |
Creates a new OpenAI assistant |
ai:assistant-list [company] [--interactive] |
Lists all available assistants |
ai:assistant-update [name] |
Updates an existing assistant |
ai:assistant-delete [name] |
Removes an existing assistant |
Command | Description |
---|---|
ai:knowledge-add [company] [--name=] [--description=] [--interactive] |
Creates a new Vector Store for documents |
ai:knowledge-list [company] [--interactive] |
Lists all knowledge bases |
ai:knowledge-link [company] [--interactive] |
Links a Vector Store to an Assistant |
ai:knowledge-remove [company] [--interactive] |
Removes a Vector Store |
Command | Description |
---|---|
ai:chat-start [company] [--interactive] |
Starts a new conversation with an assistant |
ai:chat-active [company] [--interactive] |
Lists all active conversations |
ai:chat-list [thread_id] [--limit=10] [--interactive] |
Lists messages from a conversation |
ai:chat-send [thread_id] [--message=] [--interactive] |
Sends a message to an existing conversation |
All commands were implemented following SOLID principles, especially the Single Responsibility Principle (SRP). Each command follows the same general structure:
- Class properties: Store the command's state during execution
- handle() method: Main entry point, coordinates execution flow
- Specific methods: Each responsibility has its own dedicated method
- Error handling: Implemented at each step to ensure robustness
All commands support an interactive mode (flag --interactive
), which guides the user through dialogs. This mode is especially useful for beginners.
php artisan ai:assistant-create "Support Assistant" "This assistant helps with technical support"
php artisan ai:assistant-list my-company
php artisan ai:knowledge-add my-company --name="Technical Documentation" --description="Knowledge base for technical documentation""
php artisan ai:knowledge-link my-company
php artisan ai:chat-start my-company --interactive
php artisan ai:chat-send --message="Hello, I need help with configuration"
- PHP 8.1+
- Laravel 10+
- OpenAI account with access to Assistants API
- AiHub module properly configured
Modules/AiHub/
├── Console/
│ ├── Ia/
│ │ ├── Assistant/
│ │ │ ├── CreateChatCommand.php
│ │ │ ├── DeleteChatCommand.php
│ │ │ ├── ListAssistantCommand.php
│ │ │ └── UpdateChatCommand.php
│ │ ├── Thread/
│ │ │ ├── CreateThreadCommand.php
│ │ │ ├── ListMessagesCommand.php
│ │ │ ├── ListThreadCommand.php
│ │ │ └── SendMessageCommand.php
│ │ └── VectorStore/
│ │ ├── AttachVectorCommand.php
│ │ ├── CreateVectorCommand.php
│ │ ├── DeleteVectorCommand.php
│ │ └── ListVectorCommand.php
├── Models/
│ ├── Assistant.php
│ ├── Company.php
│ ├── Message.php
│ ├── Thread.php
│ └── VectorStore.php
├── Ai/
│ ├── Contracts/
│ │ ├── Ai.php
│ │ ├── Assistant.php
│ │ ├── Thread.php
│ │ ├── VectorStore.php
│ │ └── File.php
│ │
│ ├── Clients/
│ │ ├── OpenAi/
│ │ │ ├── OpenAi.php
│ │ │ ├── OpenAiAssistant.php
│ │ │ ├── OpenAiThread.php
│ │ │ ├── OpenAiVectorStore.php
│ │ │ └── OpenAiFile.php
│ │ │
│ │ └── Anthropic/
│ │ └── ... (Exemplo para futuros provedores)
│ │
│ ├── Factory/
│ │ └── AiFactory.php
│ │
│ ├── AiService.php
│ └── AiServiceProvider.php
└── config/
└── aihub.php
- Single Responsibility: Each method and class has a single well-defined responsibility.
- Encapsulation: State variables are class properties, not local variables.
- Documentation: All methods have documentation comments (PHPDoc).
- Error Handling: Failure points are properly handled and reported.
- User Feedback: Clear messages about operation progress.
- Input Validation: User-provided data is validated before use.
- Control Flow: Execution flow is clear and consistent between commands.
- Extensibility: Contract-based architecture and factory for easy addition of new providers.
This module was built to be extensible! Your contribution is very welcome. If you're interested in the architecture and would like to add support for another AI provider (such as Anthropic, Google AI, etc.), feel free to:
- Fork this repository.
- Implement Client classes for the new provider, following existing contracts.
- Update AiFactory and configuration to include the new provider.
- Submit a Pull Request with your changes. Together, we can make this module a robust hub for integrating various AIs into Laravel projects!
This module is open-source and available under the MIT license.
© 2025 - Developed with Laravel, OpenAI PHP SDK, laravel-modules and the community!