import {z} from 'zod' export type TextContentPart = {type: 'text', text: string}; export type ImageURL = {url: string}; export type ImageContentPart = {type: 'image_url', image_url: ImageURL}; export type ChatMessage = {role: 'system' | 'developer' | 'user' | 'assistant', content?: (string | Array | null) | undefined}; export type StreamOptions = {include_usage?: boolean | undefined}; export type ResponseFormatText = {type: 'text'}; export type ResponseFormatJSONObject = {type: 'json_object'}; export type JSONSchemaSpec = {name: string, schema?: ({[key: string]: unknown} | null) | undefined}; export type ResponseFormatJSONSchema = {type: 'json_schema', json_schema: JSONSchemaSpec}; export type ChatCompletionRequest = {messages: Array, model: 'jina-ocr-v1', stream?: boolean | undefined, stream_options?: (StreamOptions | null) | undefined, max_completion_tokens?: (number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, presence_penalty?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, stop?: (string | Array | null) | undefined, seed?: (number | null) | undefined, logprobs?: (boolean | null) | undefined, top_logprobs?: (number | null) | undefined, logit_bias?: ({[key: string]: number} | null) | undefined, response_format?: (ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | null) | undefined}; export type CreateApiV1ChatCompletionsArgs = {body: ChatCompletionRequest}; export type CreateApiV1ChatCompletionsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ChatCompletionsResponse = z.unknown(); 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 }; /** * Chat Completions * * POST /v1/chat/completions */ export const createApiV1ChatCompletions = async (args: CreateApiV1ChatCompletionsArgs, options?: CreateApiV1ChatCompletionsOptions) => { const {baseUrl = "https://api.jina.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) 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 validateResponse(createApiV1ChatCompletionsResponse, await res.json()) };