/ docs-website / versioned_docs / version-2.18 / pipeline-components / embedders / fastembedtextembedder.mdx
fastembedtextembedder.mdx
1 --- 2 title: "FastembedTextEmbedder" 3 id: fastembedtextembedder 4 slug: "/fastembedtextembedder" 5 description: "This component computes the embeddings of a string using embedding models supported by FastEmbed." 6 --- 7 8 # FastembedTextEmbedder 9 10 This component computes the embeddings of a string using embedding models supported by FastEmbed. 11 12 | | | 13 | :------------------------------------- | :------------------------------------------------------------------------------------------ | 14 | **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline | 15 | **Mandatory run variables** | “text”: A string | 16 | **Output variables** | “embedding”: A vector (list of float numbers) | 17 | **API reference** | [FastEmbed](/reference/fastembed-embedders) | 18 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/fastembed | 19 20 This component should be used to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`FastembedDocumentEmbedder`](fastembeddocumentembedder.mdx), which enriches the document with the computed embedding, known as vector. 21 22 ## Overview 23 24 `FastembedTextEmbedder` transforms a string into a vector that captures its semantics using embedding [models supported by FastEmbed](https://qdrant.github.io/fastembed/examples/Supported_Models/). 25 26 When you perform embedding retrieval, use this component first to transform your query into a vector. Then, the embedding Retriever will use the vector to search for similar or relevant documents. 27 28 ### Compatible models 29 30 You can find the original models in the [FastEmbed documentation](https://qdrant.github.io/fastembed/). 31 32 Currently, most of the models in the [Massive Text Embedding Benchmark (MTEB) Leaderboard](https://huggingface.co/spaces/mteb/leaderboard) are compatible with FastEmbed. You can look for compatibility in the [supported model list](https://qdrant.github.io/fastembed/examples/Supported_Models/). 33 34 ### Installation 35 36 To start using this integration with Haystack, install the package with: 37 38 ```bash 39 pip install fastembed-haystack 40 ``` 41 42 ### Instructions 43 44 Some recent models that you can find in MTEB require prepending the text with an instruction to work better for retrieval. 45 For example, if you use `[BAAI/bge-large-en-v1.5](https://huggingface.co/BAAI/bge-large-en-v1.5#model-list)` model, you should prefix your query with the `instruction: “passage:”`. 46 47 This is how it works with `FastembedTextEmbedder`: 48 49 ```python 50 instruction = "passage:" 51 embedder = FastembedTextEmbedder( 52 *model="*BAAI/bge-large-en-v1.5", 53 prefix=instruction) 54 ``` 55 56 ### Parameters 57 58 You can set the path where the model will be stored in a cache directory. Also, you can set the number of threads a single `onnxruntime` session can use. 59 60 ```python 61 cache_dir= "/your_cacheDirectory" 62 embedder = FastembedTextEmbedder( 63 *model="*BAAI/bge-large-en-v1.5", 64 cache_dir=cache_dir, 65 threads=2 66 ) 67 ``` 68 69 If you want to use the data parallel encoding, you can set the parameters `parallel` and `batch_size`. 70 71 - If parallel > 1, data-parallel encoding will be used. This is recommended for offline encoding of large datasets. 72 - If parallel is 0, use all available cores. 73 - If None, don't use data-parallel processing; use default `onnxruntime` threading instead. 74 75 :::tip 76 If you create a Text Embedder and a Document Embedder based on the same model, Haystack uses the same resource behind the scenes to save resources. 77 78 ::: 79 80 ## Usage 81 82 ### On its own 83 84 ```python 85 from haystack_integrations.components.embedders.fastembed import FastembedTextEmbedder 86 87 text = """It clearly says online this will work on a Mac OS system. 88 The disk comes and it does not, only Windows. 89 Do Not order this if you have a Mac!!""" 90 text_embedder = FastembedTextEmbedder(model="BAAI/bge-small-en-v1.5") 91 text_embedder.warm_up() 92 embedding = text_embedder.run(text)["embedding"] 93 ``` 94 95 ### In a pipeline 96 97 ```python 98 from haystack import Document, Pipeline 99 from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever 100 from haystack.document_stores.in_memory import InMemoryDocumentStore 101 from haystack_integrations.components.embedders.fastembed import ( 102 FastembedDocumentEmbedder, 103 FastembedTextEmbedder, 104 ) 105 106 document_store = InMemoryDocumentStore(embedding_similarity_function="cosine") 107 108 documents = [ 109 Document(content="My name is Wolfgang and I live in Berlin"), 110 Document(content="I saw a black horse running"), 111 Document(content="Germany has many big cities"), 112 Document(content="fastembed is supported by and maintained by Qdrant."), 113 ] 114 115 document_embedder = FastembedDocumentEmbedder() 116 document_embedder.warm_up() 117 documents_with_embeddings = document_embedder.run(documents)["documents"] 118 document_store.write_documents(documents_with_embeddings) 119 120 query_pipeline = Pipeline() 121 query_pipeline.add_component("text_embedder", FastembedTextEmbedder()) 122 query_pipeline.add_component( 123 "retriever", 124 InMemoryEmbeddingRetriever(document_store=document_store), 125 ) 126 query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") 127 128 query = "Who supports FastEmbed?" 129 130 result = query_pipeline.run({"text_embedder": {"text": query}}) 131 132 print(result["retriever"]["documents"][0]) # noqa: T201 133 134 ## Document(id=..., 135 ## content: 'FastEmbed is supported by and maintained by Qdrant.', 136 ## score: 0.758..) 137 ``` 138 139 ## Additional References 140 141 🧑🍳 Cookbook: [RAG Pipeline Using FastEmbed for Embeddings Generation](https://haystack.deepset.ai/cookbook/rag_fastembed)