# Build Chatbots with MongoDB and Cohere

> This page describes how to build a chatbot that provides actionable insights on technology company market reports.

<CookbookHeader href="https://github.com/cohere-ai/cohere-developer-experience/blob/main/notebooks/guides/rag_chatbot_with_cohere_and_mongodb.ipynb" />

**What you will learn:**

* How to empower leverage semantic search on customer or operational data in MongoDB Atlas.
* Pass retrieved data to Cohere’s Command R+ generative model for retrieval-augmented generation (RAG).
* Develop and deploy a RAG-optimized user interface for your app.
* Create a conversation data store for your RAG chatbot using MongoDB

**Use Case**: Develop an advanced chatbot assistant that provides asset managers with information and actionable insights on technology company market reports.

## Introduction

* What is Cohere?
* What is MongoDB?
* How Cohere and MongoDB work together?

### What is Cohere?

![](https://files.buildwithfern.com/cohere.docs.buildwithfern.com/4306e31c733aa1fcad3442bcfbc6572876502ed6973f3bf4bce90428d7c2e910/assets/images/rag-cohere-mongodb-1.png)

### What is MongoDB?

![](https://files.buildwithfern.com/cohere.docs.buildwithfern.com/d8636cc5150ec52d5b18a914c3d100856c74ea2fb59f225840b4a2f666c57d83/assets/images/rag-cohere-mongodb-2.png)

### What exactly are we showing today?

![](https://files.buildwithfern.com/cohere.docs.buildwithfern.com/673005bd1923e2a6514a7a7858df85e7142637e043c828075e8035a84e4ef647/assets/images/rag-cohere-mongodb-3.png)

## Step 1: Install libaries and Set Environment Variables

Critical Security Reminder: Safeguard your production environment by never committing sensitive information, such as environment variable values, to public repositories. This practice is essential for maintaining the security and integrity of your systems.

Libraries:

* `cohere`: A Python library for accessing Cohere's large language models, enabling natural language processing tasks like text generation, classification, and embedding.
* `pymongo`: The recommended Python driver for MongoDB, allowing Python applications to interact with MongoDB databases for data storage and retrieval.
* `datasets`: A library by Hugging Face that provides easy access to a wide range of datasets for machine learning and natural language processing tasks.
  \*`tqdm`: A fast, extensible progress bar library for Python, useful for displaying progress in long-running operations or loops.

```sh
pip install --quiet datasets tqdm cohere pymongo
```

```python
import os
import cohere

os.environ["COHERE_API_KEY"] = ""
co = cohere.Client(os.environ.get("COHERE_API_KEY"))

os.environ["HF_TOKEN"] = ""
```

## Step 2: Data Loading and Preparation

**Dataset Information**

This dataset contains detailed information about multiple technology companies in the Information Technology sector. For each company, the dataset includes:

1. Company name and stock ticker symbol
2. Market analysis reports for recent years (typically 2023 and 2024), which include:

* Title and author of the report
* Date of publication
* Detailed content covering financial performance, product innovations, market position, challenges, and future outlook
* Stock recommendations and price targets

3. Key financial metrics such as:

* Current stock price
* 52-week price range
* Market capitalization
* Price-to-earnings (P/E) ratio
* Dividend yield

4. Recent news items, typically including:

* Date of the news
* Headline
* Brief summary

The market analysis reports provide in-depth information about each company's performance, innovations, challenges, and future prospects. They offer insights into the companies' strategies, market positions, and potential for growth.

```python
import pandas as pd
from datasets import load_dataset