-
Notifications
You must be signed in to change notification settings - Fork 35
Adds Provider interfaces #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6cdfde0
feat: adds provider interfaces
JasonTheAdams de089d4
refactor: limits to ModelConfig type
JasonTheAdams c37dabb
refactor: adds mapping for lookup optimization
JasonTheAdams 5b46445
refactor: switches operations to be provider-based
JasonTheAdams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Providers/Contracts/ModelMetadataDirectoryInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Contracts; | ||
|
|
||
| use InvalidArgumentException; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; | ||
|
|
||
| /** | ||
| * Interface for accessing model metadata within a provider. | ||
| * | ||
| * Provides methods to list, check, and retrieve model metadata | ||
| * for all models supported by a provider. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ModelMetadataDirectoryInterface | ||
| { | ||
| /** | ||
| * Lists all available model metadata. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return list<ModelMetadata> Array of model metadata. | ||
| */ | ||
| public function listModelMetadata(): array; | ||
|
|
||
| /** | ||
| * Checks if metadata exists for a specific model. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $modelId Model identifier. | ||
| * @return bool True if metadata exists, false otherwise. | ||
| */ | ||
| public function hasModelMetadata(string $modelId): bool; | ||
|
|
||
| /** | ||
| * Gets metadata for a specific model. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $modelId Model identifier. | ||
| * @return ModelMetadata Model metadata. | ||
| * @throws InvalidArgumentException If model metadata not found. | ||
| */ | ||
| public function getModelMetadata(string $modelId): ModelMetadata; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Contracts; | ||
|
|
||
| /** | ||
| * Interface for checking provider availability. | ||
| * | ||
| * Determines whether a provider is configured and available | ||
| * for use based on API keys, credentials, or other requirements. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ProviderAvailabilityInterface | ||
| { | ||
| /** | ||
| * Checks if the provider is configured. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return bool True if the provider is configured and available, false otherwise. | ||
| */ | ||
| public function isConfigured(): bool; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Contracts; | ||
|
|
||
| use InvalidArgumentException; | ||
| use WordPress\AiClient\Providers\DTO\ProviderMetadata; | ||
| use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelConfig; | ||
|
|
||
| /** | ||
| * Interface for AI providers. | ||
| * | ||
| * Providers represent AI services (Google, OpenAI, Anthropic, etc.) | ||
| * and provide access to models, metadata, and availability information. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ProviderInterface | ||
| { | ||
| /** | ||
| * Gets provider metadata. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return ProviderMetadata Provider metadata. | ||
| */ | ||
| public static function metadata(): ProviderMetadata; | ||
|
|
||
| /** | ||
| * Creates a model instance. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $modelId Model identifier. | ||
| * @param ?ModelConfig $modelConfig Model configuration. | ||
| * @return ModelInterface Model instance. | ||
| * @throws InvalidArgumentException If model not found or configuration invalid. | ||
| */ | ||
| public static function model(string $modelId, ?ModelConfig $modelConfig = null): ModelInterface; | ||
|
|
||
| /** | ||
| * Gets provider availability checker. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return ProviderAvailabilityInterface Provider availability checker. | ||
| */ | ||
| public static function availability(): ProviderAvailabilityInterface; | ||
|
|
||
| /** | ||
| * Gets model metadata directory. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return ModelMetadataDirectoryInterface Model metadata directory. | ||
| */ | ||
| public static function modelMetadataDirectory(): ModelMetadataDirectoryInterface; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Models\Contracts; | ||
|
|
||
| use WordPress\AiClient\Providers\Models\DTO\ModelConfig; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; | ||
|
|
||
| /** | ||
| * Interface for AI models. | ||
| * | ||
| * Models represent specific AI models from providers and define | ||
| * their capabilities, configuration, and execution methods. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ModelInterface | ||
| { | ||
| /** | ||
| * Gets model metadata. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return ModelMetadata Model metadata. | ||
| */ | ||
| public function metadata(): ModelMetadata; | ||
|
|
||
| /** | ||
| * Sets model configuration. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param ModelConfig $config Model configuration. | ||
| * @return void | ||
| */ | ||
| public function setConfig(ModelConfig $config): void; | ||
|
|
||
| /** | ||
| * Gets model configuration. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return ModelConfig Current model configuration. | ||
| */ | ||
| public function getConfig(): ModelConfig; | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/Providers/Models/Contracts/WithGenerativeAiOperationsInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Models\Contracts; | ||
|
|
||
| use InvalidArgumentException; | ||
| use WordPress\AiClient\Operations\DTO\GenerativeAiOperation; | ||
|
|
||
| /** | ||
| * Interface for models that support generative AI operations. | ||
| * | ||
| * Provides methods to retrieve and manage long-running generative AI | ||
| * operations such as text, image, or speech generation. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface WithGenerativeAiOperationsInterface | ||
| { | ||
| /** | ||
| * Gets a generative AI operation by ID. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $operationId Operation identifier. | ||
| * @return GenerativeAiOperation The generative AI operation. | ||
| * @throws InvalidArgumentException If operation not found. | ||
| */ | ||
| public function getOperation(string $operationId): GenerativeAiOperation; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Providers/Models/ImageGeneration/Contracts/ImageGenerationModelInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts; | ||
|
|
||
| use WordPress\AiClient\Messages\DTO\Message; | ||
| use WordPress\AiClient\Results\DTO\GenerativeAiResult; | ||
|
|
||
| /** | ||
| * Interface for models that support image generation. | ||
| * | ||
| * Provides synchronous methods for generating images from text prompts. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ImageGenerationModelInterface | ||
| { | ||
| /** | ||
| * Generates images from a prompt. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param list<Message> $prompt Array of messages containing the image generation prompt. | ||
| * @return GenerativeAiResult Result containing generated images. | ||
| */ | ||
| public function generateImageResult(array $prompt): GenerativeAiResult; | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Providers/Models/ImageGeneration/Contracts/ImageGenerationOperationModelInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Models\ImageGeneration\Contracts; | ||
|
|
||
| use WordPress\AiClient\Messages\DTO\Message; | ||
| use WordPress\AiClient\Operations\DTO\GenerativeAiOperation; | ||
|
|
||
| /** | ||
| * Interface for models that support asynchronous image generation operations. | ||
| * | ||
| * Provides methods for initiating long-running image generation tasks. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface ImageGenerationOperationModelInterface | ||
| { | ||
| /** | ||
| * Creates an image generation operation. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param list<Message> $prompt Array of messages containing the image generation prompt. | ||
| * @return GenerativeAiOperation The initiated image generation operation. | ||
| */ | ||
| public function generateImageOperation(array $prompt): GenerativeAiOperation; | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/Providers/Models/SpeechGeneration/Contracts/SpeechGenerationModelInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Models\SpeechGeneration\Contracts; | ||
|
|
||
| use WordPress\AiClient\Messages\DTO\Message; | ||
| use WordPress\AiClient\Results\DTO\GenerativeAiResult; | ||
|
|
||
| /** | ||
| * Interface for models that support speech generation. | ||
| * | ||
| * Provides synchronous methods for generating speech from prompts. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| interface SpeechGenerationModelInterface | ||
| { | ||
| /** | ||
| * Generates speech from a prompt. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param list<Message> $prompt Array of messages containing the speech generation prompt. | ||
| * @return GenerativeAiResult Result containing generated speech audio. | ||
| */ | ||
| public function generateSpeechResult(array $prompt): GenerativeAiResult; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to question whether this approach makes sense: Looking up an existing operation is technically subject to the provider support, not model-specific, so maybe this shouldn't be an interface for models to implement?
Maybe we could instead have a
ProviderOperationsHandlerInterface(something like that) with the relevant instance methods (likegetOperation), and anotherProviderWithOperationsInterfacewhich the actual provider class would implement, with a single static methodoperationsHandler()which would return the provider-specific instance. That would align well with how the rest works, but keep it optional, as not every provider would need this.LMKWYT. If this needs more thinking and more discussion, we could leave it out of this PR entirely, as we'll only implement long-running operations later anyway. The key features for the MVP and the big three providers don't need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know more than I do in this case, but are we confident that looking up operations will always be on a per-provider and not per-model basis? I'd hate to make assumptions that back us into a corner where a provider exists with varying model operational abilities.
I do agree, though, that we shouldn't spend too much time on this right now.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All existing long-running operation implementations in APIs I've seen are model independent - i.e. tied to the entire provider, not specific models. It also makes sense conceptually because the model is only relevant once you trigger/start the operation. At that point you pass all the same information you'd pass to a regular request (model ID, model config etc.), but after that you just get an operation ID back that you can look up provider-wide - no need to remember which model was used at that point.
From that perspective, I'd suggest we go with what I mentioned in the previous comment:
ProviderOperationsHandlerInterfacewith (non-static) methodgetOperation(just like the current interface)ProviderWithOperationsHandlerInterfacewith static methodoperationsHandler(): ProviderOperationsHandlerInterfaceWould you be okay replacing this current interface with that? This way we have a starting point in place for operations, which IMO makes sense for a comprehensive picture (similar to #35 (comment)), even if we'll only use it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha! Appreciate the insight! I resolved this in 5b46445