Skip to main content
This is an interactive notebook. You can run it locally or use the links below:
This tutorial shows you how to build, trace, and evaluate a computer vision pipeline that performs named entity recognition (NER) on images of handwritten patient information. By the end, you’ll have a working optical character recognition (OCR) pipeline backed by a vision-language model (VLM) and a W&B Weave Evaluation that measures how accurately the pipeline extracts structured fields from images. This guide is for developers who want to use Weave to iterate on prompts and systematically measure the quality of multimodal extraction pipelines. The following sections walk through five stages: creating and iterating on prompts, retrieving the dataset, building the NER pipeline, defining scorers, and running an evaluation.

Prerequisites

Before you begin, install and import the required libraries, get your W&B API key, and initialize your Weave project. Completing this step ensures that your environment can authenticate with W&B and log traces to your Weave project.

Create and iterate on prompts with Weave

Good prompt engineering is critical to guiding the model to properly extract entities. In this section, you author an initial prompt, publish it to Weave so you can track changes over time, and then refine it with stricter validation rules. First, create a basic prompt that gives the model the instructions on what to extract from the image data and how to format it. Then, store the prompt in Weave for tracking and iteration.
Next, improve the prompt by adding more instructions and validation rules to help reduce errors in the output. Publishing the revised version under the same name lets Weave track the prompt as a new version so you can compare results across iterations.

Get the dataset

With a prompt in place, you next need input data to run through the pipeline. Retrieve the dataset of handwritten notes that serves as input for the OCR pipeline. The images in the dataset are already base64 encoded, which means that the LLM can use the data without any pre-processing.

Build the NER pipeline

Now that you have a prompt and a dataset, build the NER pipeline that connects them to the VLM. The pipeline consists of two functions:
  • An encode_image function that takes a PIL image from the dataset and returns a base64 encoded string representation of the image that can be passed to the VLM.
  • An extract_named_entities_from_image function that takes an image and system prompt and returns the extracted entities from that image as described by the system prompt.
Now, create a function called named_entity_recognation that:
  • Passes the image data to the NER pipeline.
  • Returns correctly formatted JSON with the results.
Use the @weave.op() decorator to automatically track and trace function execution in the W&B UI. Every time named_entity_recognation runs, the full trace results are visible in the Weave UI. To view the traces, navigate to the Traces tab of your Weave project.
Finally, run the pipeline over the dataset, and view the results. This step produces the model outputs that you evaluate in the next section. The following code loops over the dataset and stores the results in a local file processing_results.json. The results are also viewable in the Weave UI.
You see something similar to the following in the Traces table in the Weave UI.
Weave Traces table showing NER pipeline execution results.

Evaluate the pipeline using Weave

Now that you have created a pipeline to perform NER using a VLM, you can use Weave to systematically evaluate it and find out how well it performs. Evaluating the pipeline lets you measure extraction quality across the full dataset rather than relying on spot checks. For more information about Evaluations in Weave, see Evaluations Overview. A fundamental part of a Weave Evaluation is the Scorer. Scorers evaluate AI outputs and return evaluation metrics. They take the AI’s output, analyze it, and return a dictionary of results. Scorers can use your input data as reference if needed and can also output extra information, such as explanations or reasonings from the evaluation. In this section, you create two scorers to evaluate the pipeline:
  • Programatic scorer.
  • LLM-as-a-judge scorer.

Programatic scorer

The first Scorer is a deterministic check that runs without an LLM. The programmatic scorer, check_for_missing_fields_programatically, takes the model output (the output of the named_entity_recognition function), and identifies which keys are missing or empty in the results. This check is useful for identifying samples where the model missed capturing any fields.

LLM-as-a-judge scorer

Because the programmatic scorer only catches missing or empty fields, you need a second scorer to check whether the extracted values match what appears in the image. In this step of the evaluation, you provide both the image data and the model’s output to ensure the assessment reflects actual NER performance. The image content is explicitly referenced, not just the model output. The scorer used for this step, check_for_missing_fields_with_llm, uses an LLM to perform scoring (specifically OpenAI’s gpt-4o). As specified by the contents of the eval_prompt, check_for_missing_fields_with_llm outputs a Boolean value. If all fields match the information in the image and formatting is correct, the scorer returns true. If any field is missing, empty, incorrect, or mismatched, the result is false, and the scorer also returns a message explaining the problem.

Run the evaluation

With both Scorers defined, you can now run the evaluation. Define an evaluation call that automatically loops over the dataset passed and logs the results together in the Weave UI. The following code kicks off the evaluation and applies the two Scorers to every output from the NER pipeline. Results are visible in the Evals tab in the Weave UI.
When the previous code runs, Weave generates a link to the Evaluation table in the Weave UI. Follow the link to view the results and compare different iterations of the pipeline across models, prompts, and datasets of your choice. The Weave UI automatically creates a visualization like the following for your team.
Weave Evaluation results comparing scorer outputs across the dataset.