import {z} from 'zod' export type JudgeClassificationOutputOption = {value: string, description: string}; export type JudgeClassificationOutput = {type?: 'CLASSIFICATION' | undefined, options: Array}; export type JudgeRegressionOutput = {type?: 'REGRESSION' | undefined, min?: number | undefined, min_description: string, max?: number | undefined, max_description: string}; export type CreateJudgeRequest = {name: string, description: string, model_name: string, output: JudgeClassificationOutput | JudgeRegressionOutput, instructions: string, tools: Array}; export type CreateApiV1ObservabilityJudgesArgs = {body: CreateJudgeRequest}; export type CreateApiV1ObservabilityJudgesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const judgeClassificationOutputOption = z.object({value: z.string(), description: z.string()}); export const judgeClassificationOutput = z.object({type: z.literal("CLASSIFICATION").optional(), options: z.array(judgeClassificationOutputOption)}); export const judgeRegressionOutput = z.object({type: z.literal("REGRESSION").optional(), min: z.number().gte(0).optional(), min_description: z.string(), max: z.number().lt(1000000000).optional(), max_description: z.string()}); export const judge = z.object({id: z.string(), created_at: z.string(), updated_at: z.string(), deleted_at: z.string().nullable(), owner_id: z.string(), workspace_id: z.string(), name: z.string(), description: z.string(), model_name: z.string(), output: z.discriminatedUnion("type", [judgeClassificationOutput, judgeRegressionOutput]), instructions: z.string(), tools: z.array(z.string()), up_revision: z.string().nullable().optional(), down_revision: z.string().nullable().optional(), base_revision: z.string().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 }; /** * Create a new judge * * POST /v1/observability/judges */ export const createApiV1ObservabilityJudges = async (args: CreateApiV1ObservabilityJudgesArgs, options?: CreateApiV1ObservabilityJudgesOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/observability/judges${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(judge, await res.json()) };