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.summarizer 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.list_item_chooser import item_chooser

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.text_item_chooser 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.0.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.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextender-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 c0511411304604ad09b3f88b80159d134080ef9f8d8325caab5825e63106b5ff
MD5 fa0c56a50b8aa96265310fb46934f96b
BLAKE2b-256 76b251283105dfce067ada93d5ce580429ad1a0f9d32ea6c8eb200e15ffdbcf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contextender-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a5e5aa74a52f7b36d422de14ef67734d9f69ee878c21646400fdbb3e0c4ec00
MD5 987903f704c5933244229e9bc1cd3dfb
BLAKE2b-256 0b6548004f99d83b2142da3b29ddc6e124eb87c4fd5779c8fed5c547f98caf0d

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