How to Train Chatbot using RAG and custom data

?
The RAG, representing the old age generation, describes the process where the llM (a large language model can be done by training to be drawn to the support of special, small. Usually, the LLMS like Chatgpt is trained across the Internet (billions of data points). This means that they are inclined to yellowishes and halucinations analysis.
Here is an example of a situation where the rag can be used and useful:
I want to build a US State Tour Guide Chat Bot, which contains standard information about the US States, such as their topics, people and attractions. To do this, I can download the Wikipedia pages of the US Provinces and train my LLM using text from some pages.
To create your RAG llm
One of the most popular tools to build rag systems LLamaindexwhich is:
- Simplify the combination between llms and foreign data sources
- Allows enhancements to arrange, index, and ask their data in a manner prepared for the use of the llM
- Works with many types of data, such as PDFs and text files
- Helps build a rag pipe and put the relevant chunks for data on the move before passing it to the llm with the generation of the llm.
Download your data
Start by getting the data you want to train your model. Downloading PDFs from Wikipedia (CC with 4.0) in the correct format, make sure you click Print and click As PDF. ”
Do not just send Wikipedia as PDF – LLAMA will not like the format you are on and will refuse your files.
For purposes of this article and keep things simple, I will download the next 5 popular pages:
- Floria
- Sailor
- Washington DC
- New York
- Tip
Be sure to keep this all in the folder where your project can easily get. Save them from another called “Data”.
Find the API keys required
Before creating your State States Database, there are 2 Keys of API you will need to produce.
- One from Openai, to reach the basic llm
- One from lama to access the Index database you uploaded to custom data
When you already have these API keys, keep yourself in the .env file on your project.
#.env file
LLAMA_API_KEY = ""
OPENAI_API_KEY = ""
Create a clue and upload your data
Create a llamactloud account. Once you have entered, find a portion of the index and click “Create” to create a new reference.
Reform shops and carry the documentation remotely to look at API without having to rebuild or keep them in place.
Here's how it works:
- If you create your indication, there will be a place where you can download the files to feed the model database. Put your PDFs here.
- LLamaindex Parses and chunks Docs.
- Creates an index (eg vector index, the alternate keyword).
- This ndex stored in the llamactloud.
- You can then arouse using the llm with API.
The next thing you need to do stops the embark on the model. The LLM prevention model will enhance your project and deal with Returning Relevant Information and Delivery text.
If you make a new index you want to choose “Create New Empassity”:

If you make your new embassy you have to give your Opelai API key and say your model.

Once you have created your model, leave some indicators as automatic and hit “creating index” down.
It may take a few minutes to combine and stored all documents, so make sure all the Scriptures are processed before trying to make a question. The situation should show on the right side of the screen where you make your index in the box “Index Files Summary”.
To access your model by code
Once you have created your indication, you will also receive an organization ID. With cleaning code, add your organization ID and index name to your .env file. After that, find all the variables needed to start your index to your code:
index = LlamaCloudIndex(
name=os.getenv("INDEX_NAME"),
project_name="Default",
organization_id=os.getenv("ORG_ID"),
api_key=os.getenv("LLAMA_API_KEY")
)
Ask your index and ask a question
To do this, you will need to explain the question (faster) and create an answer by calling the indication as:
query = "What state has the highest population?"
response = index.as_query_engine().query(query)
# Print out just the text part of the response
print(response.response)
Having a long discussion with your bot
By asking the answer from the LLM the way recently can occur above, you can easily access information from the documents you upload. However, if you ask the next question, like “what is the bundle?” Besides the context, the model will not remember what your first question was. This is because we have never planned to follow the history history.
To do this, you need:
- Create a memory using ChatemoryBufder
- Create a chat engine and add a constructed memory using a ContactChatengine
To create a chat engine:
from llama_index.core.chat_engine import ContextChatEngine
from llama_index.core.memory import ChatMemoryBuffer
# Create a retriever from the index
retriever = index.as_retriever()
# Set up memory
memory = ChatMemoryBuffer.from_defaults(token_limit=2000)
# Create chat engine with memory
chat_engine = ContextChatEngine.from_defaults(
retriever=retriever,
memory=memory,
llm=OpenAI(model="gpt-4o"),
)
Next, feed your question in your chat engine:
# To query:
response = chat_engine.chat("What is the population of New York?")
print(response.response)
This gives the answer: “Since 2024, the number of about 19,867,248 years.”
May I ask the following question:
response = chat_engine.chat("What about California?")
print(response.response)
This gives the following reply: “Since 2024, the population of California is 39,431,263.” As you can see, this model remembered that what we asked earlier was humanity and correctly answered.

Store
Retree generatured generation generation is an effective method to train llm in certain data. The llamactloud provides a simple and direct route to build your rag framework and ask a lying model below.
This code I used by this lesson was recorded in the brochure, but can be folded on the streapolic app to create natural biological conversation with Chatbot and discussion. I have put a broadcast code here in my Gituthub.
Thanks for reading
- Connect me to LinkedIn
- I bought a coffee to support my work!
- I give 1: 1 Data Science Tetoring, Trainard work / counseling, writing advice, refresh the review and more on toportate!



