Build Multilingual AI Memory using Cohere Embedding Models
Langbase AI memory now supports Cohere embeddings. This guide will highlight their performance, cost, and use cases, and walk you through the process of creating and using Cohere embeddings in Langbase Memory.
Memory allows you to store, organize, and retrieve information. It can be used to build powerful Retrieval Augmented Generation (RAG) based AI agents. These agents can assist your queries using your own data, leading to more accurate and relevant responses.
Embeddings are a crucial part of Memory. They are mathematical representations of text as vectors in a multi-dimensional space. They capture the semantic meaning of the input, making it easier for LLMs to understand and process the information.
For example, the phrases "I like cats" and "I enjoy felines" will have similar embeddings because they mean roughly the same thing.
In a RAG, high quality embeddings help the model retrieve the most-relevant information from the memory to generate accurate responses. Good embedding qualities include multilingual support, semantic understanding, and performance.
Cohere Embeddings
At Langbase, we are introducing support for embedding models from various providers, starting from Cohere. Currently, the following embedding models are supported:
embed-multilingual-v3.0By Cohereembed-multilingual-light-v3.0By Coheretext-embedding-3-largeBy OpenAI
Cohere embed has emerged as an impressive OpenAI alternative, and here are two reasons that stand out:
-
Multilingual Advantage: Cohere's
embedexcel at multilingual applications showing better performance across different languages in benchmarks compared to OpenAI. -
Cost: Cohere can be more cost-effective than OpenAI, especially for high volume data due to their flat rates. Cohere charges $1 per 1000 embeddings, while OpenAI charges on a per-token basis.
It's bananas easy to try it out. Let's take a look at how we can build a AI memory using Cohere's embeddings at Langbase.
Step #0
We will be building an AI memory agent using Langbase. So please go ahead and create an account on Langbase.
Step #1
Now let's set up a Node project. To do it, run the following command in your project terminal:
npm init -y
This will create a package.json file with basic information. We will use the Langbase SDK and dotenv package to read environment variables in our code. Run the command below.
npm install langbase dotenv
Lastly, let's create an index.js file in our project directory. This will contain all the necessary code we will write to build an AI memory agent.
Step #2
The next step is to generate a user API key which you can do here. We will use this key to authenticate our Langbase API requests.
Now go ahead and create a .env file in your project directory and add your API key there.
LANGBASE_API_KEY=<REPLACE_WITH_LANGBASE_API_KEY>
Step #3
Now we will create an AI memory agent on Langbase using Langbase SDK. Using the docs, we will write a function to create a new memory in the index.js file.
import 'dotenv/config';
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function createNewMemory() {
const response = await langbase.memory.create({
name: 'multilingual-knowledge-base',
description: 'Advanced memory with multilingual support using Cohere',
embedding_model: 'cohere:embed-multilingual-v3.0'
});
const newMemory = await response.json();
return newMemory;
}
This function will create a multilingual-knowledge-base memory on Langbase. The embedding_model parameter specifies the embedding model you want to use for the memory. In this case, we are using Cohere's embed-multilingual-v3.0.
Step #4
Now when you upload docs in multilingual-knowledge-base memory, Langbase will use Cohere embed-multilingual-v3.0 to embed them.
Let's say you have a markdown file product-faqs.md with product FAQs in different languages like this:
1. What is the battery life of the smartphone? (English)
The smartphone has a battery life of up to 24 hours on a single charge.
2. ¿Cuál es la duración de la batería del teléfono inteligente? (Spanish)
El teléfono inteligente tiene una duración de batería de hasta 24 horas con una sola carga.
3. Quelle est l'autonomie de la batterie du smartphone ? (French)
Le smartphone a une autonomie de batterie allant jusqu'à 24 heures avec une seule charge.
4. Wie lange hält der Akku des Smartphones? (German)
Das Smartphone hat eine Akkulaufzeit von bis zu 24 Stunden mit einer einzigen Ladung.
5. Qual è la durata della batteria dello smartphone? (Italian)
Lo smartphone ha una durata della batteria di fino a 24 ore con una sola carica.
---
6. Does the smartphone support wireless charging? (English)
Yes, the smartphone supports fast wireless charging.
7. ¿El teléfono inteligente admite carga inalámbrica? (Spanish)
Sí, el teléfono inteligente admite carga inalámbrica rápida.
8. Le smartphone prend-il en charge la charge sans fil ? (French)
Oui, le smartphone prend en charge la charge sans fil rapide.
9. Unterstützt das Smartphone kabelloses Laden? (German)
Ja, das Smartphone unterstützt schnelles kabelloses Laden.
10. Lo smartphone supporta la ricarica wireless? (Italian)
Sì, lo smartphone supporta la ricarica wireless veloce.
---
11. What storage options are available for the smartphone? (English)
The smartphone is available in 64GB, 128GB, and 256GB storage options.
12. ¿Qué opciones de almacenamiento están disponibles para el teléfono inteligente? (Spanish)
El teléfono inteligente está disponible en opciones de almacenamiento de 64GB, 128GB y 256GB.
13. Quelles options de stockage sont disponibles pour le smartphone ? (French)
Le smartphone est disponible en options de stockage de 64 Go, 128 Go et 256 Go.
14. Welche Speicheroptionen sind für das Smartphone verfügbar? (German)
Das Smartphone ist in den Speicheroptionen 64 GB, 128 GB und 256 GB erhältlich.
15. Quali opzioni di archiviazione sono disponibili per lo smartphone? (Italian)
Lo smartphone è disponibile nelle opzioni di archiviazione da 64 GB, 128 GB e 256 GB.
---
16. Is there a warranty for the smartphone? (English)
Yes, the smartphone comes with a one-year warranty.
17. ¿Hay garantía para el teléfono inteligente? (Spanish)
Sí, el teléfono inteligente viene con un año de garantía.
18. Y a-t-il une garantie pour le smartphone ? (French)
Oui, le smartphone est livré avec une garantie d'un an.
19. Gibt es eine Garantie für das Smartphone? (German)
Ja, das Smartphone hat eine einjährige Garantie.
20. C'è una garanzia per lo smartphone? (Italian)
Sì, lo smartphone viene fornito con una garanzia di un anno.
You can upload this file to the memory using the Langbase SDK:
import { readFileSync } from 'fs';
async function uploadDocument(filePath) {
return await langbase.memory.documents.upload({
memoryName: 'multilingual-knowledge-base',
contentType: 'text/markdown',
documentName: 'faqs.md',
document: readFileSync(filePath),
meta: {
url: 'https://example.com/faqs.md'
}
});
}
This function will upload the product-faqs.md file from your project folder (using the given file path) to the multilingual-knowledge-base memory.
Once uploaded, Langbase will use Cohere's embed-multilingual-v3.0 to process it. You can view the file's status in the memory tab of Langbase Studio. Once ready, you can test the memory by asking questions in different languages to verify if it provides relevant answers.
Step #5
We will use the Memory Retrieve function, to get the relevant chunks from the memory.
const englishQuery = `Is there a warranty for the smartphone?`;
const spanishQuery = `¿Hay garantía para el teléfono inteligente?`;
async function retrieveSimilarChunks(query) {
const response= await langbase.memory.retrieve({
query: spanishQuery,
memory: [{ name: "multilingual-knowledge-base" }]
});
const similarChunks = await response.json();
return similarChunks;
}
Here is what the final code will look like:
import 'dotenv/config';
import { readFileSync } from 'fs';
import { Langbase } from 'langbase';
const path = require('path');
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY
});
async function createNewMemory() {
const response = await langbase.memory.create({
name: 'multilingual-knowledge-base',
description: 'Advanced memory with multilingual support using Cohere',
embedding_model: 'cohere:embed-multilingual-v3.0'
});
const newMemory = await response.json();
return newMemory;
}
async function uploadDocument(filePath) {
return await langbase.memory.documents.upload({
memoryName: 'multilingual-knowledge-base',
contentType: 'text/markdown',
documentName: 'faqs.md',
document: readFileSync(filePath),
meta: {
url: 'https://example.com/faqs.md'
}
});
}
async function retrieveSimilarChunks(query) {
const response = await langbase.memory.retrieve({
query: query,
memory: [{ name: 'multilingual-knowledge-base' }]
});
const similarChunks = await response.json();
return similarChunks;
}
(async function () {
const newMemory = await createNewMemory();
// Update file path to use your local file
const filePath = path.join(__dirname, 'product-faqs.md');
await uploadDocument(signedUrl, 'product-faqs.md');
// Wait for the uploaded document to be processed, then retrieve similar chunks
const englishQuery = `What kind of warranty do I get after purchasing the phone?`;
const spanishQuery = `¿Qué tipo de garantía tengo después de comprar el teléfono?`;
const englishQueryResult = await retrieveSimilarChunks(englishQuery);
console.log(JSON.stringify(englishQueryResult, null, 2));
const spanishQueryResult = await retrieveSimilarChunks(spanishQuery);
console.log(JSON.stringify(spanishQueryResult, null, 2));
})();
Step #6
Lastly, we will run our index.js file. It will create a memory, upload markdown documents inside it along with their metadata and then finally retrieve chunks from the memory for the following indirect user queries:
What kind of warranty do I get after purchasing the phone?(English)¿Qué tipo de garantía tengo después de comprar el teléfono?(Spanish)
node index.js
It should show an output close to this for the english and spanish queries:
// English query response
[
{
"text": "Yes, the smartphone comes with a one-year warranty.",
"similarity": 0.99,
"metadata": {
"url": "https://example.com/faqs"
},
}
]
// Spanish query response
[
{
"text": "Sí, el teléfono inteligente viene con un año de garantía.",
"similarity": 0.99,
"metadata": {
"url": "https://example.com/faqs"
},
}
]
You can see that the memory agent is able to retrieve the relevant information for both the English and Spanish queries. Feel free to play around with different queries.
In single digit minutes, your multilingual knowledge base memory is ready to assist users in multiple languages. That's all from this guide.
Dive deeper into Langbase's Memory API to explore more features and functionalities. You can also checkout the guide on building multi-agent AI support here.