agentrun-mem0ai 0.0.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. agentrun_mem0/__init__.py +6 -0
  2. agentrun_mem0/client/__init__.py +0 -0
  3. agentrun_mem0/client/main.py +1747 -0
  4. agentrun_mem0/client/project.py +931 -0
  5. agentrun_mem0/client/utils.py +115 -0
  6. agentrun_mem0/configs/__init__.py +0 -0
  7. agentrun_mem0/configs/base.py +90 -0
  8. agentrun_mem0/configs/embeddings/__init__.py +0 -0
  9. agentrun_mem0/configs/embeddings/base.py +110 -0
  10. agentrun_mem0/configs/enums.py +7 -0
  11. agentrun_mem0/configs/llms/__init__.py +0 -0
  12. agentrun_mem0/configs/llms/anthropic.py +56 -0
  13. agentrun_mem0/configs/llms/aws_bedrock.py +192 -0
  14. agentrun_mem0/configs/llms/azure.py +57 -0
  15. agentrun_mem0/configs/llms/base.py +62 -0
  16. agentrun_mem0/configs/llms/deepseek.py +56 -0
  17. agentrun_mem0/configs/llms/lmstudio.py +59 -0
  18. agentrun_mem0/configs/llms/ollama.py +56 -0
  19. agentrun_mem0/configs/llms/openai.py +79 -0
  20. agentrun_mem0/configs/llms/vllm.py +56 -0
  21. agentrun_mem0/configs/prompts.py +459 -0
  22. agentrun_mem0/configs/rerankers/__init__.py +0 -0
  23. agentrun_mem0/configs/rerankers/base.py +17 -0
  24. agentrun_mem0/configs/rerankers/cohere.py +15 -0
  25. agentrun_mem0/configs/rerankers/config.py +12 -0
  26. agentrun_mem0/configs/rerankers/huggingface.py +17 -0
  27. agentrun_mem0/configs/rerankers/llm.py +48 -0
  28. agentrun_mem0/configs/rerankers/sentence_transformer.py +16 -0
  29. agentrun_mem0/configs/rerankers/zero_entropy.py +28 -0
  30. agentrun_mem0/configs/vector_stores/__init__.py +0 -0
  31. agentrun_mem0/configs/vector_stores/alibabacloud_mysql.py +64 -0
  32. agentrun_mem0/configs/vector_stores/aliyun_tablestore.py +32 -0
  33. agentrun_mem0/configs/vector_stores/azure_ai_search.py +57 -0
  34. agentrun_mem0/configs/vector_stores/azure_mysql.py +84 -0
  35. agentrun_mem0/configs/vector_stores/baidu.py +27 -0
  36. agentrun_mem0/configs/vector_stores/chroma.py +58 -0
  37. agentrun_mem0/configs/vector_stores/databricks.py +61 -0
  38. agentrun_mem0/configs/vector_stores/elasticsearch.py +65 -0
  39. agentrun_mem0/configs/vector_stores/faiss.py +37 -0
  40. agentrun_mem0/configs/vector_stores/langchain.py +30 -0
  41. agentrun_mem0/configs/vector_stores/milvus.py +42 -0
  42. agentrun_mem0/configs/vector_stores/mongodb.py +25 -0
  43. agentrun_mem0/configs/vector_stores/neptune.py +27 -0
  44. agentrun_mem0/configs/vector_stores/opensearch.py +41 -0
  45. agentrun_mem0/configs/vector_stores/pgvector.py +52 -0
  46. agentrun_mem0/configs/vector_stores/pinecone.py +55 -0
  47. agentrun_mem0/configs/vector_stores/qdrant.py +47 -0
  48. agentrun_mem0/configs/vector_stores/redis.py +24 -0
  49. agentrun_mem0/configs/vector_stores/s3_vectors.py +28 -0
  50. agentrun_mem0/configs/vector_stores/supabase.py +44 -0
  51. agentrun_mem0/configs/vector_stores/upstash_vector.py +34 -0
  52. agentrun_mem0/configs/vector_stores/valkey.py +15 -0
  53. agentrun_mem0/configs/vector_stores/vertex_ai_vector_search.py +28 -0
  54. agentrun_mem0/configs/vector_stores/weaviate.py +41 -0
  55. agentrun_mem0/embeddings/__init__.py +0 -0
  56. agentrun_mem0/embeddings/aws_bedrock.py +100 -0
  57. agentrun_mem0/embeddings/azure_openai.py +55 -0
  58. agentrun_mem0/embeddings/base.py +31 -0
  59. agentrun_mem0/embeddings/configs.py +30 -0
  60. agentrun_mem0/embeddings/gemini.py +39 -0
  61. agentrun_mem0/embeddings/huggingface.py +44 -0
  62. agentrun_mem0/embeddings/langchain.py +35 -0
  63. agentrun_mem0/embeddings/lmstudio.py +29 -0
  64. agentrun_mem0/embeddings/mock.py +11 -0
  65. agentrun_mem0/embeddings/ollama.py +53 -0
  66. agentrun_mem0/embeddings/openai.py +49 -0
  67. agentrun_mem0/embeddings/together.py +31 -0
  68. agentrun_mem0/embeddings/vertexai.py +64 -0
  69. agentrun_mem0/exceptions.py +503 -0
  70. agentrun_mem0/graphs/__init__.py +0 -0
  71. agentrun_mem0/graphs/configs.py +105 -0
  72. agentrun_mem0/graphs/neptune/__init__.py +0 -0
  73. agentrun_mem0/graphs/neptune/base.py +497 -0
  74. agentrun_mem0/graphs/neptune/neptunedb.py +511 -0
  75. agentrun_mem0/graphs/neptune/neptunegraph.py +474 -0
  76. agentrun_mem0/graphs/tools.py +371 -0
  77. agentrun_mem0/graphs/utils.py +97 -0
  78. agentrun_mem0/llms/__init__.py +0 -0
  79. agentrun_mem0/llms/anthropic.py +87 -0
  80. agentrun_mem0/llms/aws_bedrock.py +665 -0
  81. agentrun_mem0/llms/azure_openai.py +141 -0
  82. agentrun_mem0/llms/azure_openai_structured.py +91 -0
  83. agentrun_mem0/llms/base.py +131 -0
  84. agentrun_mem0/llms/configs.py +34 -0
  85. agentrun_mem0/llms/deepseek.py +107 -0
  86. agentrun_mem0/llms/gemini.py +201 -0
  87. agentrun_mem0/llms/groq.py +88 -0
  88. agentrun_mem0/llms/langchain.py +94 -0
  89. agentrun_mem0/llms/litellm.py +87 -0
  90. agentrun_mem0/llms/lmstudio.py +114 -0
  91. agentrun_mem0/llms/ollama.py +117 -0
  92. agentrun_mem0/llms/openai.py +147 -0
  93. agentrun_mem0/llms/openai_structured.py +52 -0
  94. agentrun_mem0/llms/sarvam.py +89 -0
  95. agentrun_mem0/llms/together.py +88 -0
  96. agentrun_mem0/llms/vllm.py +107 -0
  97. agentrun_mem0/llms/xai.py +52 -0
  98. agentrun_mem0/memory/__init__.py +0 -0
  99. agentrun_mem0/memory/base.py +63 -0
  100. agentrun_mem0/memory/graph_memory.py +698 -0
  101. agentrun_mem0/memory/kuzu_memory.py +713 -0
  102. agentrun_mem0/memory/main.py +2229 -0
  103. agentrun_mem0/memory/memgraph_memory.py +689 -0
  104. agentrun_mem0/memory/setup.py +56 -0
  105. agentrun_mem0/memory/storage.py +218 -0
  106. agentrun_mem0/memory/telemetry.py +90 -0
  107. agentrun_mem0/memory/utils.py +208 -0
  108. agentrun_mem0/proxy/__init__.py +0 -0
  109. agentrun_mem0/proxy/main.py +189 -0
  110. agentrun_mem0/reranker/__init__.py +9 -0
  111. agentrun_mem0/reranker/base.py +20 -0
  112. agentrun_mem0/reranker/cohere_reranker.py +85 -0
  113. agentrun_mem0/reranker/huggingface_reranker.py +147 -0
  114. agentrun_mem0/reranker/llm_reranker.py +142 -0
  115. agentrun_mem0/reranker/sentence_transformer_reranker.py +107 -0
  116. agentrun_mem0/reranker/zero_entropy_reranker.py +96 -0
  117. agentrun_mem0/utils/factory.py +283 -0
  118. agentrun_mem0/utils/gcp_auth.py +167 -0
  119. agentrun_mem0/vector_stores/__init__.py +0 -0
  120. agentrun_mem0/vector_stores/alibabacloud_mysql.py +547 -0
  121. agentrun_mem0/vector_stores/aliyun_tablestore.py +252 -0
  122. agentrun_mem0/vector_stores/azure_ai_search.py +396 -0
  123. agentrun_mem0/vector_stores/azure_mysql.py +463 -0
  124. agentrun_mem0/vector_stores/baidu.py +368 -0
  125. agentrun_mem0/vector_stores/base.py +58 -0
  126. agentrun_mem0/vector_stores/chroma.py +332 -0
  127. agentrun_mem0/vector_stores/configs.py +67 -0
  128. agentrun_mem0/vector_stores/databricks.py +761 -0
  129. agentrun_mem0/vector_stores/elasticsearch.py +237 -0
  130. agentrun_mem0/vector_stores/faiss.py +479 -0
  131. agentrun_mem0/vector_stores/langchain.py +180 -0
  132. agentrun_mem0/vector_stores/milvus.py +250 -0
  133. agentrun_mem0/vector_stores/mongodb.py +310 -0
  134. agentrun_mem0/vector_stores/neptune_analytics.py +467 -0
  135. agentrun_mem0/vector_stores/opensearch.py +292 -0
  136. agentrun_mem0/vector_stores/pgvector.py +404 -0
  137. agentrun_mem0/vector_stores/pinecone.py +382 -0
  138. agentrun_mem0/vector_stores/qdrant.py +270 -0
  139. agentrun_mem0/vector_stores/redis.py +295 -0
  140. agentrun_mem0/vector_stores/s3_vectors.py +176 -0
  141. agentrun_mem0/vector_stores/supabase.py +237 -0
  142. agentrun_mem0/vector_stores/upstash_vector.py +293 -0
  143. agentrun_mem0/vector_stores/valkey.py +824 -0
  144. agentrun_mem0/vector_stores/vertex_ai_vector_search.py +635 -0
  145. agentrun_mem0/vector_stores/weaviate.py +343 -0
  146. agentrun_mem0ai-0.0.11.data/data/README.md +205 -0
  147. agentrun_mem0ai-0.0.11.dist-info/METADATA +277 -0
  148. agentrun_mem0ai-0.0.11.dist-info/RECORD +150 -0
  149. agentrun_mem0ai-0.0.11.dist-info/WHEEL +4 -0
  150. agentrun_mem0ai-0.0.11.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,117 @@
