import {z} from 'zod' export type PromptTemplateCreateRequest = {name: string, description?: string | undefined, lang?: string | undefined, system_prompt?: string | undefined, pre_prompt_query?: string | undefined, prompt_query?: string | undefined, hyde_no_rag_llm_prompt_extension?: string | undefined, pre_prompt_summary?: string | undefined, prompt_summary?: string | undefined, system_prompt_reflection?: string | undefined, prompt_reflection?: string | undefined, auto_gen_description_prompt?: string | undefined, auto_gen_document_summary_pre_prompt_summary?: string | undefined, auto_gen_document_summary_prompt_summary?: string | undefined, auto_gen_document_sample_questions_prompt?: string | undefined, default_sample_questions?: Array | undefined, image_batch_image_prompt?: string | undefined, image_batch_final_prompt?: string | undefined}; export type CreateApiPromptTemplatesArgs = {body: PromptTemplateCreateRequest}; export type CreateApiPromptTemplatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const promptTemplate = z.object({name: z.string().optional(), description: z.string().optional(), lang: z.string().optional(), system_prompt: z.string().optional(), pre_prompt_query: z.string().optional(), prompt_query: z.string().optional(), hyde_no_rag_llm_prompt_extension: z.string().optional(), pre_prompt_summary: z.string().optional(), prompt_summary: z.string().optional(), system_prompt_reflection: z.string().optional(), prompt_reflection: z.string().optional(), auto_gen_description_prompt: z.string().optional(), auto_gen_document_summary_pre_prompt_summary: z.string().optional(), auto_gen_document_summary_prompt_summary: z.string().optional(), auto_gen_document_sample_questions_prompt: z.string().optional(), default_sample_questions: z.array(z.string()).optional(), image_batch_image_prompt: z.string().optional(), image_batch_final_prompt: z.string().optional(), id: z.string(), is_default: z.boolean(), user_count: z.number().int().optional(), group_count: z.number().int().optional(), username: z.string().optional(), user_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), is_public: z.boolean(), visible: z.boolean().optional(), is_system_default: z.boolean().optional()}); 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 }; /** * Creates a new prompt template. * * POST /prompt_templates */ export const createApiPromptTemplates = async (args: CreateApiPromptTemplatesArgs, options?: CreateApiPromptTemplatesOptions) => { 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(/\/+$/, '')}/prompt_templates${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(promptTemplate, await res.json()) };