import {z} from 'zod' export type EvalAnalysisRequest = {name?: (string | null) | undefined, eval_type?: (string | null) | undefined, status?: (string | null) | undefined, language?: (string | null) | undefined, modality?: (string | null) | undefined, created_by?: (string | null) | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, is_jailbreak?: (boolean | null) | undefined, is_sandbox?: (boolean | null) | undefined, workspace_uuid?: (string | null) | undefined, min_pass_rate?: (number | null) | undefined, max_pass_rate?: (number | null) | undefined, has_score_runs?: (boolean | null) | undefined, score_run_status?: (string | null) | undefined, run_created_after?: (string | null) | undefined, run_created_before?: (string | null) | undefined, sort_by?: ('created_at' | 'updated_at' | 'name' | 'pass_rate' | 'num_score_runs' | 'last_run_date') | undefined, sort_order?: ('asc' | 'desc') | undefined, limit?: number | undefined, offset?: number | undefined}; export type CreateApiV2EvalAnalysisArgs = {body: EvalAnalysisRequest}; export type CreateApiV2EvalAnalysisOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const tool = z.object({name: z.string(), content: z.union([z.string(), z.object({})]).nullable()}); export const toolArray = z.object({type: z.literal("array").optional(), value: z.array(tool)}); export const toolDict = z.object({type: z.literal("dict").optional(), value: z.object({})}).and(z.record(z.string(), z.unknown())); export const toolString = z.object({type: z.literal("string").optional(), value: z.string()}); export const agentInstructions = z.object({agent_name: z.string().nullable().optional(), system_prompt: z.string(), tools: z.discriminatedUnion("type", [toolArray, toolDict, toolString]).optional()}); export const workflowInstructions = z.object({instructions: z.array(agentInstructions)}); export const contentType = z.enum(["text", "image", "video"]); export const fileReference = z.object({file_uuid: z.string().nullable().optional(), remote_file_path: z.string().nullable().optional()}); export const exampleType = z.enum(["good", "bad"]); export const promptExample = z.object({content: z.string(), example_uuid: z.string().nullable().optional(), type: exampleType.optional(), explanation: z.string().nullable().optional()}); export const status = z.enum(["created", "processing", "finished", "failed"]); export const eval = z.object({eval_uuid: z.string().nullable().optional(), name: z.string().nullable().optional(), created_by: z.string().nullable().optional(), ai_description: z.string(), ai_instructions: z.union([z.string(), agentInstructions, workflowInstructions]).nullable().optional(), eval_type: z.string(), eval_instructions: z.string().nullable().optional(), language: z.string().nullable().optional(), modality: contentType.optional(), ground_truth: z.union([z.string(), fileReference]).nullable().optional(), num_prompts: z.number().int().nullable().optional(), prompt_examples: z.array(promptExample).nullable().optional(), is_jailbreak: z.boolean().optional(), is_sandbox: z.boolean().optional(), workspace_uuid: z.string().nullable().optional(), status: status.nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const evalStats = z.object({total_score_runs: z.number().int(), avg_pass_rate: z.number().nullable().optional(), best_pass_rate: z.number().nullable().optional(), worst_pass_rate: z.number().nullable().optional(), last_run_date: z.string().nullable().optional(), total_responses_scored: z.number().int()}); export const evalAnalysisResult = z.object({eval: eval, stats: evalStats}); export const evalAnalysisResponse = z.object({results: z.array(evalAnalysisResult), total_count: z.number().int(), has_more: z.boolean()}); 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 }; /** * Analyze Evals * * POST /v2/eval-analysis * * Also accepts auth: AuthBearer */ export const createApiV2EvalAnalysis = async (args: CreateApiV2EvalAnalysisArgs, options?: CreateApiV2EvalAnalysisOptions) => { const {baseUrl = "https://api.aymara.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("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/eval-analysis${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(evalAnalysisResponse, await res.json()) };