import {z} from 'zod' export type DatasetReference = {name: string, version?: (string | null) | undefined}; export type RLConfig = {max_steps?: (number | null) | undefined, kl_beta?: (number | null) | undefined, group_size?: (number | null) | undefined, sampling_temperature?: (number | null) | undefined, max_completion_length?: (number | null) | undefined, logging_steps?: (number | null) | undefined, dpo_beta?: (number | null) | undefined, loss_type?: (string | null) | undefined, reward_type?: (string | null) | undefined, extras?: {[key: string]: unknown} | undefined}; export type TrainingPipelineStageCreate = {base_model: string, training_type?: ('full' | 'lora' | 'qlora') | undefined, training_algorithm?: ('sft' | 'dpo' | 'grpo') | undefined, datasets: Array, parent_stage_index?: (number | null) | undefined, merge_parent_before?: boolean | undefined, rl_config?: (RLConfig | null) | undefined}; export type TrainingPipelineCreate = {model_name: string, project_id?: (string | null) | undefined, stages: Array, auto_deploy_final_stage?: boolean | undefined}; export type CreateApiV1TrainingPipelinesArgs = {body: TrainingPipelineCreate}; export type CreateApiV1TrainingPipelinesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const trainingPipelineStageCreateResponse = z.object({job_id: z.string(), stage_index: z.number().int(), status: z.string(), job_reference: z.string().nullable().optional(), provider_name: z.string().nullable().optional()}); export const trainingPipelineCreateResponse = z.object({pipeline_id: z.string(), status: z.string(), estimated_total_usd: z.number().nullable().optional(), stages: z.array(trainingPipelineStageCreateResponse)}); 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 Training Pipeline * * POST /v1/training-pipelines * * Also accepts auth: BearerAuth */ export const createApiV1TrainingPipelines = async (args: CreateApiV1TrainingPipelinesArgs, options: CreateApiV1TrainingPipelinesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/training-pipelines${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(trainingPipelineCreateResponse, await res.json()) };