UNPKG

@sap-ai-sdk/ai-api

Version:

SAP Cloud SDK for AI is the official Software Development Kit (SDK) for **SAP AI Core**, **SAP Generative AI Hub**, and **Orchestration Service**.

97 lines 3.87 kB
import { ErrorWithCause } from '@sap-cloud-sdk/util'; import { DeploymentApi } from '../client/AI_CORE_API/index.js'; import { deploymentCache } from './deployment-cache.js'; import { extractModel } from './model.js'; /** * @internal */ export function getResourceGroup(modelDeployment) { return typeof modelDeployment === 'object' ? modelDeployment.resourceGroup : undefined; } /** * Type guard to check if the given deployment configuration is a deployment ID configuration. * @param modelDeployment - Configuration to check. * @returns `true` if the configuration is a deployment ID configuration, `false` otherwise. */ function isDeploymentIdConfig(modelDeployment) { return (typeof modelDeployment === 'object' && 'deploymentId' in modelDeployment); } /** * Query the AI Core service for a deployment that matches the given criteria. If more than one deployment matches the criteria, the first one's ID is returned. * @param opts - The options for the deployment resolution. * @returns A promise of a deployment, if a deployment was found, fails otherwise. * @internal */ export async function resolveDeploymentId(opts) { const { model } = opts; const cachedDeployment = deploymentCache.get(opts); if (cachedDeployment?.id) { return cachedDeployment.id; } let deployments = await getAllDeployments(opts); if (model) { deployments = deployments.filter(deployment => extractModel(deployment)?.name === model.name); if (model.version) { deployments = deployments.filter(deployment => extractModel(deployment)?.version === model.version); } } if (!deployments.length) { throw new Error(`No deployment matched the given criteria: ${JSON.stringify(opts)}. Make sure the deployment is successful, as it is a prerequisite before consuming orchestration or foundation models.`); } return deployments[0].id; } /** * Get all deployments that match the given criteria. * @param opts - The options for the deployment resolution. * @returns A promise of an array of deployments. * @internal */ export async function getAllDeployments(opts) { const { destination, scenarioId, executableId, resourceGroup = 'default' } = opts; try { const { resources } = await DeploymentApi.deploymentQuery({ scenarioId, status: 'RUNNING', ...(executableId && { executableIds: [executableId] }) }, { 'AI-Resource-Group': resourceGroup }).execute(destination); deploymentCache.setAll(opts, resources); return resources; } catch (error) { throw new ErrorWithCause('Failed to fetch the list of deployments.', error); } } /** * Get the deployment ID for a given model deployment configuration and executable ID using the 'foundation-models' scenario. * @param modelDeployment - The model deployment configuration. * @param executableId - The executable ID. * @returns The ID of the deployment, if found. * @internal */ export async function getDeploymentId(modelDeployment, executableId, destination) { if (isDeploymentIdConfig(modelDeployment)) { return modelDeployment.deploymentId; } const model = typeof modelDeployment === 'string' ? { modelName: modelDeployment } : modelDeployment; return resolveDeploymentId({ scenarioId: 'foundation-models', executableId, model: translateToFoundationModel(model), resourceGroup: model.resourceGroup, destination }); } function translateToFoundationModel(modelConfig) { if (typeof modelConfig === 'string') { return { name: modelConfig }; } return { name: modelConfig.modelName, ...(modelConfig.modelVersion && { version: modelConfig.modelVersion }) }; } //# sourceMappingURL=deployment-resolver.js.map