|
| 1 | +--- |
| 2 | +title: LangChain |
| 3 | +--- |
| 4 | + |
| 5 | +Mem0 supports LangChain as a provider to access a wide range of embedding models. LangChain is a framework for developing applications powered by language models, making it easy to integrate various embedding providers through a consistent interface. |
| 6 | + |
| 7 | +For a complete list of available embedding models supported by LangChain, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/). |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +<CodeGroup> |
| 12 | +```python Python |
| 13 | +import os |
| 14 | +from mem0 import Memory |
| 15 | +from langchain_openai import OpenAIEmbeddings |
| 16 | + |
| 17 | +# Set necessary environment variables for your chosen LangChain provider |
| 18 | +os.environ["OPENAI_API_KEY"] = "your-api-key" |
| 19 | + |
| 20 | +# Initialize a LangChain embeddings model directly |
| 21 | +openai_embeddings = OpenAIEmbeddings( |
| 22 | + model="text-embedding-3-small", |
| 23 | + dimensions=1536 |
| 24 | +) |
| 25 | + |
| 26 | +# Pass the initialized model to the config |
| 27 | +config = { |
| 28 | + "embedder": { |
| 29 | + "provider": "langchain", |
| 30 | + "config": { |
| 31 | + "model": openai_embeddings |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +m = Memory.from_config(config) |
| 37 | +messages = [ |
| 38 | + {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, |
| 39 | + {"role": "assistant", "content": "How about a thriller movies? They can be quite engaging."}, |
| 40 | + {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, |
| 41 | + {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} |
| 42 | +] |
| 43 | +m.add(messages, user_id="alice", metadata={"category": "movies"}) |
| 44 | +``` |
| 45 | +</CodeGroup> |
| 46 | + |
| 47 | +## Supported LangChain Embedding Providers |
| 48 | + |
| 49 | +LangChain supports a wide range of embedding providers, including: |
| 50 | + |
| 51 | +- OpenAI (`OpenAIEmbeddings`) |
| 52 | +- Cohere (`CohereEmbeddings`) |
| 53 | +- Google (`VertexAIEmbeddings`) |
| 54 | +- Hugging Face (`HuggingFaceEmbeddings`) |
| 55 | +- Sentence Transformers (`HuggingFaceEmbeddings`) |
| 56 | +- Azure OpenAI (`AzureOpenAIEmbeddings`) |
| 57 | +- Ollama (`OllamaEmbeddings`) |
| 58 | +- Together (`TogetherEmbeddings`) |
| 59 | +- And many more |
| 60 | + |
| 61 | +You can use any of these model instances directly in your configuration. For a complete and up-to-date list of available embedding providers, refer to the [LangChain Text Embedding documentation](https://python.langchain.com/docs/integrations/text_embedding/). |
| 62 | + |
| 63 | +## Provider-Specific Configuration |
| 64 | + |
| 65 | +When using LangChain as an embedder provider, you'll need to: |
| 66 | + |
| 67 | +1. Set the appropriate environment variables for your chosen embedding provider |
| 68 | +2. Import and initialize the specific model class you want to use |
| 69 | +3. Pass the initialized model instance to the config |
| 70 | + |
| 71 | +### Examples with Different Providers |
| 72 | + |
| 73 | +#### HuggingFace Embeddings |
| 74 | + |
| 75 | +```python |
| 76 | +from langchain_huggingface import HuggingFaceEmbeddings |
| 77 | + |
| 78 | +# Initialize a HuggingFace embeddings model |
| 79 | +hf_embeddings = HuggingFaceEmbeddings( |
| 80 | + model_name="BAAI/bge-small-en-v1.5", |
| 81 | + encode_kwargs={"normalize_embeddings": True} |
| 82 | +) |
| 83 | + |
| 84 | +config = { |
| 85 | + "embedder": { |
| 86 | + "provider": "langchain", |
| 87 | + "config": { |
| 88 | + "model": hf_embeddings |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | +
|
| 94 | +#### Ollama Embeddings |
| 95 | +
|
| 96 | +```python |
| 97 | +from langchain_ollama import OllamaEmbeddings |
| 98 | +
|
| 99 | +# Initialize an Ollama embeddings model |
| 100 | +ollama_embeddings = OllamaEmbeddings( |
| 101 | + model="nomic-embed-text" |
| 102 | +) |
| 103 | +
|
| 104 | +config = { |
| 105 | + "embedder": { |
| 106 | + "provider": "langchain", |
| 107 | + "config": { |
| 108 | + "model": ollama_embeddings |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +<Note> |
| 115 | + Make sure to install the necessary LangChain packages and any provider-specific dependencies. |
| 116 | +</Note> |
| 117 | + |
| 118 | +## Config |
| 119 | + |
| 120 | +All available parameters for the `langchain` embedder config are present in [Master List of All Params in Config](../config). |
0 commit comments