import {z} from 'zod' export type EvaluatorPropertyType = 'string' | 'int' | 'float' | 'boolean' | 'enum'; export type EvaluatorProperty = {description?: string | undefined, enum_values?: Array | undefined, label?: string | undefined, name: string, type: EvaluatorPropertyType}; export type ModelLLMMessage = {content?: string | undefined, role?: string | undefined, tool_call_id?: string | undefined}; export type RequestCreateCustomEvaluatorInput = {description?: string | undefined, frequency_penalty?: number | undefined, input_schema: Array, logprobs?: boolean | undefined, max_tokens?: number | undefined, messages: Array, mode?: string | undefined, model: string, name: string, output_schema: Array, presence_penalty?: number | undefined, provider: string, slug?: string | undefined, stop?: Array | undefined, temperature?: number | undefined, top_p?: number | undefined}; export type CreateApiV2EvaluatorsArgs = {body: RequestCreateCustomEvaluatorInput}; export type CreateApiV2EvaluatorsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const typesEvaluatorType = z.enum(["AGENT_GOAL_ACCURACY", "CHAR_COUNT", "CHAR_COUNT_RATIO", "ANSWER_COMPLETENESS", "FAITHFULNESS", "JSON_VALIDATOR", "PERPLEXITY", "PII_DETECTOR", "PLACEHOLDER_REGEX", "PROFANITY_DETECTOR", "SEXISM_DETECTOR", "PROMPT_INJECTION", "TONE_DETECTION", "PROMPT_PERPLEXITY", "TOXICITY_DETECTOR", "ANSWER_RELEVANCY", "ANSWER_CORRECTNESS", "REGEX_VALIDATOR", "SECRETS_DETECTOR", "SQL_VALIDATOR", "SEMANTIC_SIMILARITY", "TOPIC_ADHERENCE", "UNCERTAINTY_DETECTOR", "WORD_COUNT", "WORD_COUNT_RATIO", "INSTRUCTION_ADHERENCE", "AGENT_FLOW_QUALITY", "AGENT_EFFICIENCY", "AGENT_GOAL_COMPLETENESS", "CONVERSATION_QUALITY", "INTENT_CHANGE", "CONTEXT_RELEVANCE", "AGENT_TOOL_ERROR_DETECTOR", "AGENT_TOOL_TRAJECTORY", "HTML_COMPARISON", "LLM_AS_A_JUDGE"]); export const responseCreateEvaluatorResponse = z.object({bindings: z.array(z.string()).optional(), evaluator_id: z.string().optional(), slug: z.string().optional(), type: typesEvaluatorType.optional(), version_id: z.string().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 custom LLM-as-a-judge evaluator * * POST /v2/evaluators */ export const createApiV2Evaluators = async (args: CreateApiV2EvaluatorsArgs, options?: CreateApiV2EvaluatorsOptions) => { const {baseUrl = "https://api.traceloop.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/evaluators${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(responseCreateEvaluatorResponse, await res.json()) };