/ docs-website / versioned_docs / version-2.24 / pipeline-components / embedders / watsonxtextembedder.mdx
watsonxtextembedder.mdx
1 --- 2 title: "WatsonxTextEmbedder" 3 id: watsonxtextembedder 4 slug: "/watsonxtextembedder" 5 description: "When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents." 6 --- 7 8 # WatsonxTextEmbedder 9 10 When you perform embedding retrieval, you use this component to transform your query into a vector. Then, the embedding Retriever looks for similar or relevant documents. 11 12 <div className="key-value-table"> 13 14 | | | 15 | --- | --- | 16 | **Most common position in a pipeline** | Before an embedding [Retriever](../retrievers.mdx) in a query/RAG pipeline | 17 | **Mandatory init variables** | `api_key`: An IBM Cloud API key. Can be set with `WATSONX_API_KEY` env var. <br /> <br />`project_id`: An IBM Cloud project ID. Can be set with `WATSONX_PROJECT_ID` env var. | 18 | **Mandatory run variables** | `text`: A string | 19 | **Output variables** | `embedding`: A list of float numbers <br /> <br />`meta`: A dictionary of metadata | 20 | **API reference** | [Watsonx](/reference/integrations-watsonx) | 21 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/watsonx | 22 23 </div> 24 25 ## Overview 26 27 To see the list of compatible IBM watsonx.ai embedding models, head over to IBM [documentation](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models-embed.html?context=wx). The default model for `WatsonxTextEmbedder` is `ibm/slate-30m-english-rtrvr`. You can specify another model with the `model` parameter when initializing this component. 28 29 Use `WatsonxTextEmbedder` to embed a simple string (such as a query) into a vector. For embedding lists of documents, use the [`WatsonxDocumentEmbedder`](watsonxdocumentembedder.mdx), which enriches the document with the computed embedding, also known as vector. 30 31 The component uses `WATSONX_API_KEY` and `WATSONX_PROJECT_ID` environment variables by default. Otherwise, you can pass API credentials at initialization with `api_key` and `project_id`: 32 33 ```python 34 embedder = WatsonxTextEmbedder( 35 api_key=Secret.from_token("<your-api-key>"), 36 project_id=Secret.from_token("<your-project-id>"), 37 ) 38 ``` 39 40 ## Usage 41 42 Install the `watsonx-haystack` package to use the `WatsonxTextEmbedder`: 43 44 ```shell 45 pip install watsonx-haystack 46 ``` 47 48 ### On its own 49 50 Here is how you can use the component on its own: 51 52 ```python 53 from haystack_integrations.components.embedders.watsonx.text_embedder import ( 54 WatsonxTextEmbedder, 55 ) 56 from haystack.utils import Secret 57 58 text_to_embed = "I love pizza!" 59 60 text_embedder = WatsonxTextEmbedder( 61 api_key=Secret.from_env_var("WATSONX_API_KEY"), 62 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 63 model="ibm/slate-30m-english-rtrvr", 64 ) 65 66 print(text_embedder.run(text_to_embed)) 67 68 ## {'embedding': [0.017020374536514282, -0.023255806416273117, ...], 69 ## 'meta': {'model': 'ibm/slate-30m-english-rtrvr', 70 ## 'truncated_input_tokens': 3}} 71 ``` 72 73 :::info 74 We recommend setting WATSONX_API_KEY and WATSONX_PROJECT_ID as environment variables instead of setting them as parameters. 75 ::: 76 77 ### In a pipeline 78 79 ```python 80 from haystack import Document 81 from haystack import Pipeline 82 from haystack.document_stores.in_memory import InMemoryDocumentStore 83 from haystack_integrations.components.embedders.watsonx.text_embedder import ( 84 WatsonxTextEmbedder, 85 ) 86 from haystack_integrations.components.embedders.watsonx.document_embedder import ( 87 WatsonxDocumentEmbedder, 88 ) 89 from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever 90 91 document_store = InMemoryDocumentStore(embedding_similarity_function="cosine") 92 93 documents = [ 94 Document(content="My name is Wolfgang and I live in Berlin"), 95 Document(content="I saw a black horse running"), 96 Document(content="Germany has many big cities"), 97 ] 98 99 document_embedder = WatsonxDocumentEmbedder() 100 documents_with_embeddings = document_embedder.run(documents)["documents"] 101 document_store.write_documents(documents_with_embeddings) 102 103 query_pipeline = Pipeline() 104 query_pipeline.add_component("text_embedder", WatsonxTextEmbedder()) 105 query_pipeline.add_component( 106 "retriever", 107 InMemoryEmbeddingRetriever(document_store=document_store), 108 ) 109 query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding") 110 111 query = "Who lives in Berlin?" 112 113 result = query_pipeline.run({"text_embedder": {"text": query}}) 114 115 print(result["retriever"]["documents"][0]) 116 117 ## Document(id=..., mimetype: 'text/plain', 118 ## text: 'My name is Wolfgang and I live in Berlin') 119 ```