Skip to main content

ContextCrunch-LangChain-python

Integration for ContextCrunch in a LangChain pipeline.

Quickstart

  1. Install this package with pip install contextcrunch-langchain.
  2. Add your ContextCrunch API key in your environment file, such as CONTEXTCRUNCH_API_KEY="aaa-bbb-ccc-ddd"

RAG

You can easily modify an existing RAG pipeline by simply applying a ContextCruncher() to the context before filling the prompt template.

For example, if you are using this example from the LangChain docs, the modified pipeline becomes:

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | ContextCruncher(compression_ratio=0.95)
    | prompt
    | llm
    | StrOutputParser()
)

Conversations

You can use ConversationCruncher() to compress a long message history.

Here is an example using ConversationBufferMemory, which is a LangChain memory module that stores the entire conversation history.

model = ChatOpenAI()
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "Conversation Summary:\n{history}"),
        ("human", "{input}"),
    ]
)
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("My favourite color is purple, my favourite food is pizza.")
memory.chat_memory.add_ai_message("I understand. Your favourite color is purple, and your favourite food is pizza.")

chain = (
    {'history': RunnableLambda(memory.load_memory_variables) | itemgetter("history"), 'input': RunnablePassthrough()} # Fetch the history, feed the input to the next step
    | ConversationCruncher() # history and input is compressed, and fed into the prompt template (which takes 'history' and 'input' as inputs).
    | prompt
    | model
)
chain.invoke("What is favourite color?") # small contexts won't get compressed, so ConversationCruncher() will act as a passthrough.

Usage

ContextCruncher (RAG)

The ContextCruncher is a Runnable Lambda that takes in 2 inputs (as an input dictionary):

  • context: This is the retrieved information from RAG
  • question: The relevant query to find in the data. ContextCrunch uses this narrow down the context to only the most essential parts.

Return ContextCruncher returns a dictionary with:

  • context: The updated (compressed) context.
  • question: The original question (for later uses in a chain).

ConversationCruncher (Chat)

The ConversationCruncher is a Runnable Lambda that takes in 2 inputs (as an input dictionary):

  • history: Your relevant conversation history.
  • input: The most recent user message. ContextCrunch uses this narrow down the conversation history to the parts relevant to the input.

Return ConversationCruncher returns a dictionary with:

  • history: The compressed message history, as a single string. Ideally, you can feed this into a system message indicating that this is the conversation history.
  • input: The user message, unmodified (for later uses in a chain).

ContextCrunchDocumentCompressor (RAG w/ Documents)

As an alternative to ContextCruncher, if your data is already in the form of LangChain Documents, or if you would prefer to work with a larger document compression pipeline, you can use ContextCrunchDocumentCompressor. It takes in a list of documents, and a query string.

Here's how it would look in a typical document compression pipeline:

import bs4
from contextcrunch_langchain_python import ContextCrunchDocumentCompressor
from langchain import hub
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

cc_compressor = ContextCrunchDocumentCompressor(compression_ratio=0.8)
## Make sure to initialize base retriever as you would like, for example, here is a web scraper/splitter with chromadb, from the LangChain docs
loader = WebBaseLoader(
    web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
    bs_kwargs=dict(
        parse_only=bs4.SoupStrainer(
            class_=("post-content", "post-title", "post-header")
        )
    ),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
base_retriever = vectorstore.as_retriever()

# now we wrap the base_retrievier in a compression retriever using ContextCrunch
retriever = ContextualCompressionRetriever(base_compressor=cc_compressor, base_retriever=base_retriever)

llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)
rag_chain.invoke("What is Task Decomposition?")

Compresion Ratio

When initializing both ContextCruncher(), ConversationCruncher(), or ContextCrunchDocumentCompressor() there is also an optional compression_ratio parameter that controls how aggresively the algorithm should compress. The general trend is the higher the compression ratio, the less information is retained. Generally, a compression ratio of 0.9 is a good start, though for small contexts, the algorithm may compress less than requested compression ratio.

Metadata

Release files for contextcrunch-langchain 1.0.2

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

Source distribution (sdist)

Source distribution for contextcrunch-langchain 1.0.2
File Size Uploaded
contextcrunch_langchain-1.0.2.tar.gz 83.7 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for contextcrunch-langchain 1.0.2
File Interpreter ABI Platform
contextcrunch_langchain-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 83.7 MB

Release files / contextcrunch_langchain-1.0.2.tar.gz

Download URL contextcrunch_langchain-1.0.2.tar.gz
Size 83.7 MB
Tags Source
SHA-256 checksum
How to use checksums
dca8cae1ea0f4953d185946274a672c6b90a8428874bcf182d244f42de91dd6f
BLAKE2b-256 checksum
How to use checksums
3460f08790e7a44a5cf88fbe6557f67f0bd992c08dc940482087e85223dd8c7b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / contextcrunch_langchain-1.0.2-py3-none-any.whl

Download URL contextcrunch_langchain-1.0.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ff39acc048ec7c2ce5f64c865642144789e2a85ec808e513785a33bf0f37d46
BLAKE2b-256 checksum
How to use checksums
d21c6320c38381daedd5a25358173d754c78eea660d1f2a724109ae60192c09f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

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