LangChain Integration for ContextCrunch prompt compression.
Project description
ContextCrunch-LangChain-python
Integration for ContextCrunch in a LangChain pipeline.
Quickstart
- Install this package with
pip install contextcrunch-langchain
. - 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 RAGquestion
: 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).
Compresion Ratio
When initializing both ContextCruncher()
and ConversationCruncher()
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.
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
Built Distribution
Hashes for contextcrunch_langchain-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b18a5ae8b720472c551d08f585001a8e59fa6f001f8c321e24f36524dc02c365 |
|
MD5 | ad7d48da5405ae5029900fe27cb5e9bd |
|
BLAKE2b-256 | afa4eab777a8165c6f5ecdbbe501d44dab7ac4f1f2213f673a6ace373c443342 |
Hashes for contextcrunch_langchain-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38cb95e43a006e9b07f4484f587a057f88d70fe3d863876b05df90afbc9f8b90 |
|
MD5 | 405f382730a6e5eb7d51f41a1dbf3176 |
|
BLAKE2b-256 | 3e07e9cee8c42cf44bf28d761b55fc24b5b30de56c427e2048508673b8879d13 |