import {z} from 'zod' export type Message = {role: 'system' | 'user' | 'assistant', content: string | Array<{type: 'text' | 'image' | 'audio' | 'video', text?: string | undefined, url?: string | undefined, base64?: string | undefined}>}; export type MessagesProperty = Array; export type StreamProperty = boolean; export type JsonProperty = boolean; export type ParamsProperty = {min_length?: number | undefined, max_length?: number | undefined, temperature?: number | undefined}; export type UniversalRequestBodySchema = {text?: string | undefined, messages?: MessagesProperty | undefined, url?: string | undefined, base64?: string | undefined, question?: string | undefined, context?: string | undefined, candidate_labels?: Array | undefined, stream?: StreamProperty | undefined, json?: JsonProperty | undefined, params?: ParamsProperty | undefined}; export type CreateApiModelsV2CohereModelArgs = {model: string, body: UniversalRequestBodySchema}; export type CreateApiModelsV2CohereModelOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, providerKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiModelsV2CohereModelResponse = z.object({error: z.string().optional(), output: z.union([z.string(), z.object({})]).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 }; /** * Cohere * * POST /models/v2/cohere/{model} */ export const createApiModelsV2CohereModel = async (args: CreateApiModelsV2CohereModelArgs, options?: CreateApiModelsV2CohereModelOptions) => { const {baseUrl = "https://api.bytez.com", fetch: fetchFn = fetch, apiKeyAuth, providerKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("Authorization", apiKeyAuth) if (providerKeyAuth !== undefined) headers.set("provider-key", providerKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/models/v2/cohere/${encodeURIComponent(String(args.model))}${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(createApiModelsV2CohereModelResponse, await res.json()) };