Skip to main content

Compress your prompt and context to let LLMs deal with 2x more content.

Project description

Logo of Selective Context

Selective Context for Large Language Models

Selective Context is a novel approach for optimizing the usage of context in Large Language Models (LLMs) by filtering out less informative content. This approach allows LLMs to process long documents and maintain extended conversations more efficiently without compromising their performance on various tasks.

This repository contains the code and data for the paper: Unlocking Context Constraints of LLMs: Enhancing Context Efficiency of LLMs with Self-Information-Based Content Filtering.


Try our demo on Huggingface Space.

Key Features

  • Efficient Context Management: Selective Context maximizes the utility of fixed context length in LLMs, allowing them to process long documents and extended conversations more efficiently.
  • Informativeness Evaluation: Our method employs a base language model to compute self-information for lexical units (sentences, phrases, or tokens) in a context and use it to evaluate their informativeness.
  • Extensive Evaluation: We provide extensive evaluations of Selective Context on three data sources (arxiv papers, BBC news articles, and conversation transcripts) and four different NLP tasks (summarization, question answering, original context reconstruction, and conversation).

Getting Started

To get started, follow these steps:

  1. Clone the repository:

    git clone
    cd Selective_Context
  2. Install the required packages:

    pip install -r requirements.txt
  3. Run the Selective Context demo:

  4. If you prefer to try with web interface, try our streamlit app:

    streamlit run app/

Code Structure

  • A demo for performing context reduction using Selective Context.
  • The main module for managing context and implementing the Selective Context algorithm.
  • The main script for running experiments and evaluating the effectiveness of Selective Context.
  • A helper module for managing question answering tasks during the experiments.


To reproduce the experiments from the paper, run the following command:


This will run the experiments on arxiv papers, BBC news articles, and conversation transcripts with four different NLP tasks: summarization, question answering, original context reconstruction, and conversation.


If you find this repository helpful or use our method in your research, please consider citing our paper:

      title={Unlocking Context Constraints of LLMs: Enhancing Context Efficiency of LLMs with Self-Information-Based Content Filtering}, 
      author={Yucheng Li},


This project is licensed under the MIT License.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

selective-context-0.1.0.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

selective_context-0.1.0-py3-none-any.whl (6.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page