1
+ from typing import Dict, List, Optional, Union
2
+
3
+ try:
4
+ from ollama import Client
5
+ except ImportError:
6
+ raise ImportError("The 'ollama' library is required. Please install it using 'pip install ollama'.")
7
+
8
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
9
+ from agentrun_mem0.configs.llms.ollama import OllamaConfig
10
+ from agentrun_mem0.llms.base import LLMBase
11
+
12
+
13
+ class OllamaLLM(LLMBase):
14
+ def __init__(self, config: Optional[Union[BaseLlmConfig, OllamaConfig, Dict]] = None):
15
+ # Convert to OllamaConfig if needed
16
+ if config is None:
17
+ config = OllamaConfig()
18
+ elif isinstance(config, dict):
19
+ config = OllamaConfig(**config)
20
+ elif isinstance(config, BaseLlmConfig) and not isinstance(config, OllamaConfig):
21
+ # Convert BaseLlmConfig to OllamaConfig
22
+ config = OllamaConfig(
23
+ model=config.model,
24
+ temperature=config.temperature,
25
+ api_key=config.api_key,
26
+ max_tokens=config.max_tokens,
27
+ top_p=config.top_p,
28
+ top_k=config.top_k,
29
+ enable_vision=config.enable_vision,
30
+ vision_details=config.vision_details,
31
+ http_client_proxies=config.http_client,
32
+ )
33
+
34
+ super().__init__(config)
35
+
36
+ if not self.config.model:
37
+ self.config.model = "llama3.1:70b"
38
+
39
+ self.client = Client(host=self.config.ollama_base_url)
40
+
41
+ def _parse_response(self, response, tools):
42
+ """
43
+ Process the response based on whether tools are used or not.
44
+
45
+ Args:
46
+ response: The raw response from API.
47
+ tools: The list of tools provided in the request.
48
+
49
+ Returns:
50
+ str or dict: The processed response.
51
+ """
52
+ # Get the content from response
53
+ if isinstance(response, dict):
54
+ content = response["message"]["content"]
55
+ else:
56
+ content = response.message.content
57
+
58
+ if tools:
59
+ processed_response = {
60
+ "content": content,
61
+ "tool_calls": [],
62
+ }
63
+
64
+ # Ollama doesn't support tool calls in the same way, so we return the content
65
+ return processed_response
66
+ else:
67
+ return content
68
+
69
+ def generate_response(
70
+ self,
71
+ messages: List[Dict[str, str]],
72
+ response_format=None,
73
+ tools: Optional[List[Dict]] = None,
74
+ tool_choice: str = "auto",
75
+ **kwargs,
76
+ ):
77
+ """
78
+ Generate a response based on the given messages using Ollama.
79
+
80
+ Args:
81
+ messages (list): List of message dicts containing 'role' and 'content'.
82
+ response_format (str or object, optional): Format of the response. Defaults to "text".
83
+ tools (list, optional): List of tools that the model can call. Defaults to None.
84
+ tool_choice (str, optional): Tool choice method. Defaults to "auto".
85
+ **kwargs: Additional Ollama-specific parameters.
86
+
87
+ Returns:
88
+ str: The generated response.
89
+ """
90
+ # Build parameters for Ollama
91
+ params = {
92
+ "model": self.config.model,
93
+ "messages": messages,
94
+ }
95
+
96
+ # Handle JSON response format by using Ollama's native format parameter
97
+ if response_format and response_format.get("type") == "json_object":
98
+ params["format"] = "json"
99
+ # Also add JSON format instruction to the last message as a fallback
100
+ if messages and messages[-1]["role"] == "user":
101
+ messages[-1]["content"] += "\n\nPlease respond with valid JSON only."
102
+ else:
103
+ messages.append({"role": "user", "content": "Please respond with valid JSON only."})
104
+
105
+ # Add options for Ollama (temperature, num_predict, top_p)
106
+ options = {
107
+ "temperature": self.config.temperature,
108
+ "num_predict": self.config.max_tokens,
109
+ "top_p": self.config.top_p,
110
+ }
111
+ params["options"] = options
112
+
113
+ # Remove OpenAI-specific parameters that Ollama doesn't support
114
+ params.pop("max_tokens", None) # Ollama uses different parameter names
115
+
116
+ response = self.client.chat(**params)
117
+ return self._parse_response(response, tools)
@@ -0,0 +1,147 @@
1
+ import json
2
+ import logging
3
+ import os
4
+ from typing import Dict, List, Optional, Union
5
+
6
+ from openai import OpenAI
7
+
8
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
9
+ from agentrun_mem0.configs.llms.openai import OpenAIConfig
10
+ from agentrun_mem0.llms.base import LLMBase
11
+ from agentrun_mem0.memory.utils import extract_json
12
+
13
+
14
+ class OpenAILLM(LLMBase):
15
+ def __init__(self, config: Optional[Union[BaseLlmConfig, OpenAIConfig, Dict]] = None):
16
+ # Convert to OpenAIConfig if needed
17
+ if config is None:
18
+ config = OpenAIConfig()
19
+ elif isinstance(config, dict):
20
+ config = OpenAIConfig(**config)
21
+ elif isinstance(config, BaseLlmConfig) and not isinstance(config, OpenAIConfig):
22
+ # Convert BaseLlmConfig to OpenAIConfig
23
+ config = OpenAIConfig(
24
+ model=config.model,
25
+ temperature=config.temperature,
26
+ api_key=config.api_key,
27
+ max_tokens=config.max_tokens,
28
+ top_p=config.top_p,
29
+ top_k=config.top_k,
30
+ enable_vision=config.enable_vision,
31
+ vision_details=config.vision_details,
32
+ http_client_proxies=config.http_client,
33
+ )
34
+
35
+ super().__init__(config)
36
+
37
+ if not self.config.model:
38
+ self.config.model = "gpt-4.1-nano-2025-04-14"
39
+
40
+ if os.environ.get("OPENROUTER_API_KEY"): # Use OpenRouter
41
+ self.client = OpenAI(
42
+ api_key=os.environ.get("OPENROUTER_API_KEY"),
43
+ base_url=self.config.openrouter_base_url
44
+ or os.getenv("OPENROUTER_API_BASE")
45
+ or "https://openrouter.ai/api/v1",
46
+ )
47
+ else:
48
+ api_key = self.config.api_key or os.getenv("OPENAI_API_KEY")
49
+ base_url = self.config.openai_base_url or os.getenv("OPENAI_BASE_URL") or "https://api.openai.com/v1"
50
+
51
+ self.client = OpenAI(api_key=api_key, base_url=base_url)
52
+
53
+ def _parse_response(self, response, tools):
54
+ """
55
+ Process the response based on whether tools are used or not.
56
+
57
+ Args:
58
+ response: The raw response from API.
59
+ tools: The list of tools provided in the request.
60
+
61
+ Returns:
62
+ str or dict: The processed response.
63
+ """
64
+ if tools:
65
+ processed_response = {
66
+ "content": response.choices[0].message.content,
67
+ "tool_calls": [],
68
+ }
69
+
70
+ if response.choices[0].message.tool_calls:
71
+ for tool_call in response.choices[0].message.tool_calls:
72
+ processed_response["tool_calls"].append(
73
+ {
74
+ "name": tool_call.function.name,
75
+ "arguments": json.loads(extract_json(tool_call.function.arguments)),
76
+ }
77
+ )
78
+
79
+ return processed_response
80
+ else:
81
+ return response.choices[0].message.content
82
+
83
+ def generate_response(
84
+ self,
85
+ messages: List[Dict[str, str]],
86
+ response_format=None,
87
+ tools: Optional[List[Dict]] = None,
88
+ tool_choice: str = "auto",
89
+ **kwargs,
90
+ ):
91
+ """
92
+ Generate a JSON response based on the given messages using OpenAI.
93
+
94
+ Args:
95
+ messages (list): List of message dicts containing 'role' and 'content'.
96
+ response_format (str or object, optional): Format of the response. Defaults to "text".
97
+ tools (list, optional): List of tools that the model can call. Defaults to None.
98
+ tool_choice (str, optional): Tool choice method. Defaults to "auto".
99
+ **kwargs: Additional OpenAI-specific parameters.
100
+
101
+ Returns:
102
+ json: The generated response.
103
+ """
104
+ params = self._get_supported_params(messages=messages, **kwargs)
105
+
106
+ params.update({
107
+ "model": self.config.model,
108
+ "messages": messages,
109
+ })
110
+
111
+ if os.getenv("OPENROUTER_API_KEY"):
112
+ openrouter_params = {}
113
+ if self.config.models:
114
+ openrouter_params["models"] = self.config.models
115
+ openrouter_params["route"] = self.config.route
116
+ params.pop("model")
117
+
118
+ if self.config.site_url and self.config.app_name:
119
+ extra_headers = {
120
+ "HTTP-Referer": self.config.site_url,
121
+ "X-Title": self.config.app_name,
122
+ }
123
+ openrouter_params["extra_headers"] = extra_headers
124
+
125
+ params.update(**openrouter_params)
126
+
127
+ else:
128
+ openai_specific_generation_params = ["store"]
129
+ for param in openai_specific_generation_params:
130
+ if hasattr(self.config, param):
131
+ params[param] = getattr(self.config, param)
132
+
133
+ if response_format:
134
+ params["response_format"] = response_format
135
+ if tools: # TODO: Remove tools if no issues found with new memory addition logic
136
+ params["tools"] = tools
137
+ params["tool_choice"] = tool_choice
138
+ response = self.client.chat.completions.create(**params)
139
+ parsed_response = self._parse_response(response, tools)
140
+ if self.config.response_callback:
141
+ try:
142
+ self.config.response_callback(self, response, params)
143
+ except Exception as e:
144
+ # Log error but don't propagate
145
+ logging.error(f"Error due to callback: {e}")
146
+ pass
147
+ return parsed_response
@@ -0,0 +1,52 @@
1
+ import os
2
+ from typing import Dict, List, Optional
3
+
4
+ from openai import OpenAI
5
+
6
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
7
+ from agentrun_mem0.llms.base import LLMBase
8
+
9
+
10
+ class OpenAIStructuredLLM(LLMBase):
11
+ def __init__(self, config: Optional[BaseLlmConfig] = None):
12
+ super().__init__(config)
13
+
14
+ if not self.config.model:
15
+ self.config.model = "gpt-4o-2024-08-06"
16
+
17
+ api_key = self.config.api_key or os.getenv("OPENAI_API_KEY")
18
+ base_url = self.config.openai_base_url or os.getenv("OPENAI_API_BASE") or "https://api.openai.com/v1"
19
+ self.client = OpenAI(api_key=api_key, base_url=base_url)
20
+
21
+ def generate_response(
22
+ self,
23
+ messages: List[Dict[str, str]],
24
+ response_format: Optional[str] = None,
25
+ tools: Optional[List[Dict]] = None,
26
+ tool_choice: str = "auto",
27
+ ) -> str:
28
+ """
29
+ Generate a response based on the given messages using OpenAI.
30
+
31
+ Args:
32
+ messages (List[Dict[str, str]]): A list of dictionaries, each containing a 'role' and 'content' key.
33
+ response_format (Optional[str]): The desired format of the response. Defaults to None.
34
+
35
+
36
+ Returns:
37
+ str: The generated response.
38
+ """
39
+ params = {
40
+ "model": self.config.model,
41
+ "messages": messages,
42
+ "temperature": self.config.temperature,
43
+ }
44
+
45
+ if response_format:
46
+ params["response_format"] = response_format
47
+ if tools:
48
+ params["tools"] = tools
49
+ params["tool_choice"] = tool_choice
50
+
51
+ response = self.client.beta.chat.completions.parse(**params)
52
+ return response.choices[0].message.content
@@ -0,0 +1,89 @@
1
+ import os
2
+ from typing import Dict, List, Optional
3
+
4
+ import requests
5
+
6
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
7
+ from agentrun_mem0.llms.base import LLMBase
8
+
9
+
10
+ class SarvamLLM(LLMBase):
11
+ def __init__(self, config: Optional[BaseLlmConfig] = None):
12
+ super().__init__(config)
13
+
14
+ # Set default model if not provided
15
+ if not self.config.model:
16
+ self.config.model = "sarvam-m"
17
+
18
+ # Get API key from config or environment variable
19
+ self.api_key = self.config.api_key or os.getenv("SARVAM_API_KEY")
20
+
21
+ if not self.api_key:
22
+ raise ValueError(
23
+ "Sarvam API key is required. Set SARVAM_API_KEY environment variable or provide api_key in config."
24
+ )
25
+
26
+ # Set base URL - use config value or environment or default
27
+ self.base_url = (
28
+ getattr(self.config, "sarvam_base_url", None) or os.getenv("SARVAM_API_BASE") or "https://api.sarvam.ai/v1"
29
+ )
30
+
31
+ def generate_response(self, messages: List[Dict[str, str]], response_format=None) -> str:
32
+ """
33
+ Generate a response based on the given messages using Sarvam-M.
34
+
35
+ Args:
36
+ messages (list): List of message dicts containing 'role' and 'content'.
37
+ response_format (str or object, optional): Format of the response.
38
+ Currently not used by Sarvam API.
39
+
40
+ Returns:
41
+ str: The generated response.
42
+ """
43
+ url = f"{self.base_url}/chat/completions"
44
+
45
+ headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
46
+
47
+ # Prepare the request payload
48
+ params = {
49
+ "messages": messages,
50
+ "model": self.config.model if isinstance(self.config.model, str) else "sarvam-m",
51
+ }
52
+
53
+ # Add standard parameters that already exist in BaseLlmConfig
54
+ if self.config.temperature is not None:
55
+ params["temperature"] = self.config.temperature
56
+
57
+ if self.config.max_tokens is not None:
58
+ params["max_tokens"] = self.config.max_tokens
59
+
60
+ if self.config.top_p is not None:
61
+ params["top_p"] = self.config.top_p
62
+
63
+ # Handle Sarvam-specific parameters if model is passed as dict
64
+ if isinstance(self.config.model, dict):
65
+ # Extract model name
66
+ params["model"] = self.config.model.get("name", "sarvam-m")
67
+
68
+ # Add Sarvam-specific parameters
69
+ sarvam_specific_params = ["reasoning_effort", "frequency_penalty", "presence_penalty", "seed", "stop", "n"]
70
+
71
+ for param in sarvam_specific_params:
72
+ if param in self.config.model:
73
+ params[param] = self.config.model[param]
74
+
75
+ try:
76
+ response = requests.post(url, headers=headers, json=params, timeout=30)
77
+ response.raise_for_status()
78
+
79
+ result = response.json()
80
+
81
+ if "choices" in result and len(result["choices"]) > 0:
82
+ return result["choices"][0]["message"]["content"]
83
+ else:
84
+ raise ValueError("No response choices found in Sarvam API response")
85
+
86
+ except requests.exceptions.RequestException as e:
87
+ raise RuntimeError(f"Sarvam API request failed: {e}")
88
+ except KeyError as e:
89
+ raise ValueError(f"Unexpected response format from Sarvam API: {e}")
@@ -0,0 +1,88 @@
1
+ import json
2
+ import os
3
+ from typing import Dict, List, Optional
4
+
5
+ try:
6
+ from together import Together
7
+ except ImportError:
8
+ raise ImportError("The 'together' library is required. Please install it using 'pip install together'.")
9
+
10
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
11
+ from agentrun_mem0.llms.base import LLMBase
12
+ from agentrun_mem0.memory.utils import extract_json
13
+
14
+
15
+ class TogetherLLM(LLMBase):
16
+ def __init__(self, config: Optional[BaseLlmConfig] = None):
17
+ super().__init__(config)
18
+
19
+ if not self.config.model:
20
+ self.config.model = "mistralai/Mixtral-8x7B-Instruct-v0.1"
21
+
22
+ api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY")
23
+ self.client = Together(api_key=api_key)
24
+
25
+ def _parse_response(self, response, tools):
26
+ """
27
+ Process the response based on whether tools are used or not.
28
+
29
+ Args:
30
+ response: The raw response from API.
31
+ tools: The list of tools provided in the request.
32
+
33
+ Returns:
34
+ str or dict: The processed response.
35
+ """
36
+ if tools:
37
+ processed_response = {
38
+ "content": response.choices[0].message.content,
39
+ "tool_calls": [],
40
+ }
41
+
42
+ if response.choices[0].message.tool_calls:
43
+ for tool_call in response.choices[0].message.tool_calls:
44
+ processed_response["tool_calls"].append(
45
+ {
46
+ "name": tool_call.function.name,
47
+ "arguments": json.loads(extract_json(tool_call.function.arguments)),
48
+ }
49
+ )
50
+
51
+ return processed_response
52
+ else:
53
+ return response.choices[0].message.content
54
+
55
+ def generate_response(
56
+ self,
57
+ messages: List[Dict[str, str]],
58
+ response_format=None,
59
+ tools: Optional[List[Dict]] = None,
60
+ tool_choice: str = "auto",
61
+ ):
62
+ """
63
+ Generate a response based on the given messages using TogetherAI.
64
+
65
+ Args:
66
+ messages (list): List of message dicts containing 'role' and 'content'.
67
+ response_format (str or object, optional): Format of the response. Defaults to "text".
68
+ tools (list, optional): List of tools that the model can call. Defaults to None.
69
+ tool_choice (str, optional): Tool choice method. Defaults to "auto".
70
+
71
+ Returns:
72
+ str: The generated response.
73
+ """
74
+ params = {
75
+ "model": self.config.model,
76
+ "messages": messages,
77
+ "temperature": self.config.temperature,
78
+ "max_tokens": self.config.max_tokens,
79
+ "top_p": self.config.top_p,
80
+ }
81
+ if response_format:
82
+ params["response_format"] = response_format
83
+ if tools: # TODO: Remove tools if no issues found with new memory addition logic
84
+ params["tools"] = tools
85
+ params["tool_choice"] = tool_choice
86
+
87
+ response = self.client.chat.completions.create(**params)
88
+ return self._parse_response(response, tools)
@@ -0,0 +1,107 @@
1
+ import json
2
+ import os
3
+ from typing import Dict, List, Optional, Union
4
+
5
+ from openai import OpenAI
6
+
7
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
8
+ from agentrun_mem0.configs.llms.vllm import VllmConfig
9
+ from agentrun_mem0.llms.base import LLMBase
10
+ from agentrun_mem0.memory.utils import extract_json
11
+
12
+
13
+ class VllmLLM(LLMBase):
14
+ def __init__(self, config: Optional[Union[BaseLlmConfig, VllmConfig, Dict]] = None):
15
+ # Convert to VllmConfig if needed
16
+ if config is None:
17
+ config = VllmConfig()
18
+ elif isinstance(config, dict):
19
+ config = VllmConfig(**config)
20
+ elif isinstance(config, BaseLlmConfig) and not isinstance(config, VllmConfig):
21
+ # Convert BaseLlmConfig to VllmConfig
22
+ config = VllmConfig(
23
+ model=config.model,
24
+ temperature=config.temperature,
25
+ api_key=config.api_key,
26
+ max_tokens=config.max_tokens,
27
+ top_p=config.top_p,
28
+ top_k=config.top_k,
29
+ enable_vision=config.enable_vision,
30
+ vision_details=config.vision_details,
31
+ http_client_proxies=config.http_client,
32
+ )
33
+
34
+ super().__init__(config)
35
+
36
+ if not self.config.model:
37
+ self.config.model = "Qwen/Qwen2.5-32B-Instruct"
38
+
39
+ self.config.api_key = self.config.api_key or os.getenv("VLLM_API_KEY") or "vllm-api-key"
40
+ base_url = self.config.vllm_base_url or os.getenv("VLLM_BASE_URL")
41
+ self.client = OpenAI(api_key=self.config.api_key, base_url=base_url)
42
+
43
+ def _parse_response(self, response, tools):
44
+ """
45
+ Process the response based on whether tools are used or not.
46
+
47
+ Args:
48
+ response: The raw response from API.
49
+ tools: The list of tools provided in the request.
50
+
51
+ Returns:
52
+ str or dict: The processed response.
53
+ """
54
+ if tools:
55
+ processed_response = {
56
+ "content": response.choices[0].message.content,
57
+ "tool_calls": [],
58
+ }
59
+
60
+ if response.choices[0].message.tool_calls:
61
+ for tool_call in response.choices[0].message.tool_calls:
62
+ processed_response["tool_calls"].append(
63
+ {
64
+ "name": tool_call.function.name,
65
+ "arguments": json.loads(extract_json(tool_call.function.arguments)),
66
+ }
67
+ )
68
+
69
+ return processed_response
70
+ else:
71
+ return response.choices[0].message.content
72
+
73
+ def generate_response(
74
+ self,
75
+ messages: List[Dict[str, str]],
76
+ response_format=None,
77
+ tools: Optional[List[Dict]] = None,
78
+ tool_choice: str = "auto",
79
+ **kwargs,
80
+ ):
81
+ """
82
+ Generate a response based on the given messages using vLLM.
83
+
84
+ Args:
85
+ messages (list): List of message dicts containing 'role' and 'content'.
86
+ response_format (str or object, optional): Format of the response. Defaults to "text".
87
+ tools (list, optional): List of tools that the model can call. Defaults to None.
88
+ tool_choice (str, optional): Tool choice method. Defaults to "auto".
89
+ **kwargs: Additional vLLM-specific parameters.
90
+
91
+ Returns:
92
+ str: The generated response.
93
+ """
94
+ params = self._get_supported_params(messages=messages, **kwargs)
95
+ params.update(
96
+ {
97
+ "model": self.config.model,
98
+ "messages": messages,
99
+ }
100
+ )
101
+
102
+ if tools:
103
+ params["tools"] = tools
104
+ params["tool_choice"] = tool_choice
105
+
106
+ response = self.client.chat.completions.create(**params)
107
+ return self._parse_response(response, tools)
@@ -0,0 +1,52 @@
1
+ import os
2
+ from typing import Dict, List, Optional
3
+
4
+ from openai import OpenAI
5
+
6
+ from agentrun_mem0.configs.llms.base import BaseLlmConfig
7
+ from agentrun_mem0.llms.base import LLMBase
8
+
9
+
10
+ class XAILLM(LLMBase):
11
+ def __init__(self, config: Optional[BaseLlmConfig] = None):
12
+ super().__init__(config)
13
+
14
+ if not self.config.model:
15
+ self.config.model = "grok-2-latest"
16
+
17
+ api_key = self.config.api_key or os.getenv("XAI_API_KEY")
18
+ base_url = self.config.xai_base_url or os.getenv("XAI_API_BASE") or "https://api.x.ai/v1"
19
+ self.client = OpenAI(api_key=api_key, base_url=base_url)
20
+
21
+ def generate_response(
22
+ self,
23
+ messages: List[Dict[str, str]],
24
+ response_format=None,
25
+ tools: Optional[List[Dict]] = None,
26
+ tool_choice: str = "auto",
27
+ ):
28
+ """
29
+ Generate a response based on the given messages using XAI.
30
+
31
+ Args:
32
+ messages (list): List of message dicts containing 'role' and 'content'.
33
+ response_format (str or object, optional): Format of the response. Defaults to "text".
34
+ tools (list, optional): List of tools that the model can call. Defaults to None.
35
+ tool_choice (str, optional): Tool choice method. Defaults to "auto".
36
+
37
+ Returns:
38
+ str: The generated response.
39
+ """
40
+ params = {
41
+ "model": self.config.model,
42
+ "messages": messages,
43
+ "temperature": self.config.temperature,
44
+ "max_tokens": self.config.max_tokens,
45
+ "top_p": self.config.top_p,
46
+ }
47
+
48
+ if response_format:
49
+ params["response_format"] = response_format
50
+
51
+ response = self.client.chat.completions.create(**params)
52
+ return response.choices[0].message.content
File without changes