getData(); if (!is_array($responseData) || !$responseData) { throw ResponseException::fromMissingData('Together AI', 'models'); } /* * Together AI returns a bare list of models, but tolerate the OpenAI-style * `{ "data": [...] }` envelope as well in case the response shape changes. */ if (isset($responseData['data']) && is_array($responseData['data'])) { $modelsData = $responseData['data']; } else { $modelsData = $responseData; } $capabilities = [ CapabilityEnum::textGeneration(), CapabilityEnum::chatHistory(), ]; $baseOptions = [ 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::outputMimeType(), ['text/plain', 'application/json']), new SupportedOption(OptionEnum::outputSchema()), new SupportedOption(OptionEnum::functionDeclarations()), new SupportedOption(OptionEnum::customOptions()), ]; $textOptions = array_merge($baseOptions, [ new SupportedOption(OptionEnum::inputModalities(), [[ModalityEnum::text()]]), new SupportedOption(OptionEnum::outputModalities(), [[ModalityEnum::text()]]), ]); $multimodalInputOptions = array_merge($baseOptions, [ new SupportedOption( OptionEnum::inputModalities(), [ [ModalityEnum::text()], [ModalityEnum::text(), ModalityEnum::image()], ] ), new SupportedOption(OptionEnum::outputModalities(), [[ModalityEnum::text()]]), ]); $models = []; foreach ($modelsData as $modelData) { if (!is_array($modelData) || !isset($modelData['id'])) { continue; } // Only chat-capable models work with the Chat Completions endpoint. if (isset($modelData['type']) && $modelData['type'] !== 'chat') { continue; } $modelId = (string) $modelData['id']; $displayName = isset($modelData['display_name']) && $modelData['display_name'] !== '' ? (string) $modelData['display_name'] : $modelId; $modelOptions = self::supportsMultimodalTextInput($modelId) ? $multimodalInputOptions : $textOptions; $models[] = new ModelMetadata( $modelId, $displayName, $capabilities, $modelOptions ); } if (!$models) { throw ResponseException::fromMissingData('Together AI', 'models'); } usort($models, [$this, 'modelSortCallback']); return $models; } /** * Checks whether a Together AI model supports multimodal (image) input. * * Vision-capable model repositories commonly tag their IDs with "vl" * (vision-language), "vision", or the "llava" family name. * * @since 1.0.0 * * @param string $modelId The model ID. * @return bool True if the model supports multimodal text input, false otherwise. */ private static function supportsMultimodalTextInput(string $modelId): bool { $needle = strtolower($modelId); return str_contains($needle, '-vl-') || str_contains($needle, '-vl') || str_contains($needle, 'vision') || str_contains($needle, 'llava'); } /** * Callback function for sorting models, to be used with `usort()`. * * Together AI aggregates many models with no inherent ranking, so they are sorted by * display name (case-insensitively) for a stable, predictable listing. * * @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 { return strcasecmp($a->getName(), $b->getName()); } }