DEV Community

Cover image for GPT-4o: Learn how to Implement a RAG on the new model, step-by-step!
Vinicius Mesel
Vinicius Mesel

Posted on

GPT-4o: Learn how to Implement a RAG on the new model, step-by-step!

OpenAI just released their latest GPT model, GPT-4o, which is half of the price of the current GPT-4 model and way faster.

In this article, we will show you:

  1. What is a RAG
  2. Getting started: Setting up your OpenAI Account and API Key
  3. Using Langchain's Approach on talkdai/dialog
  4. Using GPT-4o in your content

PS: Before following this tutorial, we expect you to have a simple knowledge of file editing and docker.

What is a RAG?

RAG is a framework that allows developers to use retrieval approaches and generative Artificial Intelligence altogether, combining the power of a large language model (LLM) with an enormous amount of information and a specific custom knowledge base.

Getting started: Setting up your OpenAI Account and API Key

To get started, you will need to access the OpenAI Platform website and start a new account, if you still don't have one. The link to get into the form directly is:

Sign up screen from OpenAI Platform

Whenever you submit the form with your e-mail and password, you will receive an e-mail (just like the one below) from OpenAI to activate and verify your account.

Verify your account e-mail

If you, for some reason, don't see this e-mail in your inbox, try checking your spam. If it's still missing, just hit the resend button on the page to verify your e-mail address.

And... Congrats! You now are logged in to OpenAI Platform.

OpenAI Platform after login

Now, let's generate your API Token to be able to use the Chat Completion endpoint for GPT-4o, allowing you to use it with talkdai/dialog, a wrapper for Langchain's with a simple-to-use API.

On the left menu, you will be able to find a sub-menu item called: API Keys, just click on it and you will be redirected to the API Keys.

API Keys menu item

If it's your first time setting up your account, you will be required to verify your phone number.

Verify your phone number pop up at the API Keys page

After you verify your phone number, the Create new secret key will be enabled as the following image shows:

Create your secret button now is enabled

OpenAI keys are now project-based, which means you will be assigned a project as a "host" of this generated key on your account, it will enable you to have more budget control and gain more observability on costs and tokens usage.

Create a new secret key prompt

To make the process easier, we will just name our API Key and use the "All" permission type, for production scenarios, this is not the best practice and we recommend you set up permissions wisely.

After you hit Create, it will generate a unique secret key that you must store in a safe place, this key is only visualizable once, so if you ever need to get it again, you will need to get it from the place you saved or generate a new one.

OpenAI API Key

Using Langchain's approach on talkdai/dialog

What is Langchain?

Langchain is a framework that allows users to work with LLM models using chains (a concept that is the sum of a prompt, an LLM model, and other features that are extensible depending on the use case).

This framework has native support for OpenAI and other LLM models, granting access to developers around the globe to create awesome applications using a Generative AI approach.

What is talkdai/dialog?

talkdai/dialog, or simply Dialog, is an application that we've built to help users deploy easily any LLM agent that they would like to use (an agent is simply an instance of a chain in the case of Langchain, or the joint usage of prompts and models).

The key objective of talkdai/dialog is to enable developers to deploy LLMs in less than a day, without having any DevOps knowledge.

Setting up Dialog

On your terminal, in the folder of your choice, clone our repository with the following command, in it you will have the basic structure to easily get GPT-4o up and running for you.

git clone
Enter fullscreen mode Exit fullscreen mode

