Skip to content
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 31 commits into from Jan 26, 2024
Merged

Foundation for advanced RAG #538

merged 31 commits into from Jan 26, 2024

Conversation

langchain4j
Copy link
Owner

@langchain4j langchain4j commented Jan 23, 2024

So far, LangChain4j had only a simple (a.k.a., naive) RAG implementation: a single Retriever was invoked on each interaction with the LLM, and all retrieved TextSegments were appended to the end of the UserMessage. 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 RetrievalAugmentor interface responsible for augmenting a UserMessage with relevant content before sending it to the LLM. The RetrievalAugmentor can be used with both AiServices and ConversationalRetrievalChain, 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.

DefaultRetrievalAugmentor decomposes the entire RAG flow into more granular steps and base components:

  • QueryTransformer
  • QueryRouter
  • ContentRetriever (the old Retriever is now deprecated)
  • ContentAggregator
  • ContentInjector

This 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:
advanced-rag

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 DefaultRetrievalAugmentor by 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:

@langchain4j langchain4j marked this pull request as ready for review January 23, 2024 17:43
@geoand
Copy link
Contributor

geoand commented Jan 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants