import {z} from 'zod' export type FineTuningJobCreate = {base_model_name: string, dataset_name: string, parameters: {batch_size?: number | undefined, num_epochs?: number | undefined, shuffle?: boolean | undefined, lr?: number | undefined, seed?: number | undefined} | {[key: string]: unknown}, name: string, type: 'LORA' | 'QLORA'}; export type CreateApiV1FineTuningArgs = {body: FineTuningJobCreate}; export type CreateApiV1FineTuningOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fineTuningJobResponse = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), status: z.enum(["NEW", "QUEUED", "RUNNING", "COMPLETED", "FAILED", "STOPPING", "STOPPED"]).optional(), base_model_name: z.string().optional(), dataset_name: z.string().optional(), name: z.string().optional(), type: z.enum(["LORA", "QLORA"]).optional(), current_step: z.number().int().optional(), total_steps: z.number().int().optional(), current_epoch: z.number().int().optional(), total_epochs: z.number().int().optional(), num_tokens: z.number().int().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 a new fine-tuning job * * POST /v1/fine-tuning */ export const createApiV1FineTuning = async (args: CreateApiV1FineTuningArgs, options: CreateApiV1FineTuningOptions) => { 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/fine-tuning${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(fineTuningJobResponse, await res.json()) };