Add Groq Support (#1481)

This commit is contained in:
Dev Khant
2024-07-16 23:33:28 +05:30
committed by GitHub
parent 80f145fceb
commit 19637804b3
11 changed files with 369 additions and 6 deletions

21
mem0/llms/configs.py Normal file
View 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
View 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