import {z} from 'zod' export type GetApiFineTuningJobsArgs = {page?: number | undefined, page_size?: number | undefined, model?: (string | null) | undefined, created_after?: (string | null) | undefined, created_by_me?: boolean | undefined, status?: ('QUEUED' | 'STARTED' | 'VALIDATING' | 'VALIDATED' | 'RUNNING' | 'FAILED_VALIDATION' | 'FAILED' | 'SUCCESS' | 'CANCELLED' | 'CANCELLATION_REQUESTED' | null) | undefined, wandb_project?: (string | null) | undefined, wandb_name?: (string | null) | undefined, suffix?: (string | null) | undefined}; export type GetApiFineTuningJobsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jobMetadataOut = z.object({data_tokens: z.number().int().nullable().optional(), train_tokens_per_step: z.number().int().nullable().optional(), cost: z.number().nullable().optional(), cost_currency: z.string().nullable().optional(), estimated_start_time: z.number().int().nullable().optional(), expected_duration_seconds: z.number().int().nullable().optional(), train_tokens: z.number().int().nullable().optional()}); export const githubRepositoryOut = z.object({owner: z.string(), ref: z.string().nullable().optional(), name: z.string(), weight: z.number().gt(0).optional(), type: z.literal("github").optional(), commit_id: z.string().min(40).max(40)}); export const trainingParameters = z.object({fim_ratio: z.number().gte(0).lte(1).nullable().optional(), weight_decay: z.number().gte(0).lte(1).nullable().optional(), training_steps: z.number().int().gte(1).nullable().optional(), learning_rate: z.number().gte(1e-8).lte(1).optional(), epochs: z.number().gt(0).nullable().optional(), seq_len: z.number().int().gte(100).nullable().optional(), warmup_fraction: z.number().gte(0).lte(1).nullable().optional()}); export const fineTuneableModel = z.enum(["open-mistral-7b", "mistral-small-latest", "codestral-latest", "mistral-large-latest", "open-mistral-nemo", "ministral-3b-latest"]); export const wandbIntegrationOut = z.object({name: z.string().nullable().optional(), project: z.string(), type: z.literal("wandb").optional(), run_name: z.string().nullable().optional()}); export const jobOut = z.object({job_type: z.string(), metadata: jobMetadataOut.optional(), fine_tuned_model: z.string().nullable().optional(), created_at: z.number().int(), suffix: z.string().nullable().optional(), auto_start: z.boolean(), training_files: z.array(z.string()), repositories: z.array(githubRepositoryOut).optional(), hyperparameters: trainingParameters, model: fineTuneableModel, id: z.string(), trained_tokens: z.number().int().nullable().optional(), modified_at: z.number().int(), integrations: z.array(wandbIntegrationOut).nullable().optional(), status: z.enum(["QUEUED", "STARTED", "VALIDATING", "VALIDATED", "RUNNING", "FAILED_VALIDATION", "FAILED", "SUCCESS", "CANCELLED", "CANCELLATION_REQUESTED"]), validation_files: z.array(z.string()).nullable().optional(), object: z.literal("job").optional()}); export const jobsOut = z.object({total: z.number().int(), data: z.array(jobOut).optional(), object: z.literal("list").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 }; /** * Get Fine Tuning Jobs * * GET /fine_tuning/jobs */ export const getApiFineTuningJobs = async (args: GetApiFineTuningJobsArgs, options?: GetApiFineTuningJobsOptions) => { const {baseUrl = "https://api.mistral.ai/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.model != null) query.append("model", String(args.model)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_by_me != null) query.append("created_by_me", String(args.created_by_me)) if (args.status != null) query.append("status", String(args.status)) if (args.wandb_project != null) query.append("wandb_project", String(args.wandb_project)) if (args.wandb_name != null) query.append("wandb_name", String(args.wandb_name)) if (args.suffix != null) query.append("suffix", String(args.suffix)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/fine_tuning/jobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(jobsOut, await res.json()) };