How to Build a Chat App with Your Postgres Data using Agent Cloud
Tags: opensource, postgres, tutorial, productivity
Published: 2024-05-13 15:02:38

## Introduction

Enterprises are turning to RAG (Retrieval-Augmented Generation) because it fundamentally changes how AI can be used for better decision-making.

RAG combines the power of information retrieval with AI generation, allowing businesses to:

- **Leverage vast amounts of data**: RAG unlocks the value of internal and external data sources, providing a more comprehensive picture for informed choices.

- **Enhance accuracy and confidence**: By grounding AI generation in reliable information, RAG reduces the risk of bias or errors in decision-making.

- **Streamline the process**: RAG automates parts of the information-gathering and analysis process, leading to faster and more efficient decision-making cycles.
RAG is a framework designed to improve language models by pulling in relevant, up-to-date data directly related to a user’s query and RAG chat apps allow you to chat with your data. 

In this blog, we will learn to build a RAG chatbot in minutes using [AgentCloud](https://github.com/rnadigital/agentcloud). 

> AgentCloud is an open-source platform enabling companies to build and deploy private LLM chat apps, empowering teams to securely interact with their data.

- AgentCloud uses [Airbyte](https://airbyte.com/) to build data pipelines, which allow us to split, chunk, and embed data from over 300 data sources, including Postgres.
 
- AgentCloud uses [Qdrant](https://qdrant.tech/) as the vector store to efficiently store and manage large sets of vector embeddings. For a given user query the RAG application fetches relevant documents from vector store by analyzing how similar their vector representation is compared to the query vector.

So, let’s get started building a RAG chat application using Agent Cloud to talk privately and securely with your Postgres data.

---

### Before we start building the RAG app

If you are an open Source lover and then please consider supporting Agent Cloud, by giving us a Star on GitHub ⭐️ 

Just click on the cat 🙏

[![](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZ2h6NTVuaTlwajhtM2w0MHNiMmRsZHkxbG9qYWRrb3ZwZWE1YXo4byZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ndSat6hAmqXnO/giphy.gif)](https://github.com/rnadigital/agentcloud)

Thanks. You're cool.


---

## Setting up Agent Cloud via Docker

To run AgentCloud locally, ensure Docker is installed. Follow these steps to get AgentCloud up and running.

- Clone the repo:

`git clone https://github.com/rnadigital/agentcloud.git
`
- Navigate to the agentcloud directory: `cd agentcloud`

- Run this command: 

`chmod +x install.sh && ./install.sh
`
Running the install command will download necessary Docker images and launch the containers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9b3dpg9wt37tg9qu615g.png)

Once the install script is executed successfully we can view the containers running in the docker app:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1jjd9z3hxosk6nvmd1z.png)

To access Agent Cloud in the browser, we can hit the below URL: `http://localhost:3000/register `

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gg80v1e13vqa20jttoe.png)

Next, we need to sign to the platform

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2nn2ceyvwcf4rlog841.png)

Post sign-up, and log in to the App, to get to this landing screen.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgcaffjuri1h65pvee0d.png)

Congrats, setup is complete now.

Now let's move towards building our RAG app.

---

## Adding New Model

Agent Cloud allows us to use models like FastEmbed and OpenAI in app. 

To add a new model let’s go to the Models screen and click the Add Model option.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya90e6wajyr6ye4w8b01.png)

In the configuration screen, select a Model—such as the fast-bge-small-en for embedding text. Then, save your settings to finish the model setup.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39ar00cop62kdl9fhwrl.png)

Post successfully adding the model we will be able to view the model in the Models list.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jswp4402ghknee2w3ms4.png)

---

## Creating New Datasource

The data source for our app will be a Postgres table which contains information about Indian dishes. 

Here is the snapshot of the table data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsngm4zvy4jr98ybp6y5.png)

Our table features 181 Indian dishes representing various Indian states.

Here is a description of the table columns.

- **name** : Name of the dish
- **ingredients** : Main ingredients used
- **diet** : Type of diet - either vegetarian or non-vegetarian
- **prep_time** : Preparation time
- **cook_time** : Cooking time
- **flavor_profile** : Flavor profile includes whether the dish is spicy, sweet, bitter, etc
- **course** : Course of meal - starter, main course, dessert, etc
- **state** : State where the dish is famous or is originated
- **region** : Region where the state belongs
- **meta_info**: This column contains the dish information in an unstructured format. The column data will be embedded for vector search.

To integrate table data into our app, add the data source by navigating to the Data Source page, clicking the New Connection button, and selecting a source from the modal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8mnr02qqtumtlwi61s6.png)

Let's choose Postgres as our data source and then configure the connection.

Here's what's needed to set up the Postgres data source.

- Schedule type
- Host name
- Port
- Password
- Postgres username
- Postgres password
- Database name

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4d6olovfnv64kx8l4dly.png)

Let's disabled the SSL modes as we're running Postgres on our local machine.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82oblotary9n5x5i5c0g.png)

Next, we need to select the replication method; I have selected Xmin. 

Xmin replication is the new cursor-less replication method for Postgres. 

