The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
Add support for anthropic (#1819)
Dev Khantd32ae1a0b177
diff --git a/docs/components/llms/models/anthropic.mdx b/docs/components/llms/models/anthropic.mdxindex 916c88d2d..bb5f8c2d1 100644--- a/docs/components/llms/models/anthropic.mdx+++ b/docs/components/llms/models/anthropic.mdx@@ -11,7 +11,7 @@ os.environ["ANTHROPIC_API_KEY"] = "your-api-key"config = {"llm": {- "provider": "litellm",+ "provider": "anthropic","config": {"model": "claude-3-opus-20240229","temperature": 0.1,diff --git a/mem0/llms/anthropic.py b/mem0/llms/anthropic.pynew file mode 100644index 000000000..5e32d81c9--- /dev/null+++ b/mem0/llms/anthropic.py@@ -0,0 +1,67 @@+import subprocess+import sys+import os+import json+from typing import Dict, List, Optional++try:+ import anthropic+except ImportError:+ raise ImportError("The 'anthropic' library is required. Please install it using 'pip install anthropic'.")++from mem0.llms.base import LLMBase+from mem0.configs.llms.base import BaseLlmConfig+++class AnthropicLLM(LLMBase):+ def __init__(self, config: Optional[BaseLlmConfig] = None):+ super().__init__(config)++ if not self.config.model:+ self.config.model = "claude-3-5-sonnet-20240620"++ api_key = self.config.api_key or os.getenv("ANTHROPIC_API_KEY")+ self.client = anthropic.Anthropic(api_key=api_key)++ 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 Anthropic.++ 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.+ """+ # Separate system message from other messages+ system_message = ""+ filtered_messages = []+ for message in messages:+ if message['role'] == 'system':+ system_message = message['content']+ else:+ filtered_messages.append(message)++ params = {+ "model": self.config.model,+ "messages": filtered_messages,+ "system": system_message,+ "temperature": self.config.temperature,+ "max_tokens": self.config.max_tokens,+ "top_p": self.config.top_p,+ }+ if tools: # TODO: Remove tools if no issues found with new memory addition logic+ params["tools"] = tools+ params["tool_choice"] = tool_choice++ response = self.client.messages.create(**params)+ return response.content[0].textdiff --git a/mem0/llms/aws_bedrock.py b/mem0/llms/aws_bedrock.pyindex e2c1ea0f6..b5699a9ef 100644--- a/mem0/llms/aws_bedrock.py+++ b/mem0/llms/aws_bedrock.py@@ -7,16 +7,7 @@ from typing import Dict, List, Optional, Anytry:import boto3except ImportError:- user_input = input("The 'boto3' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "boto3"])- import boto3- except subprocess.CalledProcessError:- print("Failed to install 'boto3'. Please install it manually using 'pip install boto3'")- sys.exit(1)- else:- raise ImportError("The required 'boto3' library is not installed.")+ raise ImportError("The 'boto3' library is required. Please install it using 'pip install boto3'.")from mem0.llms.base import LLMBasefrom mem0.configs.llms.base import BaseLlmConfigdiff --git a/mem0/llms/azure_openai.py b/mem0/llms/azure_openai.pyindex 520631ab1..d18569a8e 100644--- a/mem0/llms/azure_openai.py+++ b/mem0/llms/azure_openai.py@@ -87,7 +87,7 @@ class AzureOpenAILLM(LLMBase):}if response_format:params["response_format"] = response_format- if tools:+ if tools: # TODO: Remove tools if no issues found with new memory addition logicparams["tools"] = toolsparams["tool_choice"] = tool_choicediff --git a/mem0/llms/groq.py b/mem0/llms/groq.pyindex 0163f2c16..c67436c08 100644--- a/mem0/llms/groq.py+++ b/mem0/llms/groq.py@@ -7,16 +7,7 @@ from typing import Dict, List, Optionaltry:from groq import Groqexcept ImportError:- user_input = input("The 'groq' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "groq"])- from groq import Groq- except subprocess.CalledProcessError:- print("Failed to install 'groq'. Please install it manually using 'pip install groq'.")- sys.exit(1)- else:- raise ImportError("The required 'groq' library is not installed.")+ raise ImportError("The 'groq' library is required. Please install it using 'pip install groq'.")from mem0.llms.base import LLMBasefrom mem0.configs.llms.base import BaseLlmConfig@@ -28,7 +19,6 @@ class GroqLLM(LLMBase):if not self.config.model:self.config.model = "llama3-70b-8192"- self.client = Groq()api_key = self.config.api_key or os.getenv("GROQ_API_KEY")self.client = Groq(api_key=api_key)diff --git a/mem0/llms/litellm.py b/mem0/llms/litellm.pyindex 9d910370d..1a6fb7c0c 100644--- a/mem0/llms/litellm.py+++ b/mem0/llms/litellm.py@@ -6,16 +6,7 @@ from typing import Dict, List, Optionaltry:import litellmexcept ImportError:- user_input = input("The 'litellm' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "litellm"])- import litellm- except subprocess.CalledProcessError:- print("Failed to install 'litellm'. Please install it manually using 'pip install litellm'.")- sys.exit(1)- else:- raise ImportError("The required 'litellm' library is not installed.")+ raise ImportError("The 'litellm' library is required. Please install it using 'pip install litellm'.")from mem0.llms.base import LLMBasefrom mem0.configs.llms.base import BaseLlmConfig@@ -91,7 +82,7 @@ class LiteLLM(LLMBase):}if response_format:params["response_format"] = response_format- if tools:+ if tools: # TODO: Remove tools if no issues found with new memory addition logicparams["tools"] = toolsparams["tool_choice"] = tool_choicediff --git a/mem0/llms/ollama.py b/mem0/llms/ollama.pyindex e86cac5ea..e7acbdd7b 100644--- a/mem0/llms/ollama.py+++ b/mem0/llms/ollama.py@@ -5,17 +5,7 @@ from typing import Dict, List, Optionaltry:from ollama import Clientexcept ImportError:- user_input = input("The 'ollama' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "ollama"])- from ollama import Client- except subprocess.CalledProcessError:- print("Failed to install 'ollama'. Please install it manually using 'pip install ollama'.")- sys.exit(1)- else:- print("The required 'ollama' library is not installed.")- sys.exit(1)+ raise ImportError("The 'ollama' library is required. Please install it using 'pip install ollama'.")from mem0.llms.base import LLMBasefrom mem0.configs.llms.base import BaseLlmConfigdiff --git a/mem0/llms/openai.py b/mem0/llms/openai.pyindex 490051067..68a363453 100644--- a/mem0/llms/openai.py+++ b/mem0/llms/openai.py@@ -100,7 +100,7 @@ class OpenAILLM(LLMBase):if response_format:params["response_format"] = response_format- if tools:+ if tools: # TODO: Remove tools if no issues found with new memory addition logicparams["tools"] = toolsparams["tool_choice"] = tool_choicediff --git a/mem0/llms/together.py b/mem0/llms/together.pyindex fa477217d..816ed7a99 100644--- a/mem0/llms/together.py+++ b/mem0/llms/together.py@@ -7,17 +7,7 @@ from typing import Dict, List, Optionaltry:from together import Togetherexcept ImportError:- user_input = input("The 'together' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "together"])- from together import Together- except subprocess.CalledProcessError:- print("Failed to install 'together'. Please install it manually using 'pip install together'.")- sys.exit(1)- else:- print("The required 'together' library is not installed.")- sys.exit(1)+ raise ImportError("The 'together' library is required. Please install it using 'pip install together'.")from mem0.llms.base import LLMBasefrom mem0.configs.llms.base import BaseLlmConfig@@ -29,7 +19,6 @@ class TogetherLLM(LLMBase):if not self.config.model:self.config.model = "mistralai/Mixtral-8x7B-Instruct-v0.1"- self.client = Together()api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")self.client = Together(api_key=api_key)@@ -92,7 +81,7 @@ class TogetherLLM(LLMBase):}if response_format:params["response_format"] = response_format- if tools:+ if tools: # TODO: Remove tools if no issues found with new memory addition logicparams["tools"] = toolsparams["tool_choice"] = tool_choicediff --git a/mem0/llms/utils/tools.py b/mem0/llms/utils/tools.pyindex 50031e361..fb4ff4a2a 100644--- a/mem0/llms/utils/tools.py+++ b/mem0/llms/utils/tools.py@@ -1,3 +1,5 @@+# TODO: Remove these tools if no issues are found for new memory addition logic+ADD_MEMORY_TOOL = {"type": "function","function": {diff --git a/mem0/utils/factory.py b/mem0/utils/factory.pyindex bf012b420..c672cf1d6 100644--- a/mem0/utils/factory.py+++ b/mem0/utils/factory.py@@ -20,6 +20,7 @@ class LlmFactory:"litellm": "mem0.llms.litellm.LiteLLM","azure_openai": "mem0.llms.azure_openai.AzureOpenAILLM","openai_structured": "mem0.llms.openai_structured.OpenAIStructuredLLM",+ "anthropic": "mem0.llms.anthropic.AnthropicLLM"}@classmethoddiff --git a/mem0/vector_stores/chroma.py b/mem0/vector_stores/chroma.pyindex 4904aacf5..21fc85c68 100644--- a/mem0/vector_stores/chroma.py+++ b/mem0/vector_stores/chroma.py@@ -9,18 +9,7 @@ try:import chromadbfrom chromadb.config import Settingsexcept ImportError:- user_input = input("The 'chromadb' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "chromadb"])- import chromadb- from chromadb.config import Settings- except subprocess.CalledProcessError:- print("Failed to install 'chromadb'. Please install it manually using 'pip install chromadb'.")- sys.exit(1)- else:- print("The required 'chromadb' library is not installed.")- sys.exit(1)+ raise ImportError("The 'chromadb' library is required. Please install it using 'pip install chromadb'.")from mem0.vector_stores.base import VectorStoreBasediff --git a/mem0/vector_stores/pgvector.py b/mem0/vector_stores/pgvector.pyindex 7f8c2159d..dae1e2d18 100644--- a/mem0/vector_stores/pgvector.py+++ b/mem0/vector_stores/pgvector.py@@ -9,19 +9,7 @@ try:import psycopg2from psycopg2.extras import execute_valuesexcept ImportError:- user_input = input("The 'psycopg2' library is required. Install it now? [y/N]: ")- if user_input.lower() == 'y':- try:- subprocess.check_call([sys.executable, "-m", "pip", "install", "psycopg2"])- import psycopg2- from psycopg2.extras import execute_values- except subprocess.CalledProcessError:- print("Failed to install 'psycopg2'. Please install it manually using 'pip install psycopg2'.")- sys.exit(1)- else:- print("The required 'psycopg2' library is not installed.")- sys.exit(1)-+ raise ImportError("The 'psycopg2' library is required. Please install it using 'pip install psycopg2'.")from mem0.vector_stores.base import VectorStoreBase