Arize Phoenix TS
    Preparing search index...

    Module @arizeai/phoenix-evals

    Arize Phoenix logo
    @arizeai/phoenix-evals

    NPM Version Documentation

    This package provides a TypeScript evaluation library. It is vendor agnostic and can be used in isolation of any framework or platform. This package is still under active development and is subject to change.

    # or yarn, pnpm, bun, etc...
    npm install @arizeai/phoenix-evals

    The library provides a createClassifier function that allows you to create custom evaluators for different tasks like hallucination detection, relevance scoring, or any binary/multi-class classification.

    import { createClassifier } from "@arizeai/phoenix-evals/llm";
    import { openai } from "@ai-sdk/openai";

    const model = openai("gpt-4o-mini");

    const promptTemplate = `
    In this task, you will be presented with a query, a reference text and an answer. The answer is
    generated to the question based on the reference text. The answer may contain false information. You
    must use the reference text to determine if the answer to the question contains false information,
    if the answer is a hallucination of facts. Your objective is to determine whether the answer text
    contains factual information and is not a hallucination. A 'hallucination' refers to
    an answer that is not based on the reference text or assumes information that is not available in
    the reference text. Your response should be a single word: either "factual" or "hallucinated", and
    it should not include any other text or characters.

    [BEGIN DATA]
    ************
    [Query]: {{input}}
    ************
    [Reference text]: {{reference}}
    ************
    [Answer]: {{output}}
    ************
    [END DATA]

    Is the answer above factual or hallucinated based on the query and reference text?
    `;

    // Create the classifier
    const evaluator = await createClassifier({
    model,
    choices: { factual: 1, hallucinated: 0 },
    promptTemplate: promptTemplate,
    });

    // Use the classifier
    const result = await evaluator({
    output: "Arize is not open source.",
    input: "Is Arize Phoenix Open Source?",
    reference:
    "Arize Phoenix is a platform for building and deploying AI applications. It is open source.",
    });

    console.log(result);
    // Output: { label: "hallucinated", score: 0 }

    See the complete example in examples/classifier_example.ts.

    The library includes several pre-built evaluators for common evaluation tasks. These evaluators come with optimized prompts and can be used directly with any AI SDK model.

    All pre-built evaluators are available from the @arizeai/phoenix-evals/llm module:

    Evaluator Function Description
    Faithfulness createFaithfulnessEvaluator Detects hallucinations — checks if the output is grounded in the provided context
    Conciseness createConcisenessEvaluator Evaluates whether the response is appropriately concise
    Correctness createCorrectnessEvaluator Checks if the output is factually correct given the input
    Document Relevance createDocumentRelevanceEvaluator Measures how relevant a retrieved document is to the query
    Refusal createRefusalEvaluator Detects whether the model refused to answer
    Tool Invocation createToolInvocationEvaluator Evaluates whether the correct tool was invoked with the right arguments
    Tool Selection createToolSelectionEvaluator Checks whether the right tool was selected for the task
    Tool Response Handling createToolResponseHandlingEvaluator Evaluates how well the model uses a tool's response
    import {
    createFaithfulnessEvaluator,
    createConcisenessEvaluator,
    createCorrectnessEvaluator,
    createDocumentRelevanceEvaluator,
    createRefusalEvaluator,
    } from "@arizeai/phoenix-evals/llm";
    import { openai } from "@ai-sdk/openai";

    const model = openai("gpt-4o-mini");

    // Faithfulness: checks if the output is grounded in the context
    const faithfulnessEvaluator = createFaithfulnessEvaluator({ model });
    const faithfulnessResult = await faithfulnessEvaluator.evaluate({
    input: "What is the capital of France?",
    context: "France is a country in Europe. Paris is its capital city.",
    output: "The capital of France is London.",
    });
    console.log(faithfulnessResult);
    // Output: { label: "unfaithful", score: 0, explanation: "..." }

    // Correctness: checks if the output is factually correct
    const correctnessEvaluator = createCorrectnessEvaluator({ model });
    const correctnessResult = await correctnessEvaluator.evaluate({
    input: "What is the capital of France?",
    output: "Paris is the capital of France.",
    });
    console.log(correctnessResult);
    // Output: { label: "correct", score: 1, explanation: "..." }

    // Document Relevance: checks if a retrieved document is relevant to the query
    const relevanceEvaluator = createDocumentRelevanceEvaluator({ model });
    const relevanceResult = await relevanceEvaluator.evaluate({
    input: "What is the capital of France?",
    documentText: "Paris is the capital of France and a major European city.",
    });
    console.log(relevanceResult);
    // Output: { label: "relevant", score: 1, explanation: "..." }

    The library also includes built-in, deterministic (non-LLM) code evaluators for common classification metrics: precision, recall, and F-beta (including F1). These are available from the @arizeai/phoenix-evals/code module and work over a batch of expected vs. predicted labels, supporting both binary classification (via positiveLabel) and multi-class classification (via macro/micro/weighted averaging).

    • Precision — of everything predicted as a class, the fraction that was actually that class (TP / (TP + FP)). Lower precision means more false alarms.
    • Recall — of everything that actually belongs to a class, the fraction the model found (TP / (TP + FN)). Lower recall means more misses.
    • F-beta — the weighted harmonic mean of precision and recall. beta = 1 (F1, the default) weights them equally; beta > 1 weights recall more (use when missing a true positive is costlier, e.g. medical screening); beta < 1 weights precision more (use when a false alarm is costlier, e.g. spam filtering).
    import {
    createPrecisionEvaluator,
    createRecallEvaluator,
    createF1Evaluator,
    createPrecisionRecallFScoreEvaluators,
    } from "@arizeai/phoenix-evals/code";

    const f1 = createF1Evaluator();
    const result = await f1.evaluate({
    expected: ["cat", "dog", "cat", "bird"],
    output: ["cat", "cat", "cat", "bird"],
    });
    console.log(result);
    // Output: { score: 0.6 }

    // Or create matching precision/recall/F-score evaluators at once
    const { precision, recall, fScore } = createPrecisionRecallFScoreEvaluators({
    average: "weighted",
    });

    Use createFBetaEvaluator({ beta }) for F-scores other than F1 (e.g. beta: 2 weights recall higher than precision). createPrecisionRecallFScoreEvaluators returns three separate evaluators ({ precision, recall, fScore }) sharing one options object — each yields a single score, matching the individual factories.

    For multi-class data, average controls how per-class scores combine into one number: "macro" (default) weights every class equally — good for surfacing whether a rare class is being ignored; "weighted" weights each class by how often it occurs — good when overall performance matters more than parity across classes; "micro" pools every true/false positive and false negative across classes first — for single-label multi-class problems this equals overall accuracy.

    See examples/classification_metrics_example.ts for a runnable walkthrough covering binary classification, F-beta tradeoffs, and all three averaging strategies. For the underlying formulas, the precision/recall tradeoff, and citations, see the Precision / Recall / F-Score docs.

    Note: unlike the per-row LLM evaluators above, these classification-metric evaluators are batch/dataset-level: expected/output are the full sequence of labels across every example, not a single row's labels. Don't wire them directly into runExperiment as a per-row evaluator — instead, collect every row's expected/predicted label first, then call .evaluate({ expected, output }) once over the full arrays.

    When your data structure doesn't match what an evaluator expects, use bindEvaluator to map your fields to the evaluator's expected input format:

    import { bindEvaluator } from "@arizeai/phoenix-evals";
    import { createFaithfulnessEvaluator } from "@arizeai/phoenix-evals/llm";
    import { openai } from "@ai-sdk/openai";

    const model = openai("gpt-4o-mini");

    type ExampleType = {
    question: string;
    context: string;
    answer: string;
    };

    const evaluator = bindEvaluator<ExampleType>(
    createFaithfulnessEvaluator({ model }),
    {
    inputMapping: {
    input: "question", // Map "input" from "question"
    context: "context", // Map "context" from "context"
    output: "answer", // Map "output" from "answer"
    },
    }
    );

    const result = await evaluator.evaluate({
    question: "Is Arize Phoenix Open Source?",
    context:
    "Arize Phoenix is a platform for building and deploying AI applications. It is open source.",
    answer: "Arize is not open source.",
    });

    Mapping supports simple properties ("fieldName"), dot notation ("user.profile.name"), array access ("items[0].id"), JSONPath expressions ("$.items[*].id"), and function extractors ((data) => data.customField).

    See the complete example in examples/bind_evaluator_example.ts.

    This package works seamlessly with @arizeai/phoenix-client to enable experimentation workflows. You can create datasets, run experiments, and trace evaluation calls for analysis and debugging.

    To run experiments with your evaluations, install the phoenix-client

    npm install @arizeai/phoenix-client
    
    import { createFaithfulnessEvaluator } from "@arizeai/phoenix-evals/llm";
    import { openai } from "@ai-sdk/openai";
    import { createDataset } from "@arizeai/phoenix-client/datasets";
    import {
    asExperimentEvaluator,
    runExperiment,
    } from "@arizeai/phoenix-client/experiments";

    // Create your evaluator
    const faithfulnessEvaluator = createFaithfulnessEvaluator({
    model: openai("gpt-4o-mini"),
    });

    // Create a dataset for your experiment
    const dataset = await createDataset({
    name: "faithfulness-eval",
    description: "Evaluate the faithfulness of the model",
    examples: [
    {
    input: {
    question: "Is Phoenix Open-Source?",
    context: "Phoenix is Open-Source.",
    },
    },
    // ... more examples
    ],
    });

    // Define your experimental task
    const task = async (example) => {
    // Your AI system's response to the question
    return "Phoenix is not Open-Source";
    };

    // Create a custom evaluator to validate results
    const faithfulnessCheck = asExperimentEvaluator({
    name: "faithfulness",
    kind: "LLM",
    evaluate: async ({ input, output }) => {
    // Use the faithfulness evaluator from phoenix-evals
    const result = await faithfulnessEvaluator({
    input: input.question,
    context: input.context,
    output: output,
    });

    return result; // Return the evaluation result
    },
    });

    // Run the experiment with automatic tracing
    runExperiment({
    experimentName: "faithfulness-eval",
    experimentDescription: "Evaluate the faithfulness of the model",
    dataset: dataset,
    task,
    evaluators: [faithfulnessCheck],
    });

    To run examples, install dependencies using pnpm and run:

    pnpm install
    pnpx tsx examples/classifier_example.ts
    # change the file name to run other examples

    Join our community to connect with thousands of AI builders:

    Modules

    __generated__/default_templates
    __generated__/default_templates/CONCISENESS_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/CORRECTNESS_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/DOCUMENT_RELEVANCE_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/REFUSAL_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/TOOL_RESPONSE_HANDLING_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/default_templates/TOOL_SELECTION_CLASSIFICATION_EVALUATOR_CONFIG
    __generated__/types
    code
    code/classificationMetrics
    code/createClassificationMetricEvaluator
    code/createF1Evaluator
    code/createFBetaEvaluator
    code/createPrecisionEvaluator
    code/createPrecisionRecallFScoreEvaluators
    code/createRecallEvaluator
    core/EvaluatorBase
    core/FunctionEvaluator
    helpers
    helpers/asEvaluatorFn
    helpers/createEvaluator
    helpers/toEvaluationResult
    index
    llm
    llm/ClassificationEvaluator
    llm/createClassificationEvaluator
    llm/createClassifierFn
    llm/createConcisenessEvaluator
    llm/createCorrectnessEvaluator
    llm/createDocumentRelevanceEvaluator
    llm/createFaithfulnessEvaluator
    llm/createHallucinationEvaluator
    llm/createRefusalEvaluator
    llm/createToolInvocationEvaluator
    llm/createToolResponseHandlingEvaluator
    llm/createToolSelectionEvaluator
    llm/generateClassification
    llm/LLMEvaluator
    telemetry
    template
    template/applyTemplate
    template/createTemplateVariablesProxy
    template/getTemplateVariables
    types
    types/base
    types/data
    types/evals
    types/otel
    types/prompts
    types/templating
    utils
    utils/bindEvaluator
    utils/objectMappingUtils
    utils/typeUtils