Rename embedchain to mem0 and open sourcing code for long term memory (#1474)

Co-authored-by: Deshraj Yadav <deshrajdry@gmail.com>
This commit is contained in:
Taranjeet Singh
2024-07-12 07:51:33 -07:00
committed by GitHub
parent 83e8c97295
commit f842a92e25
665 changed files with 9427 additions and 6592 deletions

View File

16
mem0/embeddings/base.py Normal file
View File

@@ -0,0 +1,16 @@
from abc import ABC, abstractmethod
class EmbeddingBase(ABC):
@abstractmethod
def embed(self, text):
"""
Get the embedding for the given text.
Args:
text (str): The text to embed.
Returns:
list: The embedding vector.
"""
pass

View File

@@ -0,0 +1,19 @@
from embedding.base import EmbeddingBase
from sentence_transformers import SentenceTransformer
class HuggingFaceEmbedding(EmbeddingBase):
def __init__(self, model_name="multi-qa-MiniLM-L6-cos-v1"):
self.model = SentenceTransformer(model_name)
def get_embedding(self, text):
"""
Get the embedding for the given text using Hugging Face.
Args:
text (str): The text to embed.
Returns:
list: The embedding vector.
"""
return self.model.encode(text)

30
mem0/embeddings/ollama.py Normal file
View File

@@ -0,0 +1,30 @@
import ollama
from embedding.base import EmbeddingBase
class OllamaEmbedding(EmbeddingBase):
def __init__(self, model="nomic-embed-text"):
self.model = model
self._ensure_model_exists()
self.dims = 512
def _ensure_model_exists(self):
"""
Ensure the specified model exists locally. If not, pull it from Ollama.
"""
model_list = [m["name"] for m in ollama.list()["models"]]
if not any(m.startswith(self.model) for m in model_list):
ollama.pull(self.model)
def embed(self, text):
"""
Get the embedding for the given text using Ollama.
Args:
text (str): The text to embed.
Returns:
list: The embedding vector.
"""
response = ollama.embeddings(model=self.model, prompt=text)
return response["embedding"]

27
mem0/embeddings/openai.py Normal file
View File

@@ -0,0 +1,27 @@
from openai import OpenAI
from mem0.embeddings.base import EmbeddingBase
class OpenAIEmbedding(EmbeddingBase):
def __init__(self, model="text-embedding-3-small"):
self.client = OpenAI()
self.model = model
self.dims = 1536
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.
"""
text = text.replace("\n", " ")
return (
self.client.embeddings.create(input=[text], model=self.model)
.data[0]
.embedding
)