* } */ class CerebrasModelMetadataDirectory extends AbstractOpenAiCompatibleModelMetadataDirectory { /** * {@inheritDoc} * * @since 1.0.0 */ protected function createRequest(HttpMethodEnum $method, string $path, array $headers = [], $data = null): Request { return new Request( $method, CerebrasProvider::url($path), $headers, $data ); } /** * {@inheritDoc} * * @since 1.0.0 */ protected function parseResponseToModelMetadataList(Response $response): array { /** @var ModelsResponseData $responseData */ $responseData = $response->getData(); if (!isset($responseData['data']) || !$responseData['data']) { throw ResponseException::fromMissingData('Cerebras', 'data'); } $cerebrasCapabilities = [ CapabilityEnum::textGeneration(), CapabilityEnum::chatHistory(), ]; $cerebrasTextOptions = [ new SupportedOption(OptionEnum::systemInstruction()), new SupportedOption(OptionEnum::candidateCount()), new SupportedOption(OptionEnum::maxTokens()), new SupportedOption(OptionEnum::temperature()), new SupportedOption(OptionEnum::topP()), new SupportedOption(OptionEnum::stopSequences()), new SupportedOption(OptionEnum::presencePenalty()), new SupportedOption(OptionEnum::frequencyPenalty()), new SupportedOption(OptionEnum::logprobs()), new SupportedOption(OptionEnum::topLogprobs()), new SupportedOption(OptionEnum::outputMimeType(), ['text/plain', 'application/json']), new SupportedOption(OptionEnum::outputSchema()), new SupportedOption(OptionEnum::functionDeclarations()), new SupportedOption(OptionEnum::customOptions()), new SupportedOption(OptionEnum::inputModalities(), [[ModalityEnum::text()]]), new SupportedOption(OptionEnum::outputModalities(), [[ModalityEnum::text()]]), ]; $modelsData = (array) $responseData['data']; $models = array_values( array_map( static function (array $modelData) use ( $cerebrasCapabilities, $cerebrasTextOptions ): ModelMetadata { $modelId = $modelData['id']; return new ModelMetadata( $modelId, $modelId, // The Cerebras API does not return a display name. $cerebrasCapabilities, $cerebrasTextOptions ); }, $modelsData ) ); usort($models, [$this, 'modelSortCallback']); return $models; } /** * Checks whether a Cerebras model is a utility model (e.g. safety/guard models). * * Utility models are functional but not general-purpose text generators, so they * are sorted to the bottom of the list. * * @since 1.0.0 * * @param string $modelId The model ID. * @return bool True if the model is a utility model, false otherwise. */ private static function isUtilityModel(string $modelId): bool { return str_contains($modelId, 'guard'); } /** * Callback function for sorting models by ID, to be used with `usort()`. * * This method expresses preferences for certain models or model families within the provider by putting them * earlier in the sorted list. The objective is not to be opinionated about which models are better, but to ensure * that more commonly used, more recent, or flagship models are presented first to users. * * @since 1.0.0 * * @param ModelMetadata $a First model. * @param ModelMetadata $b Second model. * @return int Comparison result. */ protected function modelSortCallback(ModelMetadata $a, ModelMetadata $b): int { $aId = $a->getId(); $bId = $b->getId(); // Push utility models (safety/guard) to the bottom. $aUtil = self::isUtilityModel($aId); $bUtil = self::isUtilityModel($bId); if ($aUtil !== $bUtil) { return $aUtil ? 1 : -1; } // Prefer the widely used flagship families, in a stable order. $aRank = self::familyRank($aId); $bRank = self::familyRank($bId); if ($aRank !== $bRank) { return $aRank <=> $bRank; } // Fallback: Sort alphabetically. return strcmp($aId, $bId); } /** * Returns a sort rank for a model based on its family, lower being earlier. * * @since 1.0.0 * * @param string $modelId The model ID. * @return int The family rank (lower sorts first). */ private static function familyRank(string $modelId): int { $families = ['llama', 'qwen', 'gpt-oss', 'gemma', 'deepseek']; foreach ($families as $rank => $family) { if (str_contains($modelId, $family)) { return $rank; } } return count($families); } }