Skip to main content

Logo of Selective Context

Selective Context for LLMs

Selective Context compresses your prompt and context to allows LLMs (such as ChatGPT) to process 2x more content. It is especially useful in dealing with long documents and maintaining long conversations without compromising their performance on various tasks!

This repository contains the code and data for the paper: Compressing Context to Enhance Inference Efficiency of Large Language Models.

Updates!!

  • Oct 9 2023: This work has been accepted for the main proceedings of EMNLP 2023 🥳. The paper link above is the latest conference version. If you are looking for the previous arxiv version of the paper: 👉 Unlocking Context Constraints of LLMs.

  • May 6 2023: 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. Install selective-context via Pypi:

    pip install selective-context
    
  2. Import SelectiveContext:

    from selective_context import SelectiveContext
    
  3. Compress your prompt and context. The context contains the compressed context:

    sc = SelectiveContext(model_type='gpt2', lang='en')
    context, reduced_content = sc(text)
    
  4. You can also adjust the reduce ratio:

    context, reduced_content = sc(text, reduce_ratio = 0.5)
    
  5. If you prefer to try with web interface, try our streamlit app:

    streamlit run app/app.py
    

    Or directly visit our Space on Hugging Face Hub.

Code Structure

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

Experiments

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

python main.py

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.

Dataset in the paper

The dataset used in the paper can be found at:

The datasets are created by ourselves so if you need citation just use the citation of this tool.

Citation

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

@misc{li2023compressing,
      title={Compressing Context to Enhance Inference Efficiency of Large Language Models}, 
      author={Yucheng Li and Bo Dong and Chenghua Lin and Frank Guerin},
      year={2023},
      eprint={2310.06201},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

The previous version:

@misc{li2023unlocking,
      title={Unlocking Context Constraints of LLMs: Enhancing Context Efficiency of LLMs with Self-Information-Based Content Filtering}, 
      author={Yucheng Li},
      year={2023},
      eprint={2304.12102},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

License

This project is licensed under the MIT License.

Metadata

Release files for selective-context 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for selective-context 0.1.4
File Size Uploaded
selective-context-0.1.4.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for selective-context 0.1.4
File Interpreter ABI Platform
selective_context-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / selective-context-0.1.4.tar.gz

Download URL selective-context-0.1.4.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a7c9c9ec0d03fe21e362e93bd1ecd74b47be2d1dc64dfcb01f0c4a61b77135bd
BLAKE2b-256 checksum
How to use checksums
05947a6013b4b585a39cc655fcbd417d31304930ac2a7bcc38878a12c68e37af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release files / selective_context-0.1.4-py3-none-any.whl

Download URL selective_context-0.1.4-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
173a4c2034e928e8e0a96572e79ec5e81d0332dc2bcd5b731308f58b2b860eb9
BLAKE2b-256 checksum
How to use checksums
e106731800737b268647087cbdac7f6d61c595f6a3b0796a6c8b5c209a02aea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.5

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page