import {z} from 'zod' export type PublicRecommendHyperparamsDto = {dataset_id: string, model?: string | undefined, augmentation_domain_rows?: number | undefined, augmentation_general_rows?: number | undefined}; export type CreateApiApiV1AutoscientistRecommendHyperparamsArgs = {body: PublicRecommendHyperparamsDto}; export type CreateApiApiV1AutoscientistRecommendHyperparamsOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const publicAutoScientistHyperparamsDto = z.object({training_type: z.enum(["lora", "full"]).optional(), n_epochs: z.number().gte(1).lte(20).optional(), learning_rate: z.number().gte(1e-8).lte(0.01).optional(), batch_size: z.union([z.literal("max"), z.number().int().gte(1)]).optional(), lora_r: z.number().gte(1).lte(64).optional(), lora_alpha: z.number().optional(), lora_dropout: z.number().gte(0).lte(1).optional(), lora_trainable_modules: z.string().optional(), lr_scheduler_type: z.enum(["linear", "cosine"]).optional(), min_lr_ratio: z.number().gte(0).lte(1).optional(), scheduler_num_cycles: z.number().gt(0).lte(4).optional(), warmup_ratio: z.number().gte(0).lte(1).optional(), max_grad_norm: z.number().gte(0).optional(), weight_decay: z.number().gte(0).optional(), train_on_inputs: z.boolean().optional(), dpo_beta: z.number().gte(0.05).lte(0.9).optional(), dpo_normalize_logratios_by_length: z.boolean().optional(), rpo_alpha: z.number().gte(0).optional(), simpo_gamma: z.number().gte(0).optional()}); export const publicRecommendHyperparamsResponseDto = z.object({model: z.string(), hyperparams: publicAutoScientistHyperparamsDto}); 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 }; /** * Recommend hyperparameters * * POST /api/v1/autoscientist/recommend-hyperparams */ export const createApiApiV1AutoscientistRecommendHyperparams = async (args: CreateApiApiV1AutoscientistRecommendHyperparamsArgs, options: CreateApiApiV1AutoscientistRecommendHyperparamsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/autoscientist/recommend-hyperparams${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(publicRecommendHyperparamsResponseDto, await res.json()) };