Skip to main content
This is an interactive notebook. You can run it locally or use the links below:
Summarizing complex technical documents while preserving crucial details is a challenging task. The Chain of Density (CoD) summarization technique offers a solution by iteratively refining summaries to be more concise and information-dense. This guide demonstrates how to implement CoD using Weave for tracking and evaluating the application.
Weave evaluation dashboard with Chain of Density summarization results, metrics, and performance comparisons

What is chain of density summarization

arXiv Chain of Density (CoD) is an iterative summarization technique that produces increasingly concise and information-dense summaries. It works by:
  1. Starting with an initial summary
  2. Iteratively refining the summary, making it more concise while preserving key information
  3. Increasing the density of entities and technical details with each iteration
This approach is particularly useful for summarizing scientific papers or technical documents where preserving detailed information is crucial.

Why use Weave

In this tutorial, you’ll use Weave to implement and evaluate a Chain of Density summarization pipeline for ArXiv papers. You’ll learn how to:
  • Track your LLM pipeline: Use Weave to automatically log inputs, outputs, and intermediate steps of your summarization process.
  • Evaluate LLM outputs: Create consistent evaluations of your summaries using Weave’s built-in tools.
  • Build composable operations: Combine and reuse Weave operations across different parts of your summarization pipeline.
  • Integrate with existing code: Add Weave to your existing Python code with minimal overhead.
By the end of this tutorial, you’ll have created a CoD summarization pipeline that uses Weave’s capabilities for model serving, evaluation, and result tracking.

Set up the environment

First, set up the environment and import the necessary libraries. This step installs the dependencies needed for the pipeline, including Weave for tracking, Anthropic for the LLM, and PyPDF2 for reading ArXiv PDFs.
Because the pipeline calls Anthropic’s Claude model, you’ll need an Anthropic API key before running the following code.
To get an Anthropic API key:
  1. Sign up for an account at https://www.anthropic.com.
  2. Navigate to the API section in your account settings.
  3. Generate a new API key.
  4. Store the API key securely in your .env file.
This code uses Weave to track the experiment and Anthropic’s Claude model for text generation. The weave.init([PROJECT_NAME]) call sets up a new Weave project for the summarization task.

Define the ArxivPaper model

With the environment ready, the next step is to define the data structure the pipeline operates on. Create an ArxivPaper class to represent the data:
This class encapsulates the metadata and content of an ArXiv paper, which is the input to the summarization pipeline.

Load PDF content

The ArxivPaper model holds metadata and a PDF URL, but the summarization pipeline needs the full text of the paper. To work with the full paper content, add a function to load and extract text from PDFs:

Implement Chain of Density summarization

Now, implement the core CoD summarization logic using Weave operations:
Weave trace visualization with Chain of Density summarization pipeline execution
Here’s what each function does:
  • summarize_current_summary: Generates a single summary iteration based on the current state.
  • iterative_density_summarization: Applies the CoD technique by calling summarize_current_summary multiple times.
  • chain_of_density_summarization: Orchestrates the entire summarization process and returns the results.
The @weave.op() decorators ensure that Weave tracks the inputs, outputs, and execution of these functions.

Create a Weave Model

With the summarization functions in place, the next step is to package them as a Weave Model so that runs, parameters, and versions are tracked together. Now, wrap the summarization pipeline in a Weave Model:
Weave Model configuration interface for Chain of Density summarization with model settings and parameters
This ArxivChainOfDensityPipeline class encapsulates the summarization logic as a Weave Model, providing several key benefits:
  • Automatic experiment tracking: Weave captures inputs, outputs, and parameters for each run of the model.
  • Versioning: Changes to the model’s attributes or code are automatically versioned, creating a clear history of how your summarization pipeline evolves over time.
  • Reproducibility: The versioning and tracking let you reproduce any previous result or configuration of your summarization pipeline.
  • Hyperparameter management: Model attributes (like model and density_iterations) are clearly defined and tracked across different runs, facilitating experimentation.
  • Integration with Weave ecosystem: Using weave.Model works with other Weave tools, such as evaluations and serving capabilities.

Implement evaluation metrics

With the pipeline producing summaries, you need a way to measure their quality systematically. To assess the quality of the summaries, implement simple evaluation metrics:
These evaluation functions use the Claude model to assess the quality of the generated summaries based on relevance, conciseness, and technical accuracy.

Create a Weave Dataset and run evaluation

Now that the scoring function is defined, the final step is to apply it to sample inputs and execute the evaluation. To evaluate the pipeline, create a Weave Dataset and run an evaluation:
Weave Dataset configuration interface for evaluation with dataset selection and configuration options
For the evaluation, use an LLM-as-a-judge approach. This technique involves using a language model to assess the quality of outputs generated by another model or system. It uses the LLM’s understanding and reasoning capabilities to provide nuanced evaluations, especially for tasks where traditional metrics may be insufficient. arXiv
Weave evaluation dashboard with Chain of Density summarization results, metrics, and performance comparisons
This code creates a dataset with the sample ArXiv paper, defines a quality scorer, and runs an evaluation of the summarization pipeline.

Conclusion

This example demonstrated how to implement a Chain of Density summarization pipeline for ArXiv papers using Weave. You learned how to:
  • Create Weave operations for each step of the summarization process
  • Wrap the pipeline in a Weave Model for tracking and evaluation
  • Implement custom evaluation metrics using Weave operations
  • Create a dataset and run an evaluation of the pipeline
Weave tracks inputs, outputs, and intermediate steps throughout the summarization process, making it easier to debug, optimize, and evaluate your LLM application. You can extend this example to handle larger datasets, implement more sophisticated evaluation metrics, or integrate with other LLM workflows. View Full Report on W&B