import {z} from 'zod' export type GuardrailsSettings = {exception_message?: string | undefined, show_violated_responses?: boolean | undefined, guardrails_on_transcriptions?: boolean | undefined, vision_guardrails_enabled?: boolean | undefined, guardrails_vision_llm?: string | undefined, column_redaction_config?: Array> | undefined, disallowed_regex_patterns?: Array | undefined, presidio_labels_to_flag?: Array | undefined, pii_labels_to_flag?: Array | undefined, pii_detection_parse_action?: ('allow' | 'redact' | 'fail') | undefined, pii_detection_llm_input_action?: ('allow' | 'redact' | 'fail') | undefined, pii_detection_llm_output_action?: ('allow' | 'redact' | 'fail') | undefined, prompt_guard_labels_to_flag?: Array | undefined, guardrails_labels_to_flag?: Array | undefined, guardrails_llm?: string | undefined, guardrails_safe_category?: string | undefined, guardrails_entities?: {[key: string]: string} | undefined, column_redaction_custom_entities_to_flag?: Array | undefined, column_redaction_custom_entities?: {[key: string]: string} | undefined, custom_pii_entities_to_flag?: Array | undefined, custom_pii_entities?: {[key: string]: string} | undefined}; export type CollectionSettings = {max_tokens_per_chunk?: number | undefined, chunk_overlap_tokens?: number | undefined, gen_doc_summaries?: boolean | undefined, gen_doc_questions?: boolean | undefined, audio_input_language?: string | undefined, ocr_model?: string | undefined, tesseract_lang?: string | undefined, keep_tables_as_one_chunk?: boolean | undefined, chunk_by_page?: boolean | undefined, handwriting_check?: boolean | undefined, ingest_mode?: ('standard' | 'lite' | 'agent_only') | undefined, follow_links?: boolean | undefined, max_depth?: number | undefined, max_documents?: number | undefined, root_dir?: string | undefined, copy_document?: boolean | undefined, guardrails_settings?: GuardrailsSettings | undefined, expiry_timezone?: string | undefined}; 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 ChatSettings = {llm?: string | undefined, llm_args?: ChatCompletionRequestLLMArgs | undefined, self_reflection_config?: ChatCompletionSelfReflectionConfig | undefined, rag_config?: ChatCompletionRAGConfig | undefined, include_chat_history?: ('on' | 'off' | 'auto') | undefined, tags?: (Array | TagFilter) | undefined}; export type CollectionCreateRequest = {name: string, description: string, embedding_model?: string | undefined, collection_settings?: CollectionSettings | undefined, chat_settings?: ChatSettings | undefined, expiry_date?: string | undefined, workspace?: string | undefined, vex_encryption?: ('enabled' | 'disabled') | undefined}; export type CreateApiCollectionsArgs = {body: CollectionCreateRequest}; export type CreateApiCollectionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.record(z.string(), z.unknown()); export const collection = z.object({id: z.string(), name: z.string(), description: z.string(), description_auto_generated: z.boolean().optional(), description_generated_at: z.string().optional(), description_dirty_at: z.string().optional(), description_generation_started_at: z.string().optional(), embedding_model: z.string(), document_count: z.number().int(), document_size: z.number().int(), created_at: z.string(), updated_at: z.string(), user_count: z.number().int(), is_public: z.boolean(), username: z.string(), sessions_count: z.number().int(), status: z.string(), prompt_template_id: z.string().optional(), thumbnail: z.string().optional(), size_limit: z.number().int().optional(), expiry_date: z.string().optional(), inactivity_interval: z.number().int().optional(), rag_type: z.enum(["auto", "llm_only", "rag", "hyde1", "hyde2", "rag+", "all_data"]).optional(), metadata_dict: metadata.optional(), workspace: z.string().optional(), has_default_guardrails: z.boolean().optional(), vex_encryption: z.enum(["enabled", "disabled", "null"]).nullable().optional(), exempt_from_chat_expiry: 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 }; /** * Create a Collection * * POST /collections */ export const createApiCollections = async (args: CreateApiCollectionsArgs, options?: CreateApiCollectionsOptions) => { 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(/\/+$/, '')}/collections${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(collection, await res.json()) };