How to Build an AI-based Real Estate Recommendation Agent
Tags: realestate, investment, ai, gpt3
Published: 2024-02-02 13:12:02

Real estate investors are always looking for golden opportunities to invest and build their portfolios.

But finding a suitable company to help guide those investments can be tedious.




How can real estate investors choose the right company to partner with and ensure that they'll be making solid, profitable bets? This can be solved by creating a Q&A model to answer any investor's questions about a property investment company. MindsDB's [integration with LlamaIndex](https://docs.mindsdb.com/integrations/ai-engines/llamaindex?_gl=1*131m7nx*_ga*MTc5OTkzMDk1OC4xNzA0NDcyMjAz*_ga_7LGFPGV6XV*MTcwNDQ4ODY4Mi4yLjEuMTcwNDQ4OTkyOS42MC4wLjA.) assists with querying the company's website and providing information to help the investor make an informed decision.

### What is MindsDB?

[MindsDB](https://mindsdb.com/) is an AI automation platform that connects any source of data with any AI/ML framework, automating how real-time data flows between them. It’s a unique tool that can help you to build AI-powered features and applications quickly and easily.

In this tutorial, we'll explore how to create a Q&A model that can query the website of [Global Investments Inc](https://globalinvestsinc.com/)., a property investment company focused on making U.S. investments simple and accessible for investors. We'll zoom in specifically on the company's investment [properties in Detroit](https://globalinvestsinc.com/detroit-investment-property/), a hotspot for American real estate investors.

We'll use [LlamaIndex](https://www.llamaindex.ai/) as the engine and [OpenAI](https://openai.com/) as the LLM.

### Creating a Q&A model with MindsDB and LlamaIndex

In order to create a model, you'll create the ML engine using the `CREATE ML_ENGINE` syntax in the SQL Editor. Use `llama_index` as the engine name.

```sql
CREATE ML_ENGINE llama_index_engine
FROM llama_index;
```

On execution, you get:

```sql
Query successfully completed
```

Once you've created the engine, you can create the model with the `CREATE MODEL` syntax. The datasource will be uploaded as a file.

Read more documentation on how to upload a file to MindsDB [here](https://docs.mindsdb.com/sql/create/file). You can find a sample dataset [here](https://github.com/chandrevdw31/mindsdb-tutorials/blob/main/Datasets/Global%20investment.csv).

```sql
CREATE MODEL global_investment_qa
FROM files
   (SELECT * FROM global_investment)
PREDICT answer
USING
   engine = 'llama_index_engine',
   index_class = 'GPTVectorStoreIndex',
   reader = 'DFReader',
   source_url_link = 'https://globalinvestsinc.com/detroit-investment-guide/',
   input_column = 'question',
   openai_api_key = 'your_openai_api_key';
```

Where:

| Expression | Description |
| --- | --- |
| `global_investment_qa` | The name provided to the model. |
| `engine` | The engine used, which is LlamaIndex. |
| `qa_blackrock` | The name provided of the model. |
| `engine` | The engine name - here, LlamaIndex. |
| `index_class` | The vector store index used. |
| `reader` | The reader that reads the data/webpage. |
| `input_column` | Prompt provided as a question to the model. |
| `source_url_link` | The webpage link. |
| `input_column` | The column that stores prompt to the model. |
| `openai_api_key` | OpenAI API key. |

On execution, your screen will look like this:



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


The status of the model can be verified that it has trained successfully into a complete status by using the `DESCRIBE` syntax.

```sql
DESCRIBE global_investment_qa
```

On execution, your screen will look like this:



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


### Prompting the Q&A model

Once the model is successfully created, you can query the model with the `SELECT` syntax.

```sql
SELECT question,answer
FROM mindsdb.global_investment_qa
WHERE question = 'Why should investors invest into Detroit?';
```

On execution, your screen looks like this:


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

The model answered the question with the below:

`Investing in Detroit can be a great opportunity for investors due to the city's low cost of living, growing job market, low business taxes, and vibrant cultural scene. Detroit's economy is experiencing a resurgence, with new businesses, restaurants, and housing developments popping up across the city. Additionally, with the rise of the automotive industry, Detroit is a great place for investors to get in early and capitalize on the city's growth.`

You can also query batch predictions using the `JOIN` clause.

```sql
SELECT a.question,b.answer
FROM files.global_investment as a
JOIN mindsdb.global_investment_qa as b
LIMIT 4;
```

On execution, this looks like:


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

The model provides answers to the questions prompted.

### How does this benefit real estate investors?

Savvy real estate investors might use this model to help them decide if a property investment company is suitable. Using AI minimizes the amount of time spent doing research on the company and asking questions manually.

On the flip side, property investment companies could have such a Q&A model embedded into the front-end design of their websites, so that investors could easily query the page and see if the company passes muster.

Using the power of MindsDB and LlamaIndex, the investor's experience is smooth. An investor can retrieve information seamlessly from any website they like and make an informed decision.

### AI Workflows with MindsDB

Let's say you're running a property investment website and want to answer potential queries quickly. But how do the questions get to you?

One solution is to build an AI workflow that sends investor questions from the Q&A model as Slack notifications to your own company Slack channel. You can do this using MindsDB's application integration with Slack.

### Connecting Slack to MindsDB

Connecting Slack to MindsDB can be done in the follwing easy steps:

\- Connecting an existing Slack Application to MindsDB OR

\- Create a new Slack application to connect to MindsDB

Slack-SDK is used to implement this handler, which is a Python Library offering a package for each of Slack’s APIs. As we are sending notifications to Slack, the Slack app token for authentication will be used to set up a Slack application to receive notifications.

Once you have created a Slack app or have an existing one, you can go ahead and connect the Slack application to MindsDB. You can follow [this guide](https://docs.mindsdb.com/integrations/app-integrations/slack#method-2) to see how to set up a Slack app and a Bot User OAuth Token.

The `CREATE DATABASE` syntax would be used to establish a connection between Slack and MindsDB using the Bot User OAuth Token.

```sql
CREATE DATABASE slack_app
WITH
   ENGINE = 'slack',
   PARAMETERS = {
      "token": "xoxb-your_token"
   };
```

On execution, you get:

```sql
Query successfully completed
```

### Setting up an AI Workflow

As you build your AI Workflow, the questions that flow into the Q&A model will need to be saved in a data source. This way, when notifications are sent to the Slack channel, both the question and answer are included.

For the sake of this tutorial, we will continue to use Files as the data source, however when embedding the model, you will make use of a model with the below syntax:

```sql
CREATE MODEL global_investment_qa
FROM database
   (SELECT * FROM schema.table)
PREDICT answer
USING
   engine = 'llama_index_engine',
   index_class = 'GPTVectorStoreIndex',
   reader = 'DFReader',
   source_url_link = 'https://globalinvestsinc.com/detroit-investment-guide/',
   input_column = 'question',
   openai_api_key = 'your_openai_api_key';
```

Let’s continue, using Files as a data source.

Here is how to send messages to a Slack channel:

```sql
INSERT INTO slack_app.channels (channel, text)
VALUES("llama_test", "Here are questions from potential property investors");
```

The notification will be sent to your Slack channel:


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

Now let’s set up an AI Workflow by creating a job. The `CREATE JOB` syntax will be used.

```sql
CREATE JOB property_investor_questions (

    RETRAIN global_investment_qa
    FROM files (SELECT * FROM global_investment)
    USING
    join_learn_process = true;

    INSERT INTO slack_app.channels (channel, message)
    VALUES("llama_test", "Here are questions from potential investors");
    
    INSERT INTO slack_app.channels(channel, text)
        SELECT "llama_test" AS channel,
        concat('question: ', a.question, chr(10), 'answer: ', b.answer) as text
        FROM mindsdb.global_investment_qa as b
        JOIN files.global_investment as a
        LIMIT 3;

)
EVERY 5 minutes;
```

On execution, you get:

```sql
Query successfully completed
```

Your Slack channel will get this notification:


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

The job was successfully created and Slack notification sent.

With MindsDB, you can leverage the power of Large Language Models and AI workflows to guide your own investment choices or improve your webpage’s information and services for potential investors.

***Explore MindsDB further at*** [***What is MindsDB***](https://docs.mindsdb.com/what-is-mindsdb)***. Or, check us out on*** [***GitHub***](https://github.com/mindsdb/mindsdb)***. (Love what we’re doing? Give us a high five and star our repo.)***

***Questions about this tutorial or others? Join our*** [***Slack community***](https://mindsdb.com/joincommunity)***.***