-
Notifications
You must be signed in to change notification settings - Fork 70
Avoid listing models for explicit text model IDs #231
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,18 @@ final public function hasModelMetadata(string $modelId): bool | |
| */ | ||
| final public function getModelMetadata(string $modelId): ModelMetadata | ||
| { | ||
| if ($this->hasCache(self::MODELS_CACHE_KEY)) { | ||
| $modelsMetadata = $this->getModelMetadataMap(); | ||
| if (isset($modelsMetadata[$modelId])) { | ||
| return $modelsMetadata[$modelId]; | ||
| } | ||
| } | ||
|
|
||
| $explicitModelMetadata = $this->createModelMetadataForExplicitModelId($modelId); | ||
| if ($explicitModelMetadata !== null) { | ||
| return $explicitModelMetadata; | ||
| } | ||
|
Comment on lines
+78
to
+81
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a bit of a strange design that this will only be used if no cached value has been found, especially since this injected model metadata does not get cached itself. what if the data from the API differs from what's locally defined in the code? Either we need to cache the result of this too (which I'd have other concerns over though because of cache invalidation) or we need to always run this before the cache logic. If the implementer wants to add some kind of caching themselves for it, they still could.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we should run it before cache logic. I'm trying to keep this as simple as possible. |
||
|
|
||
| $modelsMetadata = $this->getModelMetadataMap(); | ||
| if (!isset($modelsMetadata[$modelId])) { | ||
| throw new InvalidArgumentException( | ||
|
|
@@ -114,6 +126,22 @@ protected function getBaseCacheKey(): string | |
| return 'ai_client_' . AiClient::VERSION . '_' . md5(static::class); | ||
| } | ||
|
|
||
| /** | ||
| * Creates metadata for an explicit model ID without listing provider models. | ||
| * | ||
| * Providers whose APIs accept arbitrary/current model IDs can override this to avoid a live list-models request | ||
| * when callers already know the model ID they want to instantiate. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $modelId The explicit model ID. | ||
| * @return ModelMetadata|null The model metadata, or null to fall back to listing provider models. | ||
| */ | ||
| protected function createModelMetadataForExplicitModelId(string $modelId): ?ModelMetadata | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Sends the API request to list models from the provider and returns the map of model ID to model metadata. | ||
| * | ||
|
|
||
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.
This being only in
getModelMetadata()but not inlistModelMetdata()will lead to inconsistent data - so the approach doesn't work correctly in terms of the full API.I understand this is meant to be used to bypass the need for a request in case an explicit model ID is passed, but that's not controlled by this class. Any code could call
getModelMetadata()here, so it'd be confusing ifgetModelMetadata()returned a different set of metadata than the same model would appear with as part oflistModelMetadata().I think there's a way to implement this in a way that the code here still bypasses the request while ensuring consistent data. E.g. for
listModelMetadata()we could make the request first (necessary there!), and then for every single model ID call this method. If the method returns something, override what the API returned - this way it's the same result as here.There's performance and data integrity to consider on that path - we'd probably want to build in memoization and potentially even caching of this so that:
listModelMetadata()is calledIt's not super trivial, but I think it's possible.
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.
Thanks for the review and thoughtful comments!
I think this is mainly a problem on slow connections or local environments where the CPU/RAM is constrained. I have not seen it on my live sites but have seen it repeatedly in a local environment inside Studio/WordPress Playground.
I will revisit this based on your feedback and update the PR to consider the broader implications.