import {z} from 'zod' export type ChatCompletionRequestLLMArgs = {[key: string]: unknown}; export type ChatCompletionSelfReflectionConfig = {[key: string]: unknown}; export type ChatCompletionRAGConfig = Record; export type TagFilter = {include?: Array | undefined, exclude?: Array | undefined}; export type ChatCompletionRequest = {message: string, system_prompt?: string | undefined, pre_prompt_query?: string | undefined, prompt_query?: string | undefined, image_batch_final_prompt?: string | undefined, image_batch_image_prompt?: string | undefined, llm?: string | undefined, llm_args?: ChatCompletionRequestLLMArgs | undefined, self_reflection_config?: ChatCompletionSelfReflectionConfig | undefined, rag_config?: ChatCompletionRAGConfig | undefined, include_chat_history?: string | undefined, tags?: (Array | TagFilter) | undefined, stream?: boolean | undefined}; export type CreateApiChatsSessionIdCompletionsArgs = {session_id: string, body: ChatCompletionRequest}; export type CreateApiChatsSessionIdCompletionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const chatCompletion = z.object({message_id: z.string(), body: z.string()}); 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 }; /** * Asks question in a given chat session. If stream is enabled, the server sends stream of delta messages. The stream is terminated with the massage having property finished set to true. This message terminating message contains also the full completion. * * POST /chats/{session_id}/completions */ export const createApiChatsSessionIdCompletions = async (args: CreateApiChatsSessionIdCompletionsArgs, options?: CreateApiChatsSessionIdCompletionsOptions) => { const {baseUrl = "https://h2ogpte.genai.h2o.ai/api/v1", 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(/\/+$/, '')}/chats/${encodeURIComponent(String(args.session_id))}/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(chatCompletion, await res.json()) };