iTranslated by AI
How LangChain's Contextual Compression Compresses Context
An abstraction called Contextual Compression has been added to LangChain. An overview can be found below.
Contextual Compression focuses on the fact that the "text of documents to be indexed" and "text used as context in prompts" have different characteristics. It improves performance by applying transformation processes to the text content included in prompts as post-processing after document retrieval.
Prerequisites
It's fine as long as you understand the general framework: "To have an LLM generate an answer to a question, text previously retrieved as context is inserted into the prompt."
The following slides I read recently were easy to understand.
Retriever to be used
For the document retrieval, I will use the retriever created in Debugging ChatGPT Retriever Plugins with LangChain.
When searching for "最近話題になった英語の勉強方法は?" (What are the English study methods that have become a hot topic recently?), the following three documents were retrieved from Pinecone.
base_retriever = ChatGPTPluginRetriever(url="http://localhost:3333", bearer_token="XXX")
docs = base_retriever.get_relevant_documents("最近話題になった英語の勉強方法は?")
for doc in docs:
print(f"""[{doc.metadata['metadata']['title'] or 'NO TITLE'}]({doc.metadata['metadata']['url']})
{doc.page_content}\n""")
[ChatGPTを使って英語の勉強をする - Qiita](https://qiita.com/s7353109/items/a5a2ed5f5dd0b41d0768)
ChatGPTを使って英語の勉強をする - Qiita はじめに 最近話題のChatGPTですが、色々触っているうちに、 これ勉強に使えるんじゃね? と思ったので、まず英語について試してみたところ、意外とうまくいったのでここに書きたいと思います。 追:週間トレンドに乗りました!!ありがとうございます!! 英訳問題 出題 まず、 日本語を英語に訳す問題を、答えを表示させずに出題して下さい。 と入力すると、 上の写真のように問
[話しやすい雰囲気を作るために意識していること - Qiita](https://qiita.com/hisamura333/items/befacfe24a43a059b038)
ることを今回は言語化
[スピーク(Speak)- 英会話アプリ](https://apps.apple.com/jp/app/%E3%82%B9%E3%83%94%E3%83%BC%E3%82%AF-speak-%E8%8B%B1%E4%BC%9A%E8%A9%B1%E3%82%A2%E3%83%97%E3%83%AA/id1286609883)
スピーク(Speak)- 英会話アプリ コンテンツが増えました! これまでは超初級と初級、それぞれだいたい1ヶ月分ずつでしたが、6日間コースで超初級、初級、中級が増え、AI講師機能も増えてました! オンライン英会話では反復がためらうんですが、これなら反復しやすいです! また、挨拶からその日のコンテンツの内容まで流れがあっていいです。シチュエーション別の暗記、というよりは会話をしている感じがあり、よくできた、
ContextualCompressionRetriever
ContextualCompressionRetriever compresses documents by specifying a single Compressor.
When using EmbeddingsFilter as a Compressor, it additionally performs the following:
- Creating Embeddings for the body text of each document found in the search.
- Filtering by comparing similarity with the input text.
In the code, you set similarity_threshold=0.86 for the EmbeddingsFilter and wrap it with ContextualCompressionRetriever as shown below.
This ensures that documents below the threshold are filtered out and excluded from the results.
base_compressor = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.86)
retriever = ContextualCompressionRetriever(
base_compressor=base_compressor, base_retriever=base_retriever
)
docs = retriever.get_relevant_documents("最近話題になった英語の勉強方法は?")
for doc in docs:
print(f"""[{doc.metadata['metadata']['title'] or 'NO TITLE'}]({doc.metadata['metadata']['url']})
{doc.page_content}\n""")
The search result is now reduced to a single item.
> python contextual_compression.py
[ChatGPTを使って英語の勉強をする - Qiita](https://qiita.com/s7353109/items/a5a2ed5f5dd0b41d0768)
ChatGPTを使って英語の勉強をする - Qiita はじめに 最近話題のChatGPTですが、色々触っているうちに、 これ勉強に使えるんじゃね? と思ったので、まず英語について試してみたところ、意外とうまくいったのでここに書きたいと思います。 追:週間トレンドに乗りました!!ありがとうございます!! 英訳問題 出題 まず、 日本語を英語に訳す問題を、答えを表示させずに出題して下さい。 と入力すると、 上の写真のように問
EmbeddingsFilter Implementation
The implementation of EmbeddingsFilter is a simple comparison of embeddings using numpy.
DocumentCompressorPipeline
DocumentCompressorPipeline is a class for stacking an arbitrary number of document transformation processes (transformers).
In the following example:
- Use
CharacterTextSplitterto split the documents and increase their count (default is by newline characters). - Use
EmbeddingsRedundantFilterto filter and reduce redundant documents that have too high a similarity with the query. - Further narrow down and refine the results using the
EmbeddingsFilterintroduced in the previous section.
By including the splitting by CharacterTextSplitter, the internal content of the documents also changes.
embeddings = OpenAIEmbeddings()
base_retriever = ChatGPTPluginRetriever(url="http://localhost:3333", bearer_token="XXX")
splitter = CharacterTextSplitter(chunk_size=20, chunk_overlap=0, separator="。")
redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings, similarity_threshold=0.85)
relevant_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=0.8)
pipeline_filter = DocumentCompressorPipeline(
transformers=[splitter, redundant_filter, relevant_filter]
)
input = "最近話題になった英語の勉強方法は?"
docs = base_retriever.get_relevant_documents(input)
query = '英語' # Example showing that the search query and compression query can be separated
filtered_docs = pipeline_filter.compress_documents(docs, query)
for doc in filtered_docs:
print(f"""[{doc.metadata['metadata']['title'] or 'NO TITLE'}]({doc.metadata['metadata']['url']})
{doc.page_content}\n""")
The number of documents has been reduced to one. Additionally, you can see that the body text has been truncated.
> python contextual_compression_pipeline.py
[ChatGPTを使って英語の勉強をする - Qiita](https://qiita.com/s7353109/items/a5a2ed5f5dd0b41d0768)
ChatGPTを使って英語の勉強をする - Qiita はじめに 最近話題のChatGPTですが、色々触っているうちに、 これ勉強に使えるんじゃね? と思ったので、まず英語について試してみたところ、意外とうまくいったのでここに書きたいと思います
EmbeddingsRedundantFilter Implementation
The implementation of EmbeddingsRedundantFilter is similar to EmbeddingsFilter, using numpy in the reverse direction.
Discussion