repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #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 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 | |
| #46 | ```typescript TypeScript |
| #47 | import { Memory } from 'mem0ai/oss'; |
| #48 | import { OpenAIEmbeddings } from "@langchain/openai"; |
| #49 | |
| #50 | // Initialize a LangChain embeddings model directly |
| #51 | const openaiEmbeddings = new OpenAIEmbeddings({ |
| #52 | modelName: "text-embedding-3-small", |
| #53 | dimensions: 1536, |
| #54 | apiKey: process.env.OPENAI_API_KEY, |
| #55 | }); |
| #56 | |
| #57 | const config = { |
| #58 | embedder: { |
| #59 | provider: 'langchain', |
| #60 | config: { |
| #61 | model: openaiEmbeddings, |
| #62 | }, |
| #63 | }, |
| #64 | }; |
| #65 | |
| #66 | const memory = new Memory(config); |
| #67 | const messages = [ |
| #68 | {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, |
| #69 | {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, |
| #70 | {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, |
| #71 | {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} |
| #72 | ] |
| #73 | await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); |
| #74 | ``` |
| #75 | </CodeGroup> |
| #76 | |
| #77 | ## Supported LangChain Embedding Providers |
| #78 | |
| #79 | LangChain supports a wide range of embedding providers, including: |
| #80 | |
| #81 | - OpenAI (`OpenAIEmbeddings`) |
| #82 | - Cohere (`CohereEmbeddings`) |
| #83 | - Google (`VertexAIEmbeddings`) |
| #84 | - Hugging Face (`HuggingFaceEmbeddings`) |
| #85 | - Sentence Transformers (`HuggingFaceEmbeddings`) |
| #86 | - Azure OpenAI (`AzureOpenAIEmbeddings`) |
| #87 | - Ollama (`OllamaEmbeddings`) |
| #88 | - Together (`TogetherEmbeddings`) |
| #89 | - And many more |
| #90 | |
| #91 | 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/). |
| #92 | |
| #93 | ## Provider-Specific Configuration |
| #94 | |
| #95 | When using LangChain as an embedder provider, you'll need to: |
| #96 | |
| #97 | 1. Set the appropriate environment variables for your chosen embedding provider |
| #98 | 2. Import and initialize the specific model class you want to use |
| #99 | 3. Pass the initialized model instance to the config |
| #100 | |
| #101 | ### Examples with Different Providers |
| #102 | |
| #103 | <CodeGroup> |
| #104 | #### HuggingFace Embeddings |
| #105 | |
| #106 | ```python Python |
| #107 | from langchain_huggingface import HuggingFaceEmbeddings |
| #108 | |
| #109 | # Initialize a HuggingFace embeddings model |
| #110 | hf_embeddings = HuggingFaceEmbeddings( |
| #111 | model_name="BAAI/bge-small-en-v1.5", |
| #112 | encode_kwargs={"normalize_embeddings": True} |
| #113 | ) |
| #114 | |
| #115 | config = { |
| #116 | "embedder": { |
| #117 | "provider": "langchain", |
| #118 | "config": { |
| #119 | "model": hf_embeddings |
| #120 | } |
| #121 | } |
| #122 | } |
| #123 | ``` |
| #124 | |
| #125 | ```typescript TypeScript |
| #126 | import { Memory } from 'mem0ai/oss'; |
| #127 | import { HuggingFaceEmbeddings } from "@langchain/community/embeddings/hf"; |
| #128 | |
| #129 | // Initialize a HuggingFace embeddings model |
| #130 | const hfEmbeddings = new HuggingFaceEmbeddings({ |
| #131 | modelName: "BAAI/bge-small-en-v1.5", |
| #132 | encode: { |
| #133 | normalize_embeddings: true, |
| #134 | }, |
| #135 | }); |
| #136 | |
| #137 | const config = { |
| #138 | embedder: { |
| #139 | provider: 'langchain', |
| #140 | config: { |
| #141 | model: hfEmbeddings, |
| #142 | }, |
| #143 | }, |
| #144 | }; |
| #145 | ``` |
| #146 | </CodeGroup> |
| #147 | |
| #148 | <CodeGroup> |
| #149 | #### Ollama Embeddings |
| #150 | |
| #151 | ```python Python |
| #152 | from langchain_ollama import OllamaEmbeddings |
| #153 | |
| #154 | # Initialize an Ollama embeddings model |
| #155 | ollama_embeddings = OllamaEmbeddings( |
| #156 | model="nomic-embed-text" |
| #157 | ) |
| #158 | |
| #159 | config = { |
| #160 | "embedder": { |
| #161 | "provider": "langchain", |
| #162 | "config": { |
| #163 | "model": ollama_embeddings |
| #164 | } |
| #165 | } |
| #166 | } |
| #167 | ``` |
| #168 | |
| #169 | ```typescript TypeScript |
| #170 | import { Memory } from 'mem0ai/oss'; |
| #171 | import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama"; |
| #172 | |
| #173 | // Initialize an Ollama embeddings model |
| #174 | const ollamaEmbeddings = new OllamaEmbeddings({ |
| #175 | model: "nomic-embed-text", |
| #176 | baseUrl: "http://localhost:11434", // Ollama server URL |
| #177 | }); |
| #178 | |
| #179 | const config = { |
| #180 | embedder: { |
| #181 | provider: 'langchain', |
| #182 | config: { |
| #183 | model: ollamaEmbeddings, |
| #184 | }, |
| #185 | }, |
| #186 | }; |
| #187 | ``` |
| #188 | </CodeGroup> |
| #189 | |
| #190 | <Note> |
| #191 | Make sure to install the necessary LangChain packages and any provider-specific dependencies. |
| #192 | </Note> |
| #193 | |
| #194 | ## Config |
| #195 | |
| #196 | All available parameters for the `langchain` embedder config are present in [Master List of All Params in Config](../config). |
| #197 |