Create a ChatBot with VertexAI and LibreChat
Tags: vertexai, librechat, generativeai, palm2
Published: 2023-11-02 16:15:17

In this short article, let's discover how to seamlessly integrate Google's VertexAI with the open source platform LibreChat to craft a state-of-the-art chatbot.

## Google VertexAI Setup

![VertexAI Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hnb48mliklkowek3a9w.png)

[VertexAI](https://cloud.google.com/vertex-ai/) is a machine learning platform available on Google Cloud. It offers a variety of services to train and deploy AI models, including those for Generative AI.

To get started:

- Navigate to the [Google Cloud Console](https://console.cloud.google.com/).
- Create a new project or connect to an existing one.
- Enable the VertexAI API.
- Create a Service Account and generate a key for it. Ensure you download and securely store the associated JSON Key file, as we'll need it later.
- Install and configure the [gcloud CLI](https://cloud.google.com/sdk/docs/install) on your machine. It's a useful tool to have on hand.

![VertexAI Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq3yd2duvvjcyhz0ausg.png)

## PaLM 2 Models

PaLM 2 offers a set of foundational pre-trained models designed for generative AI tasks, such as completion, chat, and embedding, across text, code, and images. These models are available in various sizes, ranging from "gecko" (small) to "bison" (large). For our purpose, we'll use the [PaLM 2 for Chat (chat-bison)](https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text) model.

To test our access to this model, we'll make a simple VertexAI API call. Generate the required authentication token using the `gcloud auth print-access-token` command from the gcloud CLI. Remember to have your project ID and Google Cloud location (e.g., *europe-west1-b*) on hand to construct the URL.

Here's how you can use Node.js to make the API call:

```javascript
fetch('https://us-central1-aiplatform.googleapis.com/v1/projects/<project-id>/locations/europe-west1-b/publishers/google/models/text-bison@001:predict', {
  method: 'POST',
  headers: { "Authorization": "Bearer <token>" },
  body: JSON.stringify({
      "instances": [
        { "prompt": "Say hello in Turkish"}
      ],
      "parameters": {
        "temperature": 0.2,
        "maxOutputTokens": 256,
        "topK": 40,
        "topP": 0.95
      }
    })
})
  .then((response) => response.json())
  .then((json) => console.log(json.predictions[0].content));
```

The expected output in the console is:
```
Merhaba.
```

Great! This confirms that we can manually invoke the VertexAI API and the chat-bison model.

## LibreChat Installation and Configuration

With the VertexAI endpoint set up and tested, our next step is to work with [LibreChat](https://docs.librechat.ai/). LibreChat is an open-source ChatGPT clone that can integrate with various AI models, including the PaLM 2 models via the VertexAI API. It's built using React, MongoDB, and [Meilisearch](https://www.meilisearch.com/) technologies.

![LibreChat Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d8lharlh3b8ls3mle8s.png)

Follow these steps to get LibreChat up and running:

- Clone the LibreChat repository:
```
git clone https://github.com/danny-avila/LibreChat.git
```

- Create the necessary directories:
```
cd LibreChat
mkdir meili_data images .env.production .env.development data-node
```

- Edit the configuration file:
```
cp .env.example .env
vi .env
```

In the `.env` file, leave the following parameters blank to solely activate the VertexAI endpoint:
```
OPENAI_API_KEY=
CHATGPT_TOKEN=
BINGAI_TOKEN=
ANTHROPIC_API_KEY=
```

- Create and start the Docker containers:
```
docker-compose up
```

![Docker Compose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vwpe4pufzuqrpk10sux.png)

- Once the Docker images are pulled and containers are running, navigate to LibreChat's local URL using your browser: http://localhost:3080. Create a new account.

- Complete the LibreChat configuration by clicking on the Palm icon within the chat bar, followed by the *Set API key* link.

![PaLM configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4m5rvjt7169oe5p5d6ln.png)

- Import the Service Account JSON Key that you saved during the VertexAI configuration.

![Service Account JSON Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yl8493aj4e5ld0w6dfns.png)

## And... Voilà!

You're all set! Start a chat session using VertexAI.

![Chat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef07d707t7wp1035elk0.png)

That wraps up our tutorial. While we focused on the VertexAI endpoint, remember that LibreChat is versatile—it supports endpoints from OpenAI, Azure OpenAI, BingAI, and Anthropic's models. Additionally, LibreChat can accommodate GPT-compatible plugins and even allows you to [create your own](https://github.com/danny-avila/LibreChat/blob/main/docs/features/plugins/make_your_own.md)! But that's a topic for another day. Stay tuned!