MixedbreadAI
Welcome to the mixedbread embeddings guide! This guide will help you use the mixedbread aiâs API to generate embeddings for your text documents, ensuring you get the most relevant information, just like picking the freshest bread from the bakery.
To find out more about the latest features, updates, and available models, visit mixedbread.ai.
Table of Contents
Section titled âTable of Contentsânpm i llamaindex @llamaindex/mixedbread
Next, sign up for an API key at mixedbread.ai. Once you have your API key, you can import the necessary modules and create a new instance of the MixedbreadAIEmbeddings
class.
import { MixedbreadAIEmbeddings } from "@llamaindex/mixedbread";import { Document, Settings } from "llamaindex";
Usage with LlamaIndex
Section titled âUsage with LlamaIndexâThis section will guide you through integrating mixedbread embeddings with LlamaIndex for more advanced usage.
Step 1: Load and Index Documents
Section titled âStep 1: Load and Index DocumentsâFor this example, we will use a single document. In a real-world scenario, you would have multiple documents to index, like a variety of breads in a bakery.
Settings.embedModel = new MixedbreadAIEmbeddings({ apiKey: "<MIXEDBREAD_API_KEY>", model: "mixedbread-ai/mxbai-embed-large-v1",});
const document = new Document({ text: "The true source of happiness.", id_: "bread",});
const index = await VectorStoreIndex.fromDocuments([document]);
Step 2: Create a Query Engine
Section titled âStep 2: Create a Query EngineâCombine the retriever and the embed model to create a query engine. This setup ensures that your queries are processed to provide the best results, like arranging the bread in the order of freshness and quality.
Models can require prompts to generate embeddings for queries, in the âmixedbread-ai/mxbai-embed-large-v1â modelâs case, the prompt is Represent this sentence for searching relevant passages:
.
const queryEngine = index.asQueryEngine();
const query = "Represent this sentence for searching relevant passages: What is bread?";
// Log the responseconst results = await queryEngine.query(query);console.log(results); // Serving up the freshest, most relevant results.
Embeddings with Custom Parameters
Section titled âEmbeddings with Custom ParametersâThis section will guide you through generating embeddings with custom parameters and usage with f.e. matryoshka and binary embeddings.
Step 1: Create an Instance of MixedbreadAIEmbeddings
Section titled âStep 1: Create an Instance of MixedbreadAIEmbeddingsâCreate a new instance of the MixedbreadAIEmbeddings
class with custom parameters. For example, to use the mixedbread-ai/mxbai-embed-large-v1
model with a batch size of 64, normalized embeddings, and binary encoding format:
const embeddings = new MixedbreadAIEmbeddings({ apiKey: "<MIXEDBREAD_API_KEY>", model: "mixedbread-ai/mxbai-embed-large-v1", batchSize: 64, normalized: true, dimensions: 512, encodingFormat: MixedbreadAI.EncodingFormat.Binary,});
Step 2: Define Texts
Section titled âStep 2: Define TextsâDefine the texts you want to generate embeddings for.
const texts = ["Bread is life", "Bread is love"];
Step 3: Generate Embeddings
Section titled âStep 3: Generate EmbeddingsâUse the embedDocuments
method to generate embeddings for the texts.
const result = await embeddings.embedDocuments(texts);console.log(result); // Perfectly customized embeddings, ready to serve.