import {z} from 'zod' export type GetApiInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const classifierModel = z.object({id2label: z.record(z.string(), z.string()), label2id: z.record(z.string(), z.number().int().gte(0))}); export const embeddingModel = z.object({pooling: z.string()}); export const modelType = z.union([z.object({classifier: classifierModel}), z.object({embedding: embeddingModel}), z.object({reranker: classifierModel})]); export const info = z.object({auto_truncate: z.boolean(), docker_label: z.string().nullable().optional(), max_batch_requests: z.number().int().gte(0).nullable().optional(), max_batch_tokens: z.number().int().gte(0), max_client_batch_size: z.number().int().gte(0), max_concurrent_requests: z.number().int().gte(0), max_input_length: z.number().int().gte(0), model_dtype: z.string(), model_id: z.string(), model_sha: z.string().nullable().optional(), model_type: modelType, sha: z.string().nullable().optional(), tokenization_workers: z.number().int().gte(0), version: z.string()}); 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 }; /** * Text Embeddings Inference endpoint info * * GET /info */ export const getApiInfo = async (options: GetApiInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/info${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(info, await res.json()) };