import {z} from 'zod' export type GatewayAwsS3Config = {credentialsSecret?: string | undefined, iamRoleArn?: string | undefined}; export type GatewayAzureBlobStorageConfig = {credentialsSecret?: string | undefined, managedIdentityClientId?: string | undefined, tenantId?: string | undefined}; export type GatewayJobState = 'JOB_STATE_UNSPECIFIED' | 'JOB_STATE_CREATING' | 'JOB_STATE_RUNNING' | 'JOB_STATE_COMPLETED' | 'JOB_STATE_FAILED' | 'JOB_STATE_CANCELLED' | 'JOB_STATE_DELETING' | 'JOB_STATE_WRITING_RESULTS' | 'JOB_STATE_VALIDATING' | 'JOB_STATE_DELETING_CLEANING_UP' | 'JOB_STATE_PENDING' | 'JOB_STATE_EXPIRED' | 'JOB_STATE_RE_QUEUEING' | 'JOB_STATE_CREATING_INPUT_DATASET' | 'JOB_STATE_IDLE' | 'JOB_STATE_CANCELLING' | 'JOB_STATE_EARLY_STOPPED' | 'JOB_STATE_PAUSED' | 'JOB_STATE_DELETED' | 'JOB_STATE_ARCHIVED'; export type GatewayCode = 'OK' | 'CANCELLED' | 'UNKNOWN' | 'INVALID_ARGUMENT' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'ALREADY_EXISTS' | 'PERMISSION_DENIED' | 'UNAUTHENTICATED' | 'RESOURCE_EXHAUSTED' | 'FAILED_PRECONDITION' | 'ABORTED' | 'OUT_OF_RANGE' | 'UNIMPLEMENTED' | 'INTERNAL' | 'UNAVAILABLE' | 'DATA_LOSS'; export type GatewayStatus = {code?: GatewayCode | undefined, message?: string | undefined}; export type GatewayWandbConfig = {enabled?: boolean | undefined, apiKey?: string | undefined, project?: string | undefined, entity?: string | undefined, runId?: string | undefined, url?: string | undefined}; export type GatewayJobProgress = {percent?: number | undefined, epoch?: number | undefined, totalInputRequests?: number | undefined, totalProcessedRequests?: number | undefined, successfullyProcessedRequests?: number | undefined, failedRequests?: number | undefined, outputRows?: number | undefined, inputTokens?: number | undefined, outputTokens?: number | undefined, cachedInputTokenCount?: number | undefined}; export type GatewayConstantLRSchedule = Record; export type GatewayLinearLRSchedule = {minLrRatio?: number | undefined, decayRatio?: number | undefined}; export type GatewayCosineLRSchedule = {minLrRatio?: number | undefined, decayRatio?: number | undefined}; export type GatewayLearningRateScheduler = {constant?: GatewayConstantLRSchedule | undefined, linear?: GatewayLinearLRSchedule | undefined, cosine?: GatewayCosineLRSchedule | undefined}; export type TypeMoney = {currencyCode?: string | undefined, units?: unknown | undefined, nanos?: number | undefined}; export type GatewayPurpose = 'PURPOSE_UNSPECIFIED' | 'PURPOSE_PILOT'; export type GatewayEncryptionState = 'ENCRYPTION_STATE_UNSPECIFIED' | 'ENCRYPTION_STATE_PLAINTEXT' | 'ENCRYPTION_STATE_CMEK'; export type GatewaySupervisedFineTuningJob = {name?: string | undefined, displayName?: string | undefined, createTime?: string | undefined, completedTime?: string | undefined, dataset: string, awsS3Config?: GatewayAwsS3Config | undefined, azureBlobStorageConfig?: GatewayAzureBlobStorageConfig | undefined, state?: GatewayJobState | undefined, status?: GatewayStatus | undefined, createdBy?: string | undefined, outputModel?: string | undefined, baseModel?: string | undefined, warmStartFrom?: string | undefined, jinjaTemplate?: string | undefined, earlyStop?: boolean | undefined, epochs?: number | undefined, learningRate?: number | undefined, maxContextLength?: number | undefined, loraRank?: number | undefined, wandbConfig?: GatewayWandbConfig | undefined, evaluationDataset?: string | undefined, isTurbo?: boolean | undefined, evalAutoCarveout?: boolean | undefined, updateTime?: string | undefined, nodes?: number | undefined, batchSize?: number | undefined, mtpEnabled?: boolean | undefined, mtpNumDraftTokens?: number | undefined, mtpFreezeBaseModel?: boolean | undefined, jobProgress?: GatewayJobProgress | undefined, metricsFileSignedUrl?: string | undefined, trainerLogsSignedUrl?: string | undefined, renderSamplesSignedUrl?: string | undefined, gradientAccumulationSteps?: number | undefined, learningRateWarmupSteps?: number | undefined, lrScheduler?: GatewayLearningRateScheduler | undefined, batchSizeSamples?: number | undefined, estimatedCost?: TypeMoney | undefined, optimizerWeightDecay?: number | undefined, purpose?: GatewayPurpose | undefined, encryptionState?: GatewayEncryptionState | undefined}; export type CreateApiV1AccountsAccountIdSupervisedFineTuningJobsEstimateCostArgs = {account_id: string, body: GatewaySupervisedFineTuningJob}; export type CreateApiV1AccountsAccountIdSupervisedFineTuningJobsEstimateCostOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const typeMoney = z.object({currencyCode: z.string().optional(), units: z.string().optional(), nanos: z.number().int().optional()}); export const gatewayEstimateSupervisedFineTuningJobCostResponse = z.object({estimatedCost: typeMoney.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Estimate the cost of a Supervised Fine-tuning Job * * POST /v1/accounts/{account_id}/supervisedFineTuningJobs:estimateCost */ export const createApiV1AccountsAccountIdSupervisedFineTuningJobsEstimateCost = async (args: CreateApiV1AccountsAccountIdSupervisedFineTuningJobsEstimateCostArgs, options: CreateApiV1AccountsAccountIdSupervisedFineTuningJobsEstimateCostOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/supervisedFineTuningJobs:estimateCost${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gatewayEstimateSupervisedFineTuningJobCostResponse, await res.json()) };