export type RelayChatCompletionsDto = {messages: Array<{role: string, content: unknown} | {[key: string]: unknown}>, model: string, stream?: boolean | undefined} | {[key: string]: unknown}; export type CreateApiApiAiChatCompletionsArgs = {body: RelayChatCompletionsDto}; export type CreateApiApiAiChatCompletionsOptions = {baseUrl?: string | undefined, apikeyHeaderAuthorization?: string | undefined, fetch?: typeof fetch | undefined}; /** * OpenAI 对话 * * POST /api/ai/chat/completions */ export const createApiApiAiChatCompletions = async (args: CreateApiApiAiChatCompletionsArgs, options?: CreateApiApiAiChatCompletionsOptions) => { const {baseUrl = "https://aitoearn.cn", fetch: fetchFn = fetch, apikeyHeaderAuthorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apikeyHeaderAuthorization !== undefined) headers.set("Authorization", apikeyHeaderAuthorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/ai/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 (await res.json()) as unknown };