Model and Evaluation classes. The APIs make minimal assumptions, so they fit a range of use cases.

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 the 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.10+ or Node.js 18+
- Required packages installed:
- Python:
pip install weave openai - TypeScript:
npm install weave openai
- Python:
- 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
With the libraries in place, the next step is to define the model you want to evaluate.
In Weave, Models are objects that capture both the behavior of your model or 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 changes occur. This lets you track its performance over time as you iterate on it.
To declare a Model, subclass Model and implement a predict function definition that takes one example and returns the response.
The following example model uses OpenAI to extract the names, colors, and flavors of alien fruits from input sentences.
ExtractFruitsModel class inherits from (or subclasses) weave.Model so 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
With aModel defined, you now need a dataset to evaluate it against. A Dataset is a collection of examples stored as a Weave object. Publishing the dataset to Weave versions it and makes it reusable across evaluation runs.
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.
weave.Dataset() class and publish it:
Define custom scoring functions
Now that you have a model and a dataset, you need a way to measure how well the model performs on each example. Scoring functions compare the model’s output to the expected target and produce the metrics that an evaluation reports. When you use Weave evaluations, Weave expects atarget to compare output against. The following scoring function takes two dictionaries (target and output) and returns a dictionary of boolean values that indicate whether the output matches the target. The @weave.op() decorator enables Weave to track the scoring function’s execution.
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. For more information, see the tutorial on defining a Scorer class in Model-based evaluation of RAG applications.
Use a built-in scorer and run the evaluation
With the model, dataset, and a custom scorer in place, you’re ready to wire everything together into an evaluation run. 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 two scoring 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
Complete evaluation pipeline in one script:
Complete evaluation pipeline in one script:
View your evaluation results
After the evaluation completes, you can inspect each prediction and scorer result in the Weave UI. Weave automatically captures traces of each prediction and score. To view the results, click the link that the evaluation prints.
Learn more about Weave evaluations
You now have a complete evaluation pipeline. To go deeper into Weave’s evaluation features, see the following resources:- Learn more about how to build and use scorers.
- Check out Weave’s built-in scoring functions.
- Learn about Model-Based Evaluation for using LLMs as judges.