Add Groq Support (#1481)
This commit is contained in:
22
mem0/embeddings/configs.py
Normal file
22
mem0/embeddings/configs.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
|
||||
class EmbedderConfig(BaseModel):
|
||||
provider: str = Field(
|
||||
description="Provider of the embedding model (e.g., 'ollama', 'openai')",
|
||||
default="openai",
|
||||
)
|
||||
config: Optional[dict] = Field(
|
||||
description="Configuration for the specific embedding model", default=None
|
||||
)
|
||||
|
||||
@field_validator("config")
|
||||
def validate_config(cls, v, values):
|
||||
provider = values.data.get("provider")
|
||||
if provider in ["openai", "ollama"]:
|
||||
return v
|
||||
else:
|
||||
raise ValueError(f"Unsupported embedding provider: {provider}")
|
||||
|
||||
21
mem0/llms/configs.py
Normal file
21
mem0/llms/configs.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
|
||||
class LlmConfig(BaseModel):
|
||||
provider: str = Field(
|
||||
description="Provider of the LLM (e.g., 'ollama', 'openai')", default="openai"
|
||||
)
|
||||
config: Optional[dict] = Field(
|
||||
description="Configuration for the specific LLM", default=None
|
||||
)
|
||||
|
||||
@field_validator("config")
|
||||
def validate_config(cls, v, values):
|
||||
provider = values.data.get("provider")
|
||||
if provider in ["openai", "ollama", "groq"]:
|
||||
return v
|
||||
else:
|
||||
raise ValueError(f"Unsupported LLM provider: {provider}")
|
||||
|
||||
40
mem0/llms/groq.py
Normal file
40
mem0/llms/groq.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from groq import Groq
|
||||
|
||||
from mem0.llms.base import LLMBase
|
||||
|
||||
|
||||
class GroqLLM(LLMBase):
|
||||
def __init__(self, model="llama3-70b-8192"):
|
||||
self.client = Groq()
|
||||
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 Groq.
|
||||
|
||||
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
|
||||
@@ -7,8 +7,6 @@ from typing import Any, Dict, Optional
|
||||
|
||||
from pydantic import BaseModel, Field, ValidationError
|
||||
|
||||
from mem0.embeddings.openai import OpenAIEmbedding
|
||||
from mem0.llms.openai import OpenAILLM
|
||||
from mem0.llms.utils.tools import (
|
||||
ADD_MEMORY_TOOL,
|
||||
DELETE_MEMORY_TOOL,
|
||||
@@ -21,7 +19,10 @@ from mem0.memory.storage import SQLiteManager
|
||||
from mem0.memory.telemetry import capture_event
|
||||
from mem0.memory.utils import get_update_memory_messages
|
||||
from mem0.vector_stores.configs import VectorStoreConfig
|
||||
from mem0.llms.configs import LlmConfig
|
||||
from mem0.embeddings.configs import EmbedderConfig
|
||||
from mem0.vector_stores.qdrant import Qdrant
|
||||
from mem0.utils.factory import LlmFactory, EmbedderFactory
|
||||
|
||||
# Setup user config
|
||||
setup_config()
|
||||
@@ -44,6 +45,14 @@ class MemoryConfig(BaseModel):
|
||||
description="Configuration for the vector store",
|
||||
default_factory=VectorStoreConfig,
|
||||
)
|
||||
llm: LlmConfig = Field(
|
||||
description="Configuration for the language model",
|
||||
default_factory=LlmConfig,
|
||||
)
|
||||
embedder: EmbedderConfig = Field(
|
||||
description="Configuration for the embedding model",
|
||||
default_factory=EmbedderConfig,
|
||||
)
|
||||
history_db_path: str = Field(
|
||||
description="Path to the history database",
|
||||
default=os.path.join(mem0_dir, "history.db"),
|
||||
@@ -57,7 +66,7 @@ class MemoryConfig(BaseModel):
|
||||
class Memory(MemoryBase):
|
||||
def __init__(self, config: MemoryConfig = MemoryConfig()):
|
||||
self.config = config
|
||||
self.embedding_model = OpenAIEmbedding()
|
||||
self.embedding_model = EmbedderFactory.create(self.config.embedder.provider)
|
||||
# Initialize the appropriate vector store based on the configuration
|
||||
vector_store_config = self.config.vector_store.config
|
||||
if self.config.vector_store.provider == "qdrant":
|
||||
@@ -73,7 +82,7 @@ class Memory(MemoryBase):
|
||||
f"Unsupported vector store type: {self.config.vector_store_type}"
|
||||
)
|
||||
|
||||
self.llm = OpenAILLM()
|
||||
self.llm = LlmFactory.create(self.config.llm.provider)
|
||||
self.db = SQLiteManager(self.config.history_db_path)
|
||||
self.collection_name = self.config.collection_name
|
||||
self.vector_store.create_col(
|
||||
|
||||
41
mem0/utils/factory.py
Normal file
41
mem0/utils/factory.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import importlib
|
||||
|
||||
|
||||
def load_class(class_type):
|
||||
module_path, class_name = class_type.rsplit(".", 1)
|
||||
module = importlib.import_module(module_path)
|
||||
return getattr(module, class_name)
|
||||
|
||||
|
||||
class LlmFactory:
|
||||
provider_to_class = {
|
||||
"ollama": "mem0.llms.ollama.py.OllamaLLM",
|
||||
"openai": "mem0.llms.openai.OpenAILLM",
|
||||
"groq": "mem0.llms.groq.GroqLLM"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create(cls, provider_name):
|
||||
class_type = cls.provider_to_class.get(provider_name)
|
||||
if class_type:
|
||||
llm_instance = load_class(class_type)()
|
||||
return llm_instance
|
||||
else:
|
||||
raise ValueError(f"Unsupported Llm provider: {provider_name}")
|
||||
|
||||
class EmbedderFactory:
|
||||
provider_to_class = {
|
||||
"openai": "mem0.embeddings.openai.OpenAIEmbedding",
|
||||
"ollama": "mem0.embeddings.ollama.OllamaEmbedding",
|
||||
"huggingface": "mem0.embeddings.huggingface.HuggingFaceEmbedding"
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create(cls, provider_name):
|
||||
class_type = cls.provider_to_class.get(provider_name)
|
||||
if class_type:
|
||||
embedder_instance = load_class(class_type)()
|
||||
return embedder_instance
|
||||
else:
|
||||
raise ValueError(f"Unsupported Embedder provider: {provider_name}")
|
||||
|
||||
Reference in New Issue
Block a user