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**.

82 lines 3.37 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; } /** * 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 resolveDeployment(opts) { const { model } = opts; 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]; } /** * 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 cachedDeployment = deploymentCache.get(opts); if (cachedDeployment?.id) { return cachedDeployment.id; } return (await resolveDeployment(opts)).id; } /** * 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 URL is returned. * @param opts - The options for the deployment resolution. * @returns A promise of the deployment URL, if a deployment was found, fails otherwise. */ export async function resolveDeploymentUrl(opts) { const cachedDeployment = deploymentCache.get(opts); if (cachedDeployment?.url) { return cachedDeployment.url; } return (await resolveDeployment(opts)).deploymentUrl; } /** * 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); } } //# sourceMappingURL=deployment-resolver.js.map