Cohere
Cohere is an Enterprise AI platform that provides developers with world-class models, along with the supporting platform required to deploy them securely and privately.
Using Cohere and Nile together
Cohere’s chat models, embedding models and re-ranker can be used with Nile to build B2B applications using RAG (Retrieval Augmented Generation) architectures. Cohere is a sophisticated platform, and there are several ways you can use it with Nile to build RAG architectures. We will demonstrate a simple example of using Cohere’s embedding model with Nile, but the same principles can be applied to the other Cohere models.
We’ll walk you through the setup steps and then explain the code line by line. The entire script is available here.
To get started, you’ll need a Cohere account and a Nile database. You can sign up for Cohere here and for Nile here.
Setting Up Nile
Once you’ve signed up for Nile, you’ll be promoted to create your first database. Go ahead and do so. You’ll be redirected to the “Query Editor” page of your new database. This is a good time to create the table we’ll be using in this example:
Once you’ve created the table, you’ll see it on the left-hand side of the screen. You’ll also see the tenants
table that is built-in to Nile.
Next, you’ll want to pick up your database connection string: Navigate to the “Settings” page, select “Connections” and click “Generate credentials”. Copy the connection string and keep it in a secure location.
To use Nile in your application, you’ll also need to install Psycopg2, a Python library for interacting with Postgres. And since we’ll be using vector embeddings, it helps to have pgvector’s Python client installed as well. You can install it with the following command:
Setting Up Cohere
After you’ve signed up for Cohere, select the “API Keys” link on the left-hand side of the screen. Click the “Create API Key” button, copy the key and keep it in a secure location.
You can start with a more limited trial key, and upgrade to production key once you’re ready to scale up.
Next, you’ll want to install Cohere’s Python SDK. You can do this with the following command:
Quickstart
We’ll start with a simple example that where we use Cohere’s embedding model to generate embeddings for a few todo items. And then we’ll use Nile’s RAG capabilities to retrieve todo items related to a given query.
First, create a new file called cohere_nile_quickstart.py
.
We’ll start by setting up the Cohere client:
Next, we’ll set up the connection to the Nile database, register the pgvector client with the curson, and create a tenant who will own the todo items:
Now let’s use Cohere to generate embeddings for a few todo items and insert them into Nile:
And finally, lets find the todo items that are most similar to a given question from an impatient project manager:
Run the script with the following command:
And if everything went well, you should see the following output:
Seems like a good answer to the question!
Full application
Coming soon!