diff --git a/docs/components/embedders/models/together.mdx b/docs/components/embedders/models/together.mdx new file mode 100644 index 0000000000..6788003941 --- /dev/null +++ b/docs/components/embedders/models/together.mdx @@ -0,0 +1,38 @@ +--- +title: Together +--- + +To use Together embedding models, set the `TOGETHER_API_KEY` environment variable. You can obtain the Together API key from the [Together Platform](https://api.together.xyz/settings/api-keys). + +### Usage + + The `embedding_model_dims` parameter for `vector_store` should be set to `768` for Together embedder. + +```python +import os +from mem0 import Memory + +os.environ["TOGETHER_API_KEY"] = "your_api_key" + +config = { + "embedder": { + "provider": "together", + "config": { + "model": "togethercomputer/m2-bert-80M-8k-retrieval" + } + } +} + +m = Memory.from_config(config) +m.add("I'm visiting Paris", user_id="john") +``` + +### Config + +Here are the parameters available for configuring Together embedder: + +| Parameter | Description | Default Value | +| --- | --- | --- | +| `model` | The name of the embedding model to use | `togethercomputer/m2-bert-80M-8k-retrieval` | +| `embedding_dims` | Dimensions of the embedding model | `768` | +| `api_key` | The Together API key | `None` | diff --git a/docs/components/embedders/overview.mdx b/docs/components/embedders/overview.mdx index 48e2cfdc78..bb356357a3 100644 --- a/docs/components/embedders/overview.mdx +++ b/docs/components/embedders/overview.mdx @@ -15,6 +15,7 @@ See the list of supported embedders below. + ## Usage diff --git a/mem0/embeddings/configs.py b/mem0/embeddings/configs.py index 90219ce6c3..b35ffba414 100644 --- a/mem0/embeddings/configs.py +++ b/mem0/embeddings/configs.py @@ -13,7 +13,7 @@ class EmbedderConfig(BaseModel): @field_validator("config") def validate_config(cls, v, values): provider = values.data.get("provider") - if provider in ["openai", "ollama", "huggingface", "azure_openai", "gemini", "vertexai"]: + if provider in ["openai", "ollama", "huggingface", "azure_openai", "gemini", "vertexai", "together"]: return v else: raise ValueError(f"Unsupported embedding provider: {provider}") diff --git a/mem0/embeddings/together.py b/mem0/embeddings/together.py new file mode 100644 index 0000000000..dd16bd990e --- /dev/null +++ b/mem0/embeddings/together.py @@ -0,0 +1,31 @@ +import os +from typing import Optional + +from together import Together + +from mem0.configs.embeddings.base import BaseEmbedderConfig +from mem0.embeddings.base import EmbeddingBase + + +class TogetherEmbedding(EmbeddingBase): + def __init__(self, config: Optional[BaseEmbedderConfig] = None): + super().__init__(config) + + self.config.model = self.config.model or "togethercomputer/m2-bert-80M-8k-retrieval" + api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY") + # TODO: check if this is correct + self.config.embedding_dims = self.config.embedding_dims or 768 + self.client = Together(api_key=api_key) + + def embed(self, text): + """ + Get the embedding for the given text using OpenAI. + + Args: + text (str): The text to embed. + + Returns: + list: The embedding vector. + """ + + return self.client.embeddings.create(model=self.config.model, input=text).data[0].embedding \ No newline at end of file diff --git a/mem0/utils/factory.py b/mem0/utils/factory.py index 56d2cc2c46..5e0defc307 100644 --- a/mem0/utils/factory.py +++ b/mem0/utils/factory.py @@ -44,6 +44,7 @@ class EmbedderFactory: "azure_openai": "mem0.embeddings.azure_openai.AzureOpenAIEmbedding", "gemini": "mem0.embeddings.gemini.GoogleGenAIEmbedding", "vertexai": "mem0.embeddings.vertexai.VertexAIEmbedding", + "together": "mem0.embeddings.together.TogetherEmbedding", } @classmethod