|  | 
|  | 1 | +# PHP AI Client SDK | 
|  | 2 | + | 
|  | 3 | +## Project brief | 
|  | 4 | + | 
|  | 5 | +The project implements a provider agnostic PHP AI client SDK to communicate with any generative AI models of various capabilities using a uniform API. | 
|  | 6 | + | 
|  | 7 | +The project is stewarded by [WordPress AI Team](https://make.wordpress.org/ai/) members and contributors. It is however WordPress agnostic, so that any PHP project can use it. | 
|  | 8 | + | 
|  | 9 | +## High-level architecture | 
|  | 10 | + | 
|  | 11 | +The project architecture has several key design principles: | 
|  | 12 | + | 
|  | 13 | +### API layers | 
|  | 14 | + | 
|  | 15 | +1. **Implementer API**: For developers using the SDK to add AI features to their applications | 
|  | 16 | +    - Fluent API: Chain methods for readable, declarative code (e.g. `AiClient::prompt('...')->generateText()`) | 
|  | 17 | +    - Traditional API: Method-based approach with arrays of arguments (e.g. `AiClient::generateText('...')`) | 
|  | 18 | + | 
|  | 19 | +2. **Extender API**: For developers adding new providers, models, or extending functionality | 
|  | 20 | +    - Provider Registry system for managing available AI providers | 
|  | 21 | +    - Model discovery based on capabilities and requirements | 
|  | 22 | + | 
|  | 23 | +### Core concepts | 
|  | 24 | + | 
|  | 25 | +- **Provider Agnostic**: Abstracts away provider-specific details, allowing code to work with any AI provider (Google, OpenAI, Anthropic, etc.) | 
|  | 26 | +- **Capability-Based Model Selection**: Models can be discovered and selected based on their supported capabilities (text generation, image generation, etc.) and options (input modalities, output formats, etc.) | 
|  | 27 | +- **Uniform Data Structures**: Consistent message formats, file representations, and results across all providers | 
|  | 28 | +- **Modality Support**: Designed to support arbitrary combinations of input/output modalities (text, image, audio, video) | 
|  | 29 | + | 
|  | 30 | +### Key design patterns | 
|  | 31 | + | 
|  | 32 | +- **Interface Segregation**: Separate interfaces for different model capabilities (TextGenerationModelInterface, ImageGenerationModelInterface, etc.) | 
|  | 33 | +- **Composition over Inheritance**: Models compose capabilities through interfaces rather than inheritance | 
|  | 34 | +- **DTO Pattern**: Data Transfer Objects for messages, results, and configurations with JSON schema support | 
|  | 35 | +- **Builder Pattern**: Fluent builders for constructing prompts and messages | 
|  | 36 | + | 
|  | 37 | +### Directory structure | 
|  | 38 | + | 
|  | 39 | +- **Production Code**: All production code is found in the `src` directory, in subdirectories that match the namespace structure. | 
|  | 40 | +- **Tests**: PHPUnit tests are found in the `tests/unit` directory, with each test file covering a specific class from `src`. | 
|  | 41 | +    - Each test file is named after the tested class, suffixed with `Test`. | 
|  | 42 | +    - Each test file is located in a subdirectory equivalent to the tested class's directory within `src`. | 
|  | 43 | +    - Test specific mock classes and traits are located in `tests/mocks` and `tests/traits` respectively. | 
|  | 44 | + | 
|  | 45 | +### Namespace structure | 
|  | 46 | + | 
|  | 47 | +The production code in `src` follows a structured namespace hierarchy under the root namespace `WordPress\AiClient`: | 
|  | 48 | + | 
|  | 49 | +- `Builders`: Fluent API builders (PromptBuilder, MessageBuilder) | 
|  | 50 | +- `Embeddings`: Embedding-related data structures | 
|  | 51 | +- `Files`: File handling contracts and implementations | 
|  | 52 | +- `Messages`: Message DTOs and enums | 
|  | 53 | +- `Operations`: Long-running operation support | 
|  | 54 | +- `Providers`: Provider system with contracts, models, and registry | 
|  | 55 | +- `Results`: Result data structures and transformations | 
|  | 56 | +- `Tools`: Function calling and tool support | 
|  | 57 | +- `Util`: Utility classes for common operations | 
|  | 58 | + | 
|  | 59 | +## Development tooling and commands | 
|  | 60 | + | 
|  | 61 | +### Linting and code quality | 
|  | 62 | + | 
|  | 63 | +- **Run all linting checks**: `composer lint` (runs both PHPCS and PHPStan) | 
|  | 64 | +- **Run PHP CodeSniffer**: `composer phpcs` | 
|  | 65 | +- **Fix code style issues**: `composer phpcbf` | 
|  | 66 | +- **Run PHPStan static analysis**: `composer phpstan` | 
|  | 67 | + | 
|  | 68 | +### Testing | 
|  | 69 | + | 
|  | 70 | +- **Run PHPUnit tests**: `composer phpunit` | 
|  | 71 | + | 
|  | 72 | +### Dependencies | 
|  | 73 | + | 
|  | 74 | +- **Install dependencies**: `composer install` | 
|  | 75 | +- **Update dependencies**: `composer update` | 
|  | 76 | + | 
|  | 77 | +## Coding standards and best practices | 
|  | 78 | + | 
|  | 79 | +- **Code style**: All code must be compliant with the [PER Coding Style](https://www.php-fig.org/per/coding-style/), which extends [PSR-12](https://www.php-fig.org/psr/psr-12/). | 
|  | 80 | +- **Minimum required PHP version**: All code must be backward compatible with PHP 7.4. For newer PHP functions, polyfills can be used. | 
|  | 81 | + | 
|  | 82 | +### Type hints | 
|  | 83 | + | 
|  | 84 | +All parameters, return values, and properties must use explicit type hints, except in cases where providing the correct type hint would be impossible given limitations of backward compatibility with PHP 7.4. In any case, concrete type annotations using PHPStan should be present. | 
|  | 85 | + | 
|  | 86 | +### Naming conventions | 
|  | 87 | + | 
|  | 88 | +The following naming conventions must be followed for consistency and autoloading: | 
|  | 89 | + | 
|  | 90 | +- Interfaces are suffixed with `Interface`. | 
|  | 91 | +- Traits are suffixed with `Trait`. | 
|  | 92 | +- Enums are suffixed with `Enum`. | 
|  | 93 | +- File names are the same as the class, trait, and interface name for PSR-4 autoloading. | 
|  | 94 | +- Classes, interfaces, and traits, and namespaces are not prefixed with `Ai`, excluding the root namespace. | 
|  | 95 | + | 
|  | 96 | +## Further reading | 
|  | 97 | + | 
|  | 98 | +The `docs` folder in this repository provides additional in-depth information about various aspects of the project, such as its requirements or architecture. | 
0 commit comments