New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Foundation for advanced RAG #538
Merged
Merged
+4,813
−151
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
I am just reading up on the article, but so far I like what I see! cc @cescoffier and @maxandersen who I am sure will be interested in this as well |
This was referenced Jan 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So far, LangChain4j had only a simple (a.k.a., naive) RAG implementation: a single
Retrieverwas invoked on each interaction with the LLM, and all retrievedTextSegmentswere appended to the end of theUserMessage. This approach was very limiting.This PR introduces support for much more advanced RAG use cases. The design and mental model are inspired by this article and this paper, making it advisable to read the article.
This PR introduces a
RetrievalAugmentorinterface responsible for augmenting aUserMessagewith relevant content before sending it to the LLM. TheRetrievalAugmentorcan be used with bothAiServicesandConversationalRetrievalChain, as well as stand-alone.A default implementation of
RetrievalAugmentor(DefaultRetrievalAugmentor) is provided with the library and is suggested as a good starting point. However, users are not limited to it and can have more freedom with their own custom implementations.DefaultRetrievalAugmentordecomposes the entire RAG flow into more granular steps and base components:QueryTransformerQueryRouterContentRetriever(the oldRetrieveris now deprecated)ContentAggregatorContentInjectorThis modular design aims to separate concerns and simplify development, testing, and evaluation. Most (if not all) currently known and proven RAG techniques can be represented as one or multiple base components listed above.
Here is how the decomposed RAG flow can be visualized:

This mental and software model aims to simplify the thinking, reasoning, and implementation of advanced RAG flows.
Each base component listed above has a sensible and simple default implementation configured in
DefaultRetrievalAugmentorby default but can be overridden by more sophisticated implementations (provided by the library out-of-the-box) as well as custom ones. The list of implementations is expected to grow over time as we discover new techniques and implement existing proven ones.This PR also introduces out-of-the-box support for the following proven RAG techniques: