import {z} from 'zod' export type ImageURL = {detail?: (string | null) | undefined, url: string}; export type MultiModalImageMessage = {image_url: ImageURL, type?: 'image_url' | undefined}; export type MultiModalTextMessage = {text: string, type?: 'text' | undefined}; export type FunctionCall = {arguments: string, name: string}; export type ToolCall = {function: FunctionCall, id?: string | undefined, type?: 'function' | undefined}; export type ChatMessage = {content: string | Array | null, role: string, tool_calls?: (Array | null) | undefined}; export type ChatCompletionResponseFormat = {schema?: (Record | null) | undefined, type: string}; export type StreamOptions = {include_usage?: boolean | undefined}; export type FunctionHandle = {name: string}; export type FunctionChoice = {function: FunctionHandle, type?: 'function' | undefined}; export type FunctionDefinition = {description?: string | undefined, name: string, parameters?: Record | undefined}; export type ToolDefinition = {function: FunctionDefinition, type?: 'function' | undefined}; export type CreateChatCompletionRequest = {frequency_penalty?: number | undefined, ignore_eos?: boolean | undefined, log_prompt?: boolean | undefined, logit_bias?: ({[key: string]: number} | null) | undefined, loglikelihood?: boolean | undefined, logprobs?: boolean | undefined, max_tokens?: number | undefined, messages: Array, min_p?: number | undefined, model: string, n?: number | undefined, peft?: (string | null) | undefined, presence_penalty?: number | undefined, repetition_penalty?: number | undefined, response_format?: (ChatCompletionResponseFormat | null) | undefined, stop?: (string | Array | null) | undefined, stream?: boolean | undefined, stream_options?: StreamOptions | undefined, temperature?: number | undefined, tool_choice?: ('auto' | 'required' | FunctionChoice | null) | undefined, tools?: (Array | null) | undefined, top_logprobs?: number | undefined, top_p?: number | undefined, user?: (string | null) | undefined}; export type CreateApiV1ChatCompletionsArgs = {body: CreateChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const finishReason = z.enum(["stop", "length", "tool_calls", "content_filter", "function_call", "cancelled"]); export const topLogprobs = z.object({bytes: z.array(z.unknown()).nullable().optional(), logprob: z.number(), token: z.string()}); export const logprobsContent = z.object({bytes: z.array(z.unknown()).nullable().optional(), logprob: z.number(), token: z.string(), top_logprobs: z.array(topLogprobs).optional()}); export const logprobs = z.object({content: z.array(logprobsContent.nullable()).optional()}); export const imageURL = z.object({detail: z.string().nullable().optional(), url: z.string()}); export const multiModalImageMessage = z.object({image_url: imageURL, type: z.literal("image_url").optional()}); export const multiModalTextMessage = z.object({text: z.string(), type: z.literal("text").optional()}); export const functionCall = z.object({arguments: z.string(), name: z.string()}); export const toolCall = z.object({function: functionCall, id: z.string().optional(), type: z.literal("function").optional()}); export const chatMessage = z.object({content: z.union([z.string(), z.array(z.discriminatedUnion("type", [multiModalImageMessage, multiModalTextMessage]))]).nullable(), role: z.string(), tool_calls: z.array(toolCall).nullable().optional()}); export const chatCompletionChoice = z.object({finish_reason: finishReason.nullable().optional(), index: z.number().int(), logprobs: logprobs.nullable().optional(), message: chatMessage}); export const usageStats = z.object({completion_tokens: z.number().int(), prompt_tokens: z.number().int(), total_tokens: z.number().int()}); export const chatCompletionResponse = z.object({choices: z.array(chatCompletionChoice), created: z.number().int(), id: z.string(), model: z.string(), object: z.literal("chat.completion").optional(), usage: usageStats}); 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 Chat Completion * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://text.octoai.run", 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(chatCompletionResponse, await res.json()) };