Skip to main content
This is an interactive notebook. You can run it locally or use the links below:

Summarization using Chain of Density

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.

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, we’ll use Weave to implement and evaluate a Chain of Density summarization pipeline for ArXiv papers. You’ll learn how to:
  1. Track your LLM pipeline: Use Weave to automatically log inputs, outputs, and intermediate steps of your summarization process.
  2. Evaluate LLM outputs: Create rigorous, apples-to-apples evaluations of your summaries using Weave’s built-in tools.
  3. Build composable operations: Combine and reuse Weave operations across different parts of your summarization pipeline.
  4. Integrate seamlessly: 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 leverages Weave’s capabilities for model serving, evaluation, and result tracking.

Set up the environment

First, let’s set up our environment and import the necessary libraries:
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
We’re using Weave to track our experiment and Anthropic’s Claude model for text generation. The weave.init(<project name>) call sets up a new Weave project for our summarization task.

Define the ArxivPaper model

We’ll create a simple ArxivPaper class to represent our data:
This class encapsulates the metadata and content of an ArXiv paper, which will be the input to our summarization pipeline.

Load PDF content

To work with the full paper content, we’ll add a function to load and extract text from PDFs:

Implement Chain of Density summarization

Now, let’s implement the core CoD summarization logic using Weave operations:
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.
By using @weave.op() decorators, we ensure that Weave tracks the inputs, outputs, and execution of these functions.

Create a Weave Model

Now, let’s wrap our summarization pipeline in a Weave Model:
This ArxivChainOfDensityPipeline class encapsulates our summarization logic as a Weave Model, providing several key benefits:
  1. Automatic experiment tracking: Weave captures inputs, outputs, and parameters for each run of the model.
  2. Versioning: Changes to the model’s attributes or code are automatically versioned, creating a clear history of how your summarization pipeline evolves over time.
  3. Reproducibility: The versioning and tracking make it easy to reproduce any previous result or configuration of your summarization pipeline.
  4. Hyperparameter management: Model attributes (like model and density_iterations) are clearly defined and tracked across different runs, facilitating experimentation.
  5. Integration with Weave ecosystem: Using weave.Model allows seamless integration with other Weave tools, such as evaluations and serving capabilities.

Implement evaluation metrics

To assess the quality of our summaries, we’ll 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

To evaluate our pipeline, we’ll create a Weave Dataset and run an evaluation:
For our evaluation, we’ll 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 leverages the LLM’s understanding and reasoning capabilities to provide nuanced evaluations, especially for tasks where traditional metrics may fall short. arXiv
This code creates a dataset with our sample ArXiv paper, defines a quality scorer, and runs an evaluation of our summarization pipeline.

Conclusion

In this example, we’ve demonstrated how to implement a Chain of Density summarization pipeline for ArXiv papers using Weave. We’ve shown how to:
  1. Create Weave operations for each step of the summarization process
  2. Wrap the pipeline in a Weave Model for easy tracking and evaluation
  3. Implement custom evaluation metrics using Weave operations
  4. Create a dataset and run an evaluation of the pipeline
Weave’s seamless integration allows us to track inputs, outputs, and intermediate steps throughout the summarization process, making it easier to debug, optimize, and evaluate our 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