LangChain Integration for ContextCrunch prompt compression.
Project description
ContextCrunch-LangChain-python
Integration for ContextCrunch in a LangChain pipeline.
Usage
The ContextCruncher
is a Runnable Lambda that takes in 2 inputs (as an input dictionary):
context
: This could be your conversation history, or 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.
When initializing the ContextCruncher()
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.
Return
ContextCruncher
returns a dictionary with:
context
: The updated (compressed) context.question
: The original question (for later uses in a chain).
Quickstart
Install this package with pip install contextcrunch-langchain
.
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()
)
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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee2131162c51880cff42651917dc97d9af23398f721d261a7d9530f4c7be3ebf |
|
MD5 | d19db187b912cfaff8b817c61e4a3d67 |
|
BLAKE2b-256 | 3d7899b42d973343071e3ebb85b6725b871fe44edbc1ab28bda15b8d7251e49d |
Hashes for contextcrunch_langchain-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97928a472bb9d689e060b9f9e3b9858c796ed2a5472c46c60494284c1a86e741 |
|
MD5 | 7c4738ca3068aa6edfcd54ecec0a38ca |
|
BLAKE2b-256 | eb98a3f6b741384c4817b47a0d07b9d6b78566e13a4bb88bf4737735ebe5be4b |