Add configs to Embedding docs (#1702)

This commit is contained in:
Dev Khant
2024-08-14 16:10:48 +05:30
committed by GitHub
parent aba5bb052d
commit 10cbee943c
9 changed files with 219 additions and 66 deletions

View File

@@ -1,64 +0,0 @@
---
title: Embedding models
---
## Overview
Mem0 offers support for various embedding models, allowing users to choose the one that best suits their needs.
<CardGroup cols={3}>
<Card title="OpenAI" href="#openai"></Card>
<Card title="Ollama" href="#ollama"></Card>
</CardGroup>
> When using `Qdrant` as a vector database, ensure you update the `embedding_model_dims` to match the dimensions of the embedding model you are using.
## OpenAI
To use OpenAI embedding models, set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys).
Example of how to select the desired embedding model:
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-large"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```
## Ollama
You can use embedding models from Ollama to run Mem0 locally.
Here's how to select it:
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "ollama",
"config": {
"model": "mxbai-embed-large"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```

View File

@@ -0,0 +1,57 @@
## What is Config?
Config in mem0 is a dictionary that specifies the settings for your embedding models. It allows you to customize the behavior and connection details of your chosen embedder.
## How to Define Config
The config is defined as a Python dictionary with two main keys:
- `embedder`: Specifies the embedder provider and its configuration
- `provider`: The name of the embedder (e.g., "openai", "ollama")
- `config`: A nested dictionary containing provider-specific settings
## How to Use Config
Here's a general example of how to use the config with mem0:
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "sk-xx"
config = {
"embedder": {
"provider": "your_chosen_provider",
"config": {
# Provider-specific settings go here
}
}
}
m = Memory.from_config(config)
m.add("Your text here", user_id="user", metadata={"category": "example"})
```
## Why is Config Needed?
Config is essential for:
1. Specifying which embedding model to use.
2. Providing necessary connection details (e.g., model, api_key, embedding_dims).
3. Ensuring proper initialization and connection to your chosen embedder.
## Master List of All Params in Config
Here's a comprehensive list of all parameters that can be used across different embedders:
| Parameter | Description |
|-----------|-------------|
| `model` | Embedding model to use |
| `api_key` | API key of the provider |
| `embedding_dims` | Dimensions of the embedding model |
| `ollama_base_url` | Base URL for the Ollama embedding model |
| `model_kwargs` | Key-Value arguments for the Huggingface embedding model |
## Supported Embedding Models
For detailed information on configuring specific embedders, please visit the [Embedding Models](./models) section. There you'll find information for each supported embedder with provider-specific usage examples and configuration details.

View File

@@ -0,0 +1,34 @@
To use Azure OpenAI embedding models, set the `AZURE_OPENAI_API_KEY` environment variable. You can obtain the Azure OpenAI API key from the Azure.
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
os.environ["AZURE_OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "azure_openai",
"config": {
"model": "text-embedding-3-large"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```
### Config
Here are the parameters available for configuring Azure OpenAI embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
| `embedding_dims` | Dimensions of the embedding model | `1536` |
| `api_key` | The Azure OpenAI API key | `None` |

View File

@@ -0,0 +1,32 @@
You can use embedding models from Huggingface to run Mem0 locally.
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "huggingface",
"config": {
"model": "multi-qa-MiniLM-L6-cos-v1"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```
### Config
Here are the parameters available for configuring Huggingface embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the model to use | `multi-qa-MiniLM-L6-cos-v1` |
| `embedding_dims` | Dimensions of the embedding model | `selected_model_dimensions` |
| `model_kwargs` | Additional arguments for the model | `None` |

View File

@@ -0,0 +1,32 @@
You can use embedding models from Ollama to run Mem0 locally.
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "ollama",
"config": {
"model": "mxbai-embed-large"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```
### Config
Here are the parameters available for configuring Ollama embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the OpenAI model to use | `nomic-embed-text` |
| `embedding_dims` | Dimensions of the embedding model | `512` |
| `ollama_base_url` | Base URL for ollama connection | `None` |

View File

@@ -0,0 +1,32 @@
To use OpenAI embedding models, set the `OPENAI_API_KEY` environment variable. You can obtain the OpenAI API key from the [OpenAI Platform](https://platform.openai.com/account/api-keys).
### Usage
```python
import os
from mem0 import Memory
os.environ["OPENAI_API_KEY"] = "your_api_key"
config = {
"embedder": {
"provider": "openai",
"config": {
"model": "text-embedding-3-large"
}
}
}
m = Memory.from_config(config)
m.add("I'm visiting Paris", user_id="john")
```
### Config
Here are the parameters available for configuring OpenAI embedder:
| Parameter | Description | Default Value |
| --- | --- | --- |
| `model` | The name of the embedding model to use | `text-embedding-3-small` |
| `embedding_dims` | Dimensions of the embedding model | `1536` |
| `api_key` | The OpenAI API key | `None` |

View File

@@ -0,0 +1,15 @@
---
title: Overview
---
Mem0 offers support for various embedding models, allowing users to choose the one that best suits their needs.
## Usage
To utilize a embedder, you must provide a configuration to customize its usage. If no configuration is supplied, a default configuration will be applied, and `OpenAI` will be used as the embedder.
For a comprehensive list of available parameters for embedder configuration, please refer to [Config](./config).
To view all supported embedders, visit the [Supported embedders](./models).

View File

@@ -82,7 +82,19 @@
},
{
"group": "Embedding Models",
"pages": ["components/embedders"]
"pages": [
"components/embedders/overview",
"components/embedders/config",
{
"group": "Supported Embedding Models",
"pages": [
"components/embedders/models/openai",
"components/embedders/models/azure_openai",
"components/embedders/models/ollama",
"components/embedders/models/huggingface"
]
}
]
},
{
"group": "Features",

View File

@@ -1,3 +1,4 @@
import os
from typing import Optional
from openai import AzureOpenAI
@@ -13,7 +14,9 @@ class AzureOpenAIEmbedding(EmbeddingBase):
self.config.model = "text-embedding-3-small"
if self.config.embedding_dims is None:
self.config.embedding_dims = 1536
self.client = AzureOpenAI()
api_key = os.getenv("AZURE_OPENAI_API_KEY") or self.config.api_key
self.client = AzureOpenAI(api_key=api_key)
def embed(self, text):
"""