import {z} from 'zod' export type CreateApiV1RlTrainingSessionsSessionIdOperationsTrainingCheckpointArgs = {session_id: string, 'Idempotency-Key': string}; export type CreateApiV1RlTrainingSessionsSessionIdOperationsTrainingCheckpointOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rLTrainingOperationStatus = z.enum(["TRAINING_OPERATION_STATUS_UNSPECIFIED", "TRAINING_OPERATION_STATUS_PENDING", "TRAINING_OPERATION_STATUS_RUNNING", "TRAINING_OPERATION_STATUS_COMPLETED", "TRAINING_OPERATION_STATUS_FAILED"]); export const rLModelRegistryArtifact = z.object({id: z.string(), revision_id: z.string()}); export const rLTrainingCheckpoint = z.object({id: z.string(), step: z.union([z.string(), z.number().int()]), created_at: z.string(), registration: rLModelRegistryArtifact.optional()}); export const rLTrainingCheckpointResult = z.object({checkpoint: rLTrainingCheckpoint}); export const rLTrainingOperationErrorCode = z.enum(["TRAINING_OPERATION_ERROR_CODE_UNSPECIFIED", "TRAINING_OPERATION_ERROR_CODE_RESOURCE_EXHAUSTED", "TRAINING_OPERATION_ERROR_CODE_TIMEOUT", "TRAINING_OPERATION_ERROR_CODE_INTERNAL_ERROR", "TRAINING_OPERATION_ERROR_CODE_SESSION_NOT_ACTIVE", "TRAINING_OPERATION_ERROR_CODE_INVALID_INPUT", "TRAINING_OPERATION_ERROR_CODE_NON_FINITE_LOSS"]); export const rLTrainingOperationError = z.object({code: rLTrainingOperationErrorCode.optional(), message: z.string().optional()}); export const rLTrainingCheckpointOperation = z.object({id: z.string(), status: rLTrainingOperationStatus, output: rLTrainingCheckpointResult.optional(), error: rLTrainingOperationError.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 }; /** * Save training checkpoint * * POST /v1/rl/training-sessions/{session_id}/operations/training-checkpoint */ export const createApiV1RlTrainingSessionsSessionIdOperationsTrainingCheckpoint = async (args: CreateApiV1RlTrainingSessionsSessionIdOperationsTrainingCheckpointArgs, options?: CreateApiV1RlTrainingSessionsSessionIdOperationsTrainingCheckpointOptions) => { const {baseUrl = "https://api.together.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/rl/training-sessions/${encodeURIComponent(String(args.session_id))}/operations/training-checkpoint${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(rLTrainingCheckpointOperation, await res.json()) };