import {z} from 'zod' export type ChatCompletionSystemMessageParam = {content: string, role: 'system', name?: string | undefined}; export type ChatCompletionContentPartTextParam = {text: string, type: 'text'}; export type ImageURL = {url: string, detail?: ('auto' | 'low' | 'high') | undefined}; export type ChatCompletionContentPartImageParam = {image_url: ImageURL, type: 'image_url'}; export type ChatCompletionUserMessageParam = {content: string | Array, role: 'user', name?: string | undefined}; export type FunctionCall = {arguments: string, name: string}; export type Function = {arguments: string, name: string}; export type ChatCompletionMessageToolCallParam = {id: string, function: Function, type: 'function'}; export type ChatCompletionAssistantMessageParam = {role: 'assistant', content?: (string | null) | undefined, function_call?: FunctionCall | undefined, name?: string | undefined, tool_calls?: Array | undefined}; export type ChatCompletionFunctionMessageParam = {content: string | Array, name: string, role: 'function'}; export type ChatCompletionToolMessageParam = {content: string | Array, role: 'tool', tool_call_id: string}; export type Tool = {type: 'function', function: {name: string, description?: string | undefined, parameters?: Record | undefined}}; export type CreateApiV1ChatCompletionsArgs = {body: {model: string, messages?: (Array) | undefined, timeout?: (number | number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, n?: (number | null) | undefined, stream?: (boolean | null) | undefined, stop?: (Record | null) | undefined, max_tokens?: (number | null) | undefined, presence_penalty?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, logit_bias?: (Record | null) | undefined, user?: (string | null) | undefined, seed?: (number | null) | undefined, tools?: Array | undefined, tool_choice?: ('auto' | 'none' | 'required') | undefined, logprobs?: (boolean | null) | undefined, top_logprobs?: (number | null) | undefined, functions?: (Array | null) | undefined, function_call?: (string | null) | undefined}}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const chatCompletionResponse = z.object({}); 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 chat completion * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://api.arcee.ai", 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(/\/+$/, '')}/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(chatCompletionResponse, await res.json()) };