import {z} from 'zod' export type CohereEmbeddingRequest = {model: string, input_type: string, texts?: Array | undefined, images?: Array | undefined, inputs?: (Array<{content?: (Array<{type: 'text' | 'image_url' | 'thinking' | 'document', text?: string | undefined, image_url?: ({url?: string | undefined}) | undefined, thinking?: string | undefined, document?: ({data?: Record | undefined, id?: string | undefined}) | undefined}>) | undefined}>) | undefined, embedding_types?: Array | undefined, output_dimension?: number | undefined, max_tokens?: number | undefined, truncate?: string | undefined}; export type CreateApiLitellmCohereV2EmbedArgs = {body: CohereEmbeddingRequest}; export type CreateApiLitellmCohereV2EmbedOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const cohereEmbeddingResponse = z.object({id: z.string().optional(), embeddings: z.object({float: z.array(z.array(z.number())).optional(), int8: z.array(z.array(z.number().int())).optional(), uint8: z.array(z.array(z.number().int())).optional(), binary: z.array(z.array(z.number().int())).optional(), ubinary: z.array(z.array(z.number().int())).optional(), base64: z.array(z.string()).optional()}).optional(), response_type: z.string().optional(), texts: z.array(z.string()).optional(), images: z.array(z.object({width: z.number().int().optional(), height: z.number().int().optional(), format: z.string().optional(), bit_depth: z.number().int().optional()})).optional(), meta: z.object({api_version: z.object({version: z.string().optional(), is_deprecated: z.boolean().optional(), is_experimental: z.boolean().optional()}).optional(), billed_units: z.object({input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional(), search_units: z.number().int().optional(), classifications: z.number().int().optional()}).optional(), tokens: z.object({input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional()}).optional(), warnings: z.array(z.string()).optional()}).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 embeddings (LiteLLM - Cohere format) * * POST /litellm/cohere/v2/embed * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const createApiLitellmCohereV2Embed = async (args: CreateApiLitellmCohereV2EmbedArgs, options?: CreateApiLitellmCohereV2EmbedOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/litellm/cohere/v2/embed${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(cohereEmbeddingResponse, await res.json()) };