import {z} from 'zod' export type PassthroughColumnMappingDto = {prompt: string, completion?: string | undefined, chosen?: string | undefined, rejected?: string | undefined, context?: Array | undefined}; export type DatasetSourceDto = {url?: string | undefined, files?: Array | undefined, sheet_names?: Array | undefined, sheet_name?: string | undefined, access?: ('oauth' | 'public') | undefined, name?: string | undefined, file_format?: ('csv' | 'json' | 'jsonl' | 'parquet' | 'pdf' | 'docx' | 'pptx' | 'xlsx' | 'html' | 'zip' | 'txt') | undefined, processing_mode?: ('adapt' | 'raw') | undefined, column_mapping?: PassthroughColumnMappingDto | undefined}; export type CreateDatasetDto = {source: DatasetSourceDto}; export type CreateApiApiV1DatasetsArgs = {body: CreateDatasetDto}; export type CreateApiApiV1DatasetsOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const uploadInstructionsDto = z.object({url: z.string(), method: z.string(), s3_key: z.string()}); export const createDatasetResponseDto = z.object({dataset_id: z.string(), status: z.string(), upload_instructions: uploadInstructionsDto.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 dataset from file upload, HuggingFace, Kaggle, Google Sheets, or a public S3 or Cloud Storage object * * POST /api/v1/datasets */ export const createApiApiV1Datasets = async (args: CreateApiApiV1DatasetsArgs, options: CreateApiApiV1DatasetsOptions) => { 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/datasets${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(createDatasetResponseDto, await res.json()) };