import {z} from 'zod' export type FunctionParameters = {type?: string | undefined, properties: {[key: string]: Record}, required: Array | null}; export type ToolFunctionDetail = {name: string, description?: (string | null) | undefined, parameters: FunctionParameters}; export type ToolFunctionInput = {type?: 'function' | undefined, function: ToolFunctionDetail}; export type ToolChoiceFunction = {name: string}; export type ToolChoice = {type?: ('function' | 'json_object' | null) | undefined, function?: (ToolChoiceFunction | null) | undefined}; export type RequestResponseFormat = {type: 'text' | 'json_object'}; export type RoleContentChatChoiceContentImageUrl = {url: string}; export type RoleContentChatChoiceContent = {type: 'text' | 'image_url', text?: (string | null) | undefined, image_url?: (RoleContentChatChoiceContentImageUrl | null) | undefined}; export type FunctionCallArguments = {arguments: string, name: string}; export type ToolCall = {function: FunctionCallArguments, id: string, type?: 'function' | undefined}; export type RoleContentChatChoiceInput = {role: 'user' | 'assistant' | 'system', content: string | Array | null, tool_calls?: (Array | null) | undefined}; export type CompletionRequestPayload = {model?: (string | null) | undefined, max_tokens?: (number | null) | undefined, temperature?: (number | null) | undefined, top_p?: (number | null) | undefined, tools?: (Array | null) | undefined, tool_choice?: ('none' | 'auto' | ToolChoice) | undefined, n?: (number | null) | undefined, stream?: (boolean | null) | undefined, logprobs?: (number | null) | undefined, stop?: (string | Array | null) | undefined, presence_penalty?: (number | null) | undefined, frequency_penalty?: (number | null) | undefined, best_of?: (number | null) | undefined, logit_bias?: (Record | null) | undefined, response_format?: (RequestResponseFormat | null) | undefined, messages?: (Array | null) | undefined, assistant_id?: (string | null) | undefined, assistant_version_id?: (string | null) | undefined, conversation_id?: (string | null) | undefined, parent_request_id?: (string | null) | undefined, plugins?: Array | undefined, images?: (Array | null) | undefined, files?: (Array | null) | undefined}; export type CreateApiV1ModelsRankArgs = {body: CompletionRequestPayload}; export type CreateApiV1ModelsRankOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pulzeModelWithScore = z.object({model: z.string(), provider: z.string().nullable().optional(), owner: z.string().nullable().optional(), namespace: z.string(), at: z.string().nullable().optional(), score: z.number().nullable(), quality: z.number().nullable(), latency: z.number().nullable(), cost: z.number().nullable(), is_scored: z.boolean().optional()}); export const promptNeighbors = z.object({id: z.string(), category: z.string(), similarity: z.number()}); export const pulzeEngineModelRanking = z.object({models: z.array(pulzeModelWithScore).optional(), hits: z.array(promptNeighbors).nullable().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 }; /** * Get Rank Of Best Models For Payload * * POST /v1/models/rank */ export const createApiV1ModelsRank = async (args: CreateApiV1ModelsRankArgs, options: CreateApiV1ModelsRankOptions) => { const {baseUrl, 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/models/rank${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(pulzeEngineModelRanking, await res.json()) };