1313use Cortex \ModelInfo \Providers \Concerns \ChecksSupport ;
1414use Cortex \ModelInfo \Providers \Concerns \MakesRequests ;
1515
16+ /**
17+ * @phpstan-type OllamaModelsResponse array{name: string}
18+ * @phpstan-type OllamaModelInfoResponse array{name: string, model_info: array<string, mixed>|null, capabilities: array<array-key, string>|null}
19+ */
1620class OllamaModelInfoProvider implements ModelInfoProvider
1721{
1822 use ChecksSupport;
@@ -35,31 +39,42 @@ public function supportedModelProviders(): array
3539 /**
3640 * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
3741 *
38- * @return array<array-key, string >
42+ * @return array<array-key, \Cortex\ModelInfo\Data\ModelInfo >
3943 */
4044 public function getModels (ModelProvider $ modelProvider ): array
4145 {
4246 $ this ->checkSupportOrFail ($ modelProvider );
4347
4448 $ body = $ this ->getModelsResponse ();
4549
46- return array_map (
47- // @phpstan-ignore return.type,argument.type
48- fn (array $ model ): string => $ model ['name ' ],
50+ return array_values (array_map (
51+ fn (array $ model ): ModelInfo => self ::mapModelInfo ($ model ),
4952 $ body ['models ' ],
50- );
53+ )) ;
5154 }
5255
56+ /**
57+ * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
58+ */
5359 public function getModelInfo (ModelProvider $ modelProvider , string $ model ): ModelInfo
5460 {
5561 $ this ->checkSupportOrFail ($ modelProvider );
5662
57- $ body = $ this ->getModelInfoResponse ($ model );
58- $ modelInfo = $ body ['model_info ' ] ?? [];
59- $ capabilities = $ body ['capabilities ' ] ?? [];
63+ return self ::mapModelInfo (
64+ $ this ->getModelInfoResponse ($ model ),
65+ );
66+ }
67+
68+ /**
69+ * @param OllamaModelInfoResponse|OllamaModelsResponse $modelResponseBody
70+ */
71+ protected static function mapModelInfo (array $ modelResponseBody ): ModelInfo
72+ {
73+ $ modelInfo = $ modelResponseBody ['model_info ' ] ?? [];
74+ $ capabilities = $ modelResponseBody ['capabilities ' ] ?? [];
6075
6176 return new ModelInfo (
62- name: $ model ,
77+ name: $ modelResponseBody [ ' name ' ] ,
6378 provider: ModelProvider::Ollama,
6479 type: self ::getModelType ($ capabilities ),
6580 maxInputTokens: self ::getMaxInputTokens ($ modelInfo ),
@@ -104,7 +119,7 @@ protected static function getModelType(array $capabilities): ModelType
104119 return match (true ) {
105120 in_array ('completion ' , $ capabilities , true ) => ModelType::Chat,
106121 in_array ('embedding ' , $ capabilities , true ) => ModelType::Embedding,
107- default => ModelType::Other ,
122+ default => ModelType::Unknown ,
108123 };
109124 }
110125
@@ -135,7 +150,7 @@ protected static function getMaxInputTokens(array $modelInfo): ?int
135150 /**
136151 * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
137152 *
138- * @return array{model_info: array<string, mixed>|null, capabilities: array<array-key, string>|null}
153+ * @return OllamaModelInfoResponse
139154 */
140155 protected function getModelInfoResponse (string $ model ): array
141156 {
@@ -154,7 +169,7 @@ protected function getModelInfoResponse(string $model): array
154169 /**
155170 * @throws \Cortex\ModelInfo\Exceptions\ModelInfoException
156171 *
157- * @return array{models: array{name: string} }
172+ * @return array{models: array<array-key, OllamaModelsResponse> }
158173 */
159174 protected function getModelsResponse (): array
160175 {
0 commit comments