Cursorless syncs enable syncing new or updated rows without explicitly choosing a cursor field. The Xmin system column is used to track inserts and updates to your source data. 

Now, we need to click on the Test and Save button, which will first test the DB connection and, if successful, save the data connector.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qa88321f0p1421j3wu4p.png)


We'll select the table and all its columns.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqk84s3o2rmepznru320.png)

The column meta_info is selected for embedding, which means the vector search will be performed on the contents of this column.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmejc3pc0c873miuwc3s.png)

Then we press the Continue button to create the Data Source.

It will take some time to embed the data, and post that you will be able to see this data in a new collection in the Qdrant collection dashboard. 

After setting up the data source, Agent Cloud uses Airbyte in the background to create a data pipeline between Postgres and Qdrant.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/348kghsn0603avymu4fo.png)

Once the sync is complete, the new collection will appear in the Qdrant DB.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xe02fi8ibu7lh81hu3p.png)

Example of how the data is stored inside a Qdrant point.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/druwwemk654flw3n36gi.png)

Next, we will be setting up a tool for using our data source.

---

## Setting up Tools

Tools are essential for enabling the AI agent to interact effectively with its environment, process information, and take appropriate actions to achieve its goals. 

The tools used by an AI agent can include functions, APIs, data sources, and other resources that help the agent perform specific tasks autonomously and efficiently. 

The tool we will be setting up will be responsible for querying the data source and fetching relevant documents. Agent Cloud creates a tool for us when we add a new data source by default.

The screenshot below shows that the Postgres Food data tool has been automatically created by the platform.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymca3k64j48veax9nm5t.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3c3n24wmpklvargznee8.png)

I have updated the tool configuration to change the description. 

This description is very important as it will be used by the Agent to decide which tool to use for a given task.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdaogp313pt4u2o68ct4.png)

Click on the Save button to update the tool.

---

## Creating Agent

An Agent is an intelligent system that can efficiently accomplish complex tasks. 

To create an Agent, let’s go to the Agents screen and click on the New Agent button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdkhwegv644fmb78164t.png)

In the Agent configuration, we need to provide certain information one of the information is the LLM Model. Agents use LLMs to enhance their capabilities and improve communication. 

We are using gpt-4-0125-preview OpenAI model for our agent.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29ubevjd0fvx787jf8dm.png)

Here are the remaining details needed for configuration:

- Agent Name
- Agent role
- Agent Goal
- Agent Backstory
- Agent Tools

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2jaz8kyv6ujzu2zf5uk.png)

Now click on the Save button to complete the Agent configuration.

---

## Creating Task

Tasks are specific assignments assigned to an agent for completion.

To create a new task let’s click the `Add Task` button in the Task screen.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twmhj8pzg7hvk23qj5rf.png)

When configuring the task, provide the following information:

- Task name
- Task description
- Task tools
- Task Agent

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4jr5t4cas9fajvs0xiv.png)

Post that we can click on the Save button to save the configuration.

---

## Creating App

So till now, we have created a data source for the App, a tool to retrieve to information from the data source, an LLM Agent that will be working on complex tasks using the tool, and a Task that will be assigned to the Agent based on the user’s instruction. 

Now we will be creating a conversational App that will allow us to chat with our data. 

To create the App, let’s go to the App screen and click on the New App button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3aat889vygrqgitzsvm3.png)

In the configuration page, let’s enter the app name and select the app type, which will be a conversation chat app in our case. 

Select the Task and Agent that we created. In the process, we will select sequential, and finally, in the Chat manager model, we will select the OpenAI GPT 4 model. 

Now we can click on the Save button to complete the app setup.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fybc990dy24dm3229sm.png)

After completing the app setup, the newly created app will appear in the dashboard.

Now let's see the app in action by clicking the Play button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0xjr4ugzs92f0h1puye.png)


A conversion window is opened for us where we can chat with our data.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2izvc3srvf12dij8t8i.png)

Here is the app's response to our question.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5yfdjcnzdwr6tufd1g4.png)

Yay, we did it 😎

---

## Conclusion

In this blog, we learned to build a RAG chat app with Agent Cloud and Postgres. 

> We covered creating a data source, embedding it, and storing it in Qdrant DB. Additionally, we learned how to build tools for Agents and create an app where users can interact with their private data.

### 🔍 Want to learn more about Agent Cloud?

1. [Agent Cloud vs. CrewAI](https://www.agentcloud.dev/blog/agent-cloud-vs-crewai-a-comparison)
2. [Agent Cloud vs. OpenAI](https://www.agentcloud.dev/blog/agent-cloud-vs-openai)
3. [Agent Cloud vs. Vertex AI Agents](https://www.agentcloud.dev/blog/agentcloud-vs-google-cloud-agents)
4. [How to Build a RAG Chatbot using Agent Cloud and BigQuery](https://www.agentcloud.dev/blog/a-rag-chat-app-with-agent-cloud-and-bigquery)
5. [How to Build a RAG Chatbot Using Agent Cloud And MongoDB](https://www.agentcloud.dev/blog/build-rag-chatbot-agentcloud-mongodb)