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:

create table todos (
    id uuid DEFAULT (gen_random_uuid()),
    tenant_id uuid,
    title varchar(256),
    embedding vector(1024),
    complete boolean);

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:

python -m pip install psycopg2-binary pgvector

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:

python -m pip install cohere

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:

import cohere
import psycopg2
from pgvector.psycopg2 import register_vector

cohere = cohere.Client('your-api-key')
model = "embed-english-v3.0" # Replace with your favorite Cohere model

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:

conn = psycopg2.connect('postgresql://user:password@us-west-2.db.thenile.dev:5432/mydb')
conn.set_session(autocommit=True)
cur = conn.cursor()
register_vector(cur)
cur.execute("insert into tenants (name) values ('first tenant') returning id;")
tenant_id = cur.fetchone()[0]

Now let's use Cohere to generate embeddings for a few todo items and insert them into Nile:

todo_items = [
    "Center a div",
    "Implement RAG-based HR chatbot",
    "Add OKTA authentication to the app",
    "Write a blog post about RAG with Cohere and Nile",
    "Optimize a slow database query",
]

response = cohere.embed(model=model, texts=todo_items, input_type="search_document").embeddings

for item, embedding in zip(todo_items, response):
    cur.execute("INSERT INTO todos (tenant_id, title, embedding) VALUES (%s, %s, %s)", (tenant_id, item, embedding))

And finally, lets find the todo items that are most similar to a given question from an impatient project manager:

question = "Is there any work left on authentication?"
question_embedding = cohere.embed(model=model, texts=[question], input_type="search_query").embeddings[0]

cur.execute("set nile.tenant_id = %s", (tenant_id,))
cur.execute("SELECT title FROM todos ORDER BY embedding <#> %s::vector LIMIT 1", (question_embedding,))
print(cur.fetchone())

Run the script with the following command:

python cohere_nile_quickstart.py

And if everything went well, you should see the following output:

('Add OKTA authentication to the app')

Seems like a good answer to the question!

Full application

Coming soon!