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 CreateApiV1AccountsAccountIdSupervisedFineTuningJobsArgs = {supervisedFineTuningJobId?: string | undefined, account_id: string, body: GatewaySupervisedFineTuningJob}; export type CreateApiV1AccountsAccountIdSupervisedFineTuningJobsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayAwsS3Config = z.object({credentialsSecret: z.string().optional(), iamRoleArn: z.string().optional()}); export const gatewayAzureBlobStorageConfig = z.object({credentialsSecret: z.string().optional(), managedIdentityClientId: z.string().optional(), tenantId: z.string().optional()}); export const gatewayJobState = z.enum(["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 const gatewayCode = z.enum(["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 const gatewayStatus = z.object({code: gatewayCode.optional(), message: z.string().optional()}); export const gatewayWandbConfig = z.object({enabled: z.boolean().optional(), apiKey: z.string().optional(), project: z.string().optional(), entity: z.string().optional(), runId: z.string().optional(), url: z.string().optional()}); export const gatewayJobProgress = z.object({percent: z.number().int().optional(), epoch: z.number().int().optional(), totalInputRequests: z.number().int().optional(), totalProcessedRequests: z.number().int().optional(), successfullyProcessedRequests: z.number().int().optional(), failedRequests: z.number().int().optional(), outputRows: z.number().int().optional(), inputTokens: z.number().int().optional(), outputTokens: z.number().int().optional(), cachedInputTokenCount: z.number().int().optional()}); export const gatewayConstantLRSchedule = z.object({}); export const gatewayLinearLRSchedule = z.object({minLrRatio: z.number().optional(), decayRatio: z.number().optional()}); export const gatewayCosineLRSchedule = z.object({minLrRatio: z.number().optional(), decayRatio: z.number().optional()}); export const gatewayLearningRateScheduler = z.object({constant: gatewayConstantLRSchedule.optional(), linear: gatewayLinearLRSchedule.optional(), cosine: gatewayCosineLRSchedule.optional()}); export const typeMoney = z.object({currencyCode: z.string().optional(), units: z.string().optional(), nanos: z.number().int().optional()}); export const gatewayPurpose = z.enum(["PURPOSE_UNSPECIFIED", "PURPOSE_PILOT"]); export const gatewayEncryptionState = z.enum(["ENCRYPTION_STATE_UNSPECIFIED", "ENCRYPTION_STATE_PLAINTEXT", "ENCRYPTION_STATE_CMEK"]); export const gatewaySupervisedFineTuningJob = z.object({name: z.string().optional(), displayName: z.string().optional(), createTime: z.string().optional(), completedTime: z.string().optional(), dataset: z.string(), awsS3Config: gatewayAwsS3Config.optional(), azureBlobStorageConfig: gatewayAzureBlobStorageConfig.optional(), state: gatewayJobState.optional(), status: gatewayStatus.optional(), createdBy: z.string().optional(), outputModel: z.string().optional(), baseModel: z.string().optional(), warmStartFrom: z.string().optional(), jinjaTemplate: z.string().optional(), earlyStop: z.boolean().optional(), epochs: z.number().int().optional(), learningRate: z.number().optional(), maxContextLength: z.number().int().optional(), loraRank: z.number().int().optional(), wandbConfig: gatewayWandbConfig.optional(), evaluationDataset: z.string().optional(), isTurbo: z.boolean().optional(), evalAutoCarveout: z.boolean().optional(), updateTime: z.string().optional(), nodes: z.number().int().optional(), batchSize: z.number().int().optional(), mtpEnabled: z.boolean().optional(), mtpNumDraftTokens: z.number().int().optional(), mtpFreezeBaseModel: z.boolean().optional(), jobProgress: gatewayJobProgress.optional(), metricsFileSignedUrl: z.string().optional(), trainerLogsSignedUrl: z.string().optional(), renderSamplesSignedUrl: z.string().optional(), gradientAccumulationSteps: z.number().int().optional(), learningRateWarmupSteps: z.number().int().optional(), lrScheduler: gatewayLearningRateScheduler.optional(), batchSizeSamples: z.number().int().optional(), estimatedCost: typeMoney.optional(), optimizerWeightDecay: z.number().optional(), purpose: gatewayPurpose.optional(), encryptionState: gatewayEncryptionState.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 }; /** * Create Supervised Fine-tuning Job * * POST /v1/accounts/{account_id}/supervisedFineTuningJobs */ export const createApiV1AccountsAccountIdSupervisedFineTuningJobs = async (args: CreateApiV1AccountsAccountIdSupervisedFineTuningJobsArgs, options: CreateApiV1AccountsAccountIdSupervisedFineTuningJobsOptions) => { 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 (args.supervisedFineTuningJobId != null) query.append("supervisedFineTuningJobId", String(args.supervisedFineTuningJobId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/supervisedFineTuningJobs${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(gatewaySupervisedFineTuningJob, await res.json()) };