AI Endpoints - Build a RAG Chatbot with LangChain4j
AI Endpoints is covered by the OVHcloud AI Endpoints Conditions and the OVHcloud Public Cloud Special Conditions.
Introduction
In this tutorial, we'll show you how to build a Retrieval Augmented Generation (RAG) chatbot that enhances answers by incorporating your own custom documents into the LLM’s context.
To do this, we will use LangChain4j, Java-based framework inspired by LangChain, designed to simplify the integration of LLMs (Large Language Models) into applications. Note that LangChain4j is not officially maintained by the LangChain team, despite the similar name.
Combined with OVHcloud AI Endpoints which offers both LLM and embedding models, it becomes easy to create advanced, production-ready assistants.
Definition
Retrieval Augmented Generation (RAG): Instead of relying solely on a model's built-in knowledge, RAG injects your data into the prompt to improve relevance.
Here’s how it works:
- Your documents are converted into vectors using an embedding model.
- When the user asks a question, it’s also turned into a vector.
- A similarity search is performed to find the most relevant data chunks.
- These are fed to the LLM as context, enabling grounded, accurate responses.
Instructions
Set up the environment
In order to use AI Endpoints APIs easily, create a .env file to store environment variables:
Make sure to replace the token value (OVH_AI_ENDPOINTS_ACCESS_TOKEN) by yours. If you do not have one yet, follow the instructions in the AI Endpoints - Getting Started guide.
Of course, you can change the environment variable values above to use an other LLM or another embedding model.
Optional: PostgreSQL Configuration
If you want to persist your document embeddings across sessions or share them across services, you can use a PostgreSQL instance with the pgvector extension.
Set the following environment variables to configure your database:
However, this step is optional. Indeed, if you are just testing or don’t have a PostgreSQL database available, LangChain4j also supports an in-memory embedding store. This makes it easy to get started without setting up any external infrastructure.
To use the in-memory store, you will just need to replace the embedding store configuration in the code we are going to write later:
Installing libraries
Then, create a requirements.txt file with the following libraries:
Then, launch the installation of these dependencies:
Set up project
Update your pom.xml configuration file:
Create the RAGStreamingChatbot class
The full RAG chatbot implementation is available below:
Note that the chatbot will use the streaming mode, as explained in the Memory Chatbot with LangChain4j tutorial.
Test the chatbot without knowledge base
As you can see below, the LLM gives an answer, but not the expected one.

This is not a surprise, since the model was trained before OVHcloud created AI Endpoints. The model does not know this platform.
That is why we are going to create a knowledge base, to improve the LLM's answers.
Prepare your knowledge base
Create a folder named rag-files, located at src/main/resources/rag-files and place your .txt, .md, or other text-based documents there. These will be converted into embeddings and used during retrieval.
You can find an example file in our public-cloud-examples GitHub repository.
Transform the text in vectors using AI Endpoints
To do this, we are going to create chunks from our document. A chunk is a part of the document that will be transformed in vector.
It’s then used to perform a similarity search. This is a delicate phase, and in this example, the chunking is based on the number of characters. In a more complex use case, you will create chunks based on the meaning of the text.
Next, you transform the text in vectors and store them.
If you do not have a PostgreSQL managed instance, you can use the in-memory store as mentioned earlier (only for test purposes).
Use this RAG feature for your chatbot
Adding RAG functionality to the chatbot is easy by adding the ContentRetriever to the Assistant in the RAGStreamingChatbot class:
At this step, you have completed the development of the RAGStreamingChatbot class:
Thanks to your knowledge base, our new chatbot will answer with relevant information from your custom data source.

Conclusion
You've now created a Retrieval-Augmented Generation (RAG) chatbot using your own documents and the OVHcloud AI Endpoints platform. LangChain’s integration with Chroma and embedding models makes RAG implementation straightforward and even production-ready.
Going further
If you want to go further and deploy your chatbot in the cloud, making your interface accessible to everyone, refer to the following articles and tutorials:
- AI Deploy – Tutorial – Build & use a custom Docker image
- AI Deploy – Tutorial – Deploy a Gradio app for sketch recognition
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.
Feedback
Please feel free to send us your questions, feedback, and suggestions regarding AI Endpoints and its features:
- In the #ai-endpoints channel of the OVHcloud Discord server, where you can engage with the community and OVHcloud team members.