Skip to main content

A framework for handling long LLM prompts possible exceeding the context length

Project description

contextender

contextender is a Python package designed to handle tasks that exceed the context length of large language models (LLMs). By dividing prompts iteratively, it enables processing of large inputs while improving accuracy by splitting tasks into smaller, manageable parts.

Installation

Install the package using pip:

pip install contextender

Purpose

Large language models often have limitations on the maximum context length they can process. contextender overcomes this limitation by splitting large inputs into smaller chunks and processing them iteratively. This approach allows for handling tasks that exceed the LLM's context length and increases accuracy by focusing on smaller, more manageable subtasks.

Methods

1. summarize

Purpose: Summarizes text that may exceed the LLM's context length.

  • Use Case: When you need to summarize a large body of text that cannot fit into the LLM's context window.
  • Approach: Splits the text into smaller chunks, summarizes each chunk, and combines the results iteratively.

2. item_chooser

Purpose: Chooses one or more items from a long list by splitting the list and iteratively choosing or eliminating items.

  • Use Case: When you need to select items from a Python list that is too large to process in one go.
  • Approach: Focuses on the number of items in each split rather than the context length.

3. text_choose_item

Purpose: Chooses an item from a list embedded in a text (e.g., when the list is not a Python list).

  • Use Case: When you need to select an item from a textual list while ensuring the context length is not exceeded.
  • Approach: Focuses on splitting the text to avoid exceeding the LLM's context length.

Example Usage

Summarize a Large Text

from contextender import summarize

text = "..."  # Large text to summarize
summary = summarize(
    text=text,
    llm=my_llm_callable,  # Replace with your LLM callable that takes a prompt (str) and outputs the answer (str)
    llm_context_len=10000  # Approximate number of characters (note: not tokens!) your LLM can handle
)
print(summary)

Choose Items from a Long List

from contextender import choose_item

items = [101, 42, 123, ...]  # Long list of items (of any type that can be converted to a string)
chosen_item = item_chooser(
    context=items,
    llm=my_llm_callable,  # Replace with your LLM callable that takes a prompt (str) and outputs the answer (str)
    llm_context_len=10000,  # Approximate number of characters (note: not tokens!) your LLM can handle
    task="Choose the maximum integer"
)
print(chosen_item)

Choose an Item from a Textual List

from contextender import text_choose_item

text = "1. Option A\n2. Option B\n3. Option C\n..."  # Text containing a list
chosen_item = text_choose_item(
    context=text,
    llm=my_llm_callable,  # Replace with your LLM callable that takes a prompt (str) and outputs the answer (str)
    llm_context_len=10000,  # Approximate number of characters (note: not tokens!) your LLM can handle
    task="Choose the best option"
)
print(chosen_item)

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

contextender-0.1.1.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextender-0.1.1-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file contextender-0.1.1.tar.gz.

File metadata

  • Download URL: contextender-0.1.1.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for contextender-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8be43a95f9c4fc7814165bf9393a8f012f85be76826797b21aafbc3927162966
MD5 e01b83ab9337180889b76f3143118e4b
BLAKE2b-256 9a6d28a847779217dd7949524b5df1441e8cec3fde51bfdf7dfa22818712dfba

See more details on using hashes here.

File details

Details for the file contextender-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: contextender-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for contextender-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b792af90f010c011442b1c7a182971aa199894893097bf4b3c831b21f293cf7d
MD5 33ad654df50fa267a342929258c8972d
BLAKE2b-256 0c27c0a09c4690113b14e13bfca41e8f2edf1f5ea631605064dfaa1368bac3ca

See more details on using hashes here.

Supported by

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