Skip to main content
Evaluations help you iterate and improve your applications by testing them against a set of examples after you make changes. Weave provides first-class support for tracking evaluations with Model and Evaluation classes. The APIs are designed with minimal assumptions, allowing flexibility for a wide array of use cases. Evals hero

What you’ll learn:

This guide shows you how to:
  • Set up a Model
  • Create a dataset to test an LLM’s responses against
  • Define a scoring function to compare model output to expected outputs
  • Run an evaluation that tests the model against dataset using the scoring function and an additional built-in scorer
  • View the results of the evaluation in the Weave UI

Prerequisites

  • A W&B account
  • Python 3.8+ or Node.js 18+
  • Required packages installed:
    • Python: pip install weave openai
    • TypeScript: npm install weave openai
  • An OpenAI API key set as an environment variable

Import the necessary libraries and functions

Import the following libraries into your script:

Build a Model

In Weave, Models are objects that capture both the behavior of your model/agent (logic, prompt, parameters) and its versioned metadata (parameters, code, micro-config) so you can track, compare, evaluate and iterate reliably. When you instantiate a Model, Weave automatically captures its configuration and behaviors and updates the version when there are changes. This allows you to track its performance over time as you iterate on it. Models are declared by subclassing Model and implementing a predict function definition, which takes one example and returns the response. The following example model uses OpenAI to extract the names, colors, and flavors of alien fruits from sentences sent to it.
The ExtractFruitsModel class inherits from (or subclasses) weave.Model so that Weave can track the instantiated object. @weave.op decorates the predict function to track its inputs and outputs. You can instantiate Model objects like this:

Create a dataset

Next, you need a dataset to evaluate your model on. A Dataset is a collection of examples stored as a Weave object. The following example dataset defines three example input sentences and their correct answers (labels), and then formats them in a JSON table format that scoring functions can read. This example builds a list of examples in code, but you can also log them one at a time from your running application.
Then create your dataset using the weave.Dataset() class and publish it:

Define custom scoring functions

When using Weave evaluations, Weave expects a target to compare output against. The following scoring function takes two dictionaries (target and output) and returns a dictionary of boolean values indicating whether the output matches the target. The @weave.op() decorator enables Weave to track the scoring function’s execution.
To make your own scoring function, learn more in the Scorers guide. In some applications, you may want to create custom Scorer classes. For example, you might create a standardized LLMJudge class with specific parameters (such as chat model or prompt), specific row scoring, and aggregate score calculation. See the tutorial on defining a Scorer class in the next chapter on Model-Based Evaluation of RAG applications for more information.

Use a built-in scorer and run the evaluation

Along with custom scoring functions, you can also use Weave’s built-in scorers. In the following evaluation, weave.Evaluation() uses the fruit_name_score function defined in the previous section and the built-in MultiTaskBinaryClassificationF1 scorer, which computes F1 scores. The following example runs an evaluation of ExtractFruitsModel on the fruits dataset using the scoring the two functions and logs the results to Weave.
If you’re running from a python script, you’ll need to use asyncio.run. However, if you’re running from a Jupyter notebook, you can use await directly.

Complete Example

View your evaluation results

Weave automatically captures traces of each prediction and score. Click on the link printed by the evaluation to view the results in the Weave UI. Evaluation results

Learn more about Weave evaluations

Next Steps

Build a RAG application to learn about evaluating retrieval-augmented generation.