import {z} from 'zod' export type ChatCompletionContentPartTextParam = {text: string, type: 'text'}; export type ChatCompletionDeveloperMessageParam = {content: string | Array, role: 'developer', name?: string | undefined}; export type ChatCompletionSystemMessageParam = {content: string | Array, role: 'system', name?: string | undefined}; export type ImageURL = {url: string, detail?: ('auto' | 'low' | 'high') | undefined}; export type ChatCompletionContentPartImageParam = {image_url: ImageURL, type: 'image_url'}; export type InputAudio = {data: string, format: 'wav' | 'mp3'}; export type ChatCompletionContentPartInputAudioParam = {input_audio: InputAudio, type: 'input_audio'}; export type FileFile = {file_data?: string | undefined, file_id?: string | undefined, filename?: string | undefined}; export type File = {file: FileFile, type: 'file'}; export type ChatCompletionUserMessageParam = {content: string | Array, role: 'user', name?: string | undefined}; export type Audio = {id: string}; export type ChatCompletionContentPartRefusalParam = {refusal: string, type: 'refusal'}; export type FunctionCall = {arguments: string, name: string}; export type Function = {arguments: string, name: string}; export type ChatCompletionMessageFunctionToolCallParam = {id: string, function: Function, type: 'function'}; export type Custom = {input: string, name: string}; export type ChatCompletionMessageCustomToolCallParam = {id: string, custom: Custom, type: 'custom'}; export type ChatCompletionAssistantMessageParam = {role: 'assistant', audio?: (Audio | null) | undefined, content?: (string | Array | null) | undefined, function_call?: (FunctionCall | null) | undefined, name?: string | undefined, refusal?: (string | null) | undefined, tool_calls?: (Array) | undefined}; export type ChatCompletionToolMessageParam = {content: string | Array, role: 'tool', tool_call_id: string}; export type ChatCompletionFunctionMessageParam = {content: string | null, name: string, role: 'function'}; export type FunctionDefinition = {name: string, description?: string | undefined, parameters?: {[key: string]: unknown} | undefined, strict?: (boolean | null) | undefined}; export type ChatCompletionFunctionToolParam = {function: FunctionDefinition, type: 'function'}; export type ChatCompletionRequest = {model: string, messages: Array, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, max_tokens?: (number | null) | undefined, stream?: (boolean | null) | undefined, stop?: (string | Array | null) | undefined, logprobs?: (boolean | null) | undefined, top_logprobs?: (number | null) | undefined, presence_penalty?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, user?: (string | null) | undefined, n_ctx?: (number | null) | undefined, n_batch?: (number | null) | undefined, n_gpu_layers?: (number | null) | undefined, n_threads?: (number | null) | undefined, flash_attn?: (boolean | null) | undefined, use_mmap?: (boolean | null) | undefined, use_mlock?: (boolean | null) | undefined, cache_type_k?: (string | null) | undefined, cache_type_v?: (string | null) | undefined, extra_body?: ({[key: string]: unknown} | null) | undefined, tools?: (Array | null) | undefined, tool_choice?: (string | {[key: string]: unknown} | null) | undefined, think?: (boolean | null) | undefined, thinking_budget?: (number | null) | undefined, cache_key?: (string | null) | undefined, return_cache_key?: (boolean | null) | undefined, auto_truncate?: (boolean | null) | undefined, truncation_strategy?: (string | null) | undefined}; export type CreateApiV1ChatCompletionsArgs = {body: ChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiV1ChatCompletionsResponse = z.unknown(); 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 }; /** * Chat Completions * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options: CreateApiV1ChatCompletionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/completions${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(createApiV1ChatCompletionsResponse, await res.json()) };