export type CreateApiV1ChatCompletionsArgs = {body: {model?: string | undefined, messages: Array<{role: 'system' | 'user' | 'assistant' | 'tool', content: string | Array, name?: string | undefined, tool_call_id?: string | undefined}>, stream?: boolean | undefined, user?: string | undefined}}; export type CreateApiV1ChatCompletionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * 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', "text/event-stream") 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 res.text() };