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

0
mem0/llms/__init__.py Normal file
View File

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

@@ -0,0 +1,16 @@
from abc import ABC, abstractmethod
class LLMBase(ABC):
@abstractmethod
def generate_response(self, messages):
"""
Generate a response based on the given messages.
Args:
messages (list): List of message dicts containing 'role' and 'content'.
Returns:
str: The generated response.
"""
pass

29
mem0/llms/ollama.py Normal file
View File

@@ -0,0 +1,29 @@
import ollama
from llm.base import LLMBase
class OllamaLLM(LLMBase):
def __init__(self, model="llama3"):
self.model = model
self._ensure_model_exists()
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 generate_response(self, messages):
"""
Generate a response based on the given messages using Ollama.
Args:
messages (list): List of message dicts containing 'role' and 'content'.
Returns:
str: The generated response.
"""
response = ollama.chat(model=self.model, messages=messages)
return response["message"]["content"]

41
mem0/llms/openai.py Normal file
View File

@@ -0,0 +1,41 @@
from typing import Dict, List, Optional
from openai import OpenAI
from mem0.llms.base import LLMBase
class OpenAILLM(LLMBase):
def __init__(self, model="gpt-4o"):
self.client = OpenAI()
self.model = model
def generate_response(
self,
messages: List[Dict[str, str]],
response_format=None,
tools: Optional[List[Dict]] = None,
tool_choice: str = "auto",
):
"""
Generate a response based on the given messages using OpenAI.
Args:
messages (list): List of message dicts containing 'role' and 'content'.
response_format (str or object, optional): Format of the response. Defaults to "text".
tools (list, optional): List of tools that the model can call. Defaults to None.
tool_choice (str, optional): Tool choice method. Defaults to "auto".
Returns:
str: The generated response.
"""
params = {"model": self.model, "messages": messages}
if response_format:
params["response_format"] = response_format
if tools:
params["tools"] = tools
params["tool_choice"] = tool_choice
response = self.client.chat.completions.create(**params)
return response
# return response.choices[0].message["content"]

View File

View File

54
mem0/llms/utils/tools.py Normal file
View File

@@ -0,0 +1,54 @@
ADD_MEMORY_TOOL = {
"type": "function",
"function": {
"name": "add_memory",
"description": "Add a memory",
"parameters": {
"type": "object",
"properties": {
"data": {"type": "string", "description": "Data to add to memory"}
},
"required": ["data"],
},
},
}
UPDATE_MEMORY_TOOL = {
"type": "function",
"function": {
"name": "update_memory",
"description": "Update memory provided ID and data",
"parameters": {
"type": "object",
"properties": {
"memory_id": {
"type": "string",
"description": "memory_id of the memory to update",
},
"data": {
"type": "string",
"description": "Updated data for the memory",
},
},
"required": ["memory_id", "data"],
},
},
}
DELETE_MEMORY_TOOL = {
"type": "function",
"function": {
"name": "delete_memory",
"description": "Delete memory by memory_id",
"parameters": {
"type": "object",
"properties": {
"memory_id": {
"type": "string",
"description": "memory_id of the memory to delete",
}
},
"required": ["memory_id"],
},
},
}