Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
felixarntz
left a comment
There was a problem hiding this comment.
@JasonTheAdams Looks great! A few additional points to consider, but this is close.
| */ | ||
| protected function getCacheKey(): string | ||
| { | ||
| return 'ai_client_models_' . md5(static::class); |
There was a problem hiding this comment.
It would be great if we could include the current AI Client version in the cache key. A version update may come with an updated DTO shape at some point, so it's always safest to invalidate all caches upon update. Or there may be a fix in a specific ModelMetadataDirectory implementation.
This was relevant quite a few times while I worked on AI Services, so I suggest we incorporate that here too.
We don't store the current version anywhere in the codebase, but it's easy to do that in a maintainable way. We could have a specific constant somewhere which we replace its value for as part of the release process. It could even be via a simple vibe coded script.
There was a problem hiding this comment.
I added an AiClient::VERSION. It seemed like a sensible place to put it since this a client package and that's the main class. How does that sound?
Tentatively resolved in 5ccaee3
|
|
||
| // Store in cache. | ||
| if ($cache !== null) { | ||
| $cache->set($cacheKey, $this->dehydrateModelMetadataMap($modelMetadataMap)); |
There was a problem hiding this comment.
What's the TTL? We'll need a mechanism for the cache to invalidate.
My suggestion is to use a mix of both:
- Set a reasonable TTL, e.g. for a couple hours, maybe a day.
- Add some public method to invalidate the cache. We could introduce an interface like
WithCachedDataInterfacethat has a single methodinvalidateCaches()that would invalidate all relevant caches for the class. And ourModelMetadataDirectoryimplementations for cloud providers could make use of that.
There was a problem hiding this comment.
I updated the trait in 3981997 to handle the invalidateCaches() method.
I also set a 1-day TTL. 👍
src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleModelMetadataDirectory.php
Show resolved
Hide resolved
felixarntz
left a comment
There was a problem hiding this comment.
@JasonTheAdams Almost good to go, only one important architectural consideration.
src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleModelMetadataDirectory.php
Outdated
Show resolved
Hide resolved
|
@JasonTheAdams Since you're adding the (In the future it would be great to automate this, but I think for now this is useful to at least document.) |
felixarntz
left a comment
There was a problem hiding this comment.
@JasonTheAdams LGTM, just one thing that I think is worth improving. Please check before merging, feel free to merge after updating, or let me know if you disagree.
| protected function getBaseCacheKey(): string | ||
| { | ||
| return 'ai_client_' . AiClient::VERSION . '_' . md5(static::class); | ||
| } |
There was a problem hiding this comment.
Why not put this in the trait? This implementation here does nothing that wouldn't be suitable for the trait itself - it's not tied to this specific class.
There was a problem hiding this comment.
I thought about this, but I didn't want to assume that we always need the class on the end of the key. We really only need it here because this is an abstract class. But another class may not want that in the key, which would also also keys to be shared between contexts.
There was a problem hiding this comment.
Agreed, it doesn't always need to be like that, but a protected method would still allow changing it. So IMO having this implementation in the trait as "works for ~90% of cases" would still be helpful.
But not a big deal, we can also iterate later, as changing that would not break anything.
Resolves #141
This PR adds optional PSR-16 caching support to the AI Client. When a cache is configured, model metadata from provider APIs (like the list of available models) is stored and reused instead of making repeated API calls. This improves performance and reduces API usage, especially useful in applications that frequently initialize the client or query model availability.
What's new: