import {z} from 'zod' 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 GatewayTrainerShardingScheme = {tensorParallelism?: number | undefined, pipelineParallelism?: number | undefined, contextParallelism?: number | undefined, expertParallelism?: number | undefined, sequenceParallelism?: boolean | 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 GatewayBaseTrainingConfig = {outputModel?: string | undefined, baseModel?: string | undefined, warmStartFrom?: string | undefined, jinjaTemplate?: string | undefined, learningRate?: number | undefined, maxContextLength?: number | undefined, loraRank?: number | undefined, epochs?: number | undefined, batchSize?: number | undefined, gradientAccumulationSteps?: number | undefined, learningRateWarmupSteps?: number | undefined, batchSizeSamples?: number | undefined, optimizerWeightDecay?: number | undefined, trainerShardingScheme?: GatewayTrainerShardingScheme | undefined, loraAlpha?: number | undefined, loraDropout?: number | undefined, loraTargetModules?: Array | undefined, lrScheduler?: GatewayLearningRateScheduler | undefined}; export type GatewayWandbConfig = {enabled?: boolean | undefined, apiKey?: string | undefined, project?: string | undefined, entity?: string | undefined, runId?: string | undefined, url?: string | undefined}; export type ReinforcementLearningLossConfigMethod = 'METHOD_UNSPECIFIED' | 'GRPO' | 'DAPO' | 'DPO' | 'ORPO' | 'GSPO_TOKEN'; export type GatewayDpoConfig = {beta?: number | undefined, refCacheConcurrency?: number | undefined, refCacheBatchSize?: number | undefined}; export type GatewayOrpoConfig = {lambda?: number | undefined}; export type GatewayReinforcementLearningLossConfig = {method?: ReinforcementLearningLossConfigMethod | undefined, klBeta?: number | undefined, dpo?: GatewayDpoConfig | undefined, orpo?: GatewayOrpoConfig | undefined}; export type GatewayAwsS3Config = {credentialsSecret?: string | undefined, iamRoleArn?: string | undefined}; export type GatewayAzureBlobStorageConfig = {credentialsSecret?: string | undefined, managedIdentityClientId?: string | undefined, tenantId?: string | undefined}; export type GatewayPurpose = 'PURPOSE_UNSPECIFIED' | 'PURPOSE_PILOT'; export type GatewayDpoJob = {name?: string | undefined, displayName?: string | undefined, createTime?: string | undefined, completedTime?: string | undefined, dataset: string, state?: GatewayJobState | undefined, status?: GatewayStatus | undefined, createdBy?: string | undefined, trainingConfig?: GatewayBaseTrainingConfig | undefined, wandbConfig?: GatewayWandbConfig | undefined, trainerLogsSignedUrl?: string | undefined, lossConfig?: GatewayReinforcementLearningLossConfig | undefined, awsS3Config?: GatewayAwsS3Config | undefined, azureBlobStorageConfig?: GatewayAzureBlobStorageConfig | undefined, purpose?: GatewayPurpose | undefined, useReservation?: boolean | undefined}; export type CreateApiV1AccountsAccountIdDpoJobsArgs = {dpoJobId?: string | undefined, account_id: string, body: GatewayDpoJob}; export type CreateApiV1AccountsAccountIdDpoJobsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 gatewayTrainerShardingScheme = z.object({tensorParallelism: z.number().int().optional(), pipelineParallelism: z.number().int().optional(), contextParallelism: z.number().int().optional(), expertParallelism: z.number().int().optional(), sequenceParallelism: z.boolean().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 gatewayBaseTrainingConfig = z.object({outputModel: z.string().optional(), baseModel: z.string().optional(), warmStartFrom: z.string().optional(), jinjaTemplate: z.string().optional(), learningRate: z.number().optional(), maxContextLength: z.number().int().optional(), loraRank: z.number().int().optional(), epochs: z.number().int().optional(), batchSize: z.number().int().optional(), gradientAccumulationSteps: z.number().int().optional(), learningRateWarmupSteps: z.number().int().optional(), batchSizeSamples: z.number().int().optional(), optimizerWeightDecay: z.number().optional(), trainerShardingScheme: gatewayTrainerShardingScheme.optional(), loraAlpha: z.number().int().optional(), loraDropout: z.number().optional(), loraTargetModules: z.array(z.string()).optional(), lrScheduler: gatewayLearningRateScheduler.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 reinforcementLearningLossConfigMethod = z.enum(["METHOD_UNSPECIFIED", "GRPO", "DAPO", "DPO", "ORPO", "GSPO_TOKEN"]); export const gatewayDpoConfig = z.object({beta: z.number().optional(), refCacheConcurrency: z.number().int().optional(), refCacheBatchSize: z.number().int().optional()}); export const gatewayOrpoConfig = z.object({lambda: z.number().optional()}); export const gatewayReinforcementLearningLossConfig = z.object({method: reinforcementLearningLossConfigMethod.optional(), klBeta: z.number().optional(), dpo: gatewayDpoConfig.optional(), orpo: gatewayOrpoConfig.optional()}); 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 gatewayPurpose = z.enum(["PURPOSE_UNSPECIFIED", "PURPOSE_PILOT"]); export const gatewayDpoJob = z.object({name: z.string().optional(), displayName: z.string().optional(), createTime: z.string().optional(), completedTime: z.string().optional(), dataset: z.string(), state: gatewayJobState.optional(), status: gatewayStatus.optional(), createdBy: z.string().optional(), trainingConfig: gatewayBaseTrainingConfig.optional(), wandbConfig: gatewayWandbConfig.optional(), trainerLogsSignedUrl: z.string().optional(), lossConfig: gatewayReinforcementLearningLossConfig.optional(), awsS3Config: gatewayAwsS3Config.optional(), azureBlobStorageConfig: gatewayAzureBlobStorageConfig.optional(), purpose: gatewayPurpose.optional(), useReservation: z.boolean().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 }; /** * POST /v1/accounts/{account_id}/dpoJobs */ export const createApiV1AccountsAccountIdDpoJobs = async (args: CreateApiV1AccountsAccountIdDpoJobsArgs, options: CreateApiV1AccountsAccountIdDpoJobsOptions) => { 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.dpoJobId != null) query.append("dpoJobId", String(args.dpoJobId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/dpoJobs${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(gatewayDpoJob, await res.json()) };