Embed langbase.embed()
Embed, an AI Primitive by Langbase, allows you to convert text into vector embeddings. This is particularly useful for semantic search, text similarity comparisons, and other NLP tasks.
Embedding text into vectors enables you to perform complex queries and analyses that go beyond simple keyword matching. It allows you to capture the semantic meaning of the text, making it easier to find relevant information based on context rather than just keywords.
You can use the embed()
function to generate vector embeddings for text chunks. This is particularly useful for semantic search, text similarity comparisons, and other NLP tasks.
Generate a User/Org API key
You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.
API reference
langbase.embed(options)
Generate embeddings by running the langbase.embed()
function.
Function Signature
langbase.embed(options);
// with types
langbase.embed(options: EmbedOptions);
options
- Name
options
- Type
- EmbedOptions
- Description
EmbedOptions Object
chunks: string[]; embeddingModel?: EmbeddingModels;
Following are the properties of the options object.
chunks
- Name
chunks
- Type
- string[]
- Required
- Required
- Description
An array of text chunks to generate embeddings for.
embeddingModel
- Name
embeddingModel
- Type
- EmbeddingModels
- Description
The embedding model to use. Available options:
openai:text-embedding-3-large
cohere:embed-v4.0
cohere:embed-multilingual-v3.0
cohere:embed-multilingual-light-v3.0
google:text-embedding-004
EmbeddingModels Type
type EmbeddingModels = "openai:text-embedding-3-large" | "cohere:embed-v4.0" | "cohere:embed-multilingual-v3.0" | "cohere:embed-multilingual-light-v3.0" | "google:text-embedding-004";
Usage example
Install the SDK
npm i langbase
Environment variables
.env file
LANGBASE_API_KEY="<USER/ORG-API-KEY>"
langbase.embed()
examples
langbase.embed()
import { Langbase } from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
const embeddings = await langbase.embed({
chunks: [
"The quick brown fox",
"jumps over the lazy dog"
]
});
console.log('Embeddings:', embeddings);
}
main();
Response
Response of langbase.embed()
is a Promise<number[][]>
.
EmbedResponse Type
type EmbedResponse = number[][];
- Name
response
- Type
- number[][]
- Description
A 2D array where each inner array represents the embedding vector for the corresponding input chunk.
EmbedResponse Example
[
[-0.023, 0.128, -0.194, ...],
[0.067, -0.022, 0.289, ...],
]