In the repository, you will need to add 3 files:

  • Your .env file, where that OpenAI API Key will stay stored (please, don't commit this file in your environment). This will solely based on the .env.sample file we have on the root of the repository.

  • The file where you will define your prompt (we call it prompt.toml in the docs, but you can call it whatever you want)

  • And, last but not least important, a CSV file with your content.

.env file

Copy and paste the .env.sample file to the root directory of the repository and modify it using your data.

PORT=8000 # We recommend you use this as the default port locally
OPENAI_API_KEY= # That API Key we just fetched should be put here
DIALOG_DATA_PATH=./know.csv # The relative path for the csv file inside the root directory
PROJECT_CONFIG=./prompt.toml # The relative path for your prompt setup
DATABASE_URL=postgresql://talkdai:talkdai@db:5432/talkdai # Replace the existing value with this line
STATIC_FILE_LOCATION=static # This should be left as static
LLM_CLASS=dialog.llm.agents.lcel.runnable # This is a setting that define's the Dialog Model Instance we are running, in this case we are running on the latest LCEL version
Enter fullscreen mode Exit fullscreen mode

Prompt.toml - your prompt and model's settings

This file will let you define

model_name = "gpt-4o"
temperature = 0.1

prompt = """
You are a nice bot, say something nice to the user and try to help him with his question, but also say to the user that you don't know totally about the content he asked for.
Enter fullscreen mode Exit fullscreen mode

This file is quite straightforward, it has 2 sections: one for defining model details, which allows us to tweak temperature and model values.

The second section is the most interesting: it's where you will define the initial prompt of your agent. This initial prompt will guide the operation of your agent during the instance's life.

Most of the tweaks will be done by changing the prompt's text and the model's temperature.

Knowledge base

If you want to add more specific knowledge in your LLM that is very specific to your context, this CSV will allow you to do that.

Right now, the CSV must have the following format:

  • category - the category of that knowledge;
  • subcategory - the subcategory of that knowledge;
  • question - the title or the question that the content from that line answers, and
  • content - the content in itself. This will be injected in the prompt whenever there is a similarity in the user's input with the embeddings generated by this content.

Here is a sample example:

faq,football,"Whats your favorite soccer team","My favorite soccer team is Palmeiras, from Brazil. It loses some games, but its a nice soccer team"
Enter fullscreen mode Exit fullscreen mode

Langchain's behind the scene

In this tutorial, we will be using an instance of a Dialog Agent based on Langchain's LCEL. The code below is available in the repository you just cloned.

# For the sake of simplicity, I've removed some imports and comments

chat_model = ChatOpenAI(
        name="GPT Model",
        description="The GPT model to use"
        description="The temperature to use"

prompt = ChatPromptTemplate.from_messages(
            Settings().PROJECT_CONFIG.get("prompt").get("prompt", "What can I help you with today?")
            "Here is some context for the user request: {context}"
        ("human", "{input}"),

def get_memory_instance(session_id):
    return generate_memory_instance(

retriever = DialogRetriever(
    session = next(get_session()),

def format_docs(docs):
    return "\n\n".join([d.page_content for d in docs])

chain = (
        "context": itemgetter("input") | retriever | format_docs,
        "input": RunnablePassthrough(),
        "chat_history": itemgetter("chat_history")
    | prompt
    | chat_model

runnable = RunnableWithMessageHistory(
Enter fullscreen mode Exit fullscreen mode

The first lines are where we define our model temperature and name. By default, the model will be gpt-3.5-turbo and the temperature 0, but since we defined it in the prompt configuration file, it will be changed to gpt-4o and the temperature to 0.1.

To send a prompt inside Langchain, you need to use its template, which is what we do next on the ChatPromptTemplate.from_messages instance.

In the next lines, we define the memory, how it should be consumed and the instance of the retriever (where we are going to grab our custom data from).

Using GPT-4o in your content

After this long setup, it's time to run our application and test it out. To do it, just run:

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

With this command, your docker should be up and running after the image builds.

After the logs state: Application startup complete., go to your browser and put the address you've used to host your API, in the default case, it is: http://localhost:8000.

Go to the /ask endpoint docs, fill in the JSON with the message you want to ask GPT and get the answer for it like the following screenshot:

It works!

That's all! I hope you enjoyed this content and using talkdai/dialog as your way-to-go app for deploying Langchain RAGs and Agents.

Top comments (5)

noberst profile image

Great article!

lokeshgene profile image

Good one 👍

richard_tong_b7a577a1106a profile image
Richard Tong

How is GPT-4o multimodal capability used in such RAG project?

vmesel profile image
Vinicius Mesel

Hey Richard, you can setup a multi-agent structure to use it. Right now, our project doesn't support this type of architecture yet.

ifredy profile image