User_id creation for client and formatting (#2264)
This commit is contained in:
@@ -6,12 +6,16 @@ from typing import Any, Dict, List, Optional, Union
|
|||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
from mem0.memory.setup import setup_config, get_user_id
|
||||||
from mem0.memory.telemetry import capture_client_event
|
from mem0.memory.telemetry import capture_client_event
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
warnings.filterwarnings("default", category=DeprecationWarning)
|
warnings.filterwarnings("default", category=DeprecationWarning)
|
||||||
|
|
||||||
|
# Setup user config
|
||||||
|
setup_config()
|
||||||
|
|
||||||
|
|
||||||
class APIError(Exception):
|
class APIError(Exception):
|
||||||
"""Exception raised for errors in the API."""
|
"""Exception raised for errors in the API."""
|
||||||
@@ -74,13 +78,14 @@ class MemoryClient:
|
|||||||
self.host = host or "https://api.mem0.ai"
|
self.host = host or "https://api.mem0.ai"
|
||||||
self.org_id = org_id
|
self.org_id = org_id
|
||||||
self.project_id = project_id
|
self.project_id = project_id
|
||||||
|
self.user_id = get_user_id()
|
||||||
|
|
||||||
if not self.api_key:
|
if not self.api_key:
|
||||||
raise ValueError("Mem0 API Key not provided. Please provide an API Key.")
|
raise ValueError("Mem0 API Key not provided. Please provide an API Key.")
|
||||||
|
|
||||||
self.client = httpx.Client(
|
self.client = httpx.Client(
|
||||||
base_url=self.host,
|
base_url=self.host,
|
||||||
headers={"Authorization": f"Token {self.api_key}"},
|
headers={"Authorization": f"Token {self.api_key}", "Mem0-User-ID": self.user_id},
|
||||||
timeout=300,
|
timeout=300,
|
||||||
)
|
)
|
||||||
self.user_email = self._validate_api_key()
|
self.user_email = self._validate_api_key()
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class LlmConfig(BaseModel):
|
|||||||
"azure_openai_structured",
|
"azure_openai_structured",
|
||||||
"gemini",
|
"gemini",
|
||||||
"deepseek",
|
"deepseek",
|
||||||
"xai"
|
"xai",
|
||||||
):
|
):
|
||||||
return v
|
return v
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -18,11 +18,7 @@ class XAILLM(LLMBase):
|
|||||||
base_url = self.config.xai_base_url or os.getenv("XAI_API_BASE") or "https://api.x.ai/v1"
|
base_url = self.config.xai_base_url or os.getenv("XAI_API_BASE") or "https://api.x.ai/v1"
|
||||||
self.client = OpenAI(api_key=api_key, base_url=base_url)
|
self.client = OpenAI(api_key=api_key, base_url=base_url)
|
||||||
|
|
||||||
def generate_response(
|
def generate_response(self, messages: List[Dict[str, str]], response_format=None):
|
||||||
self,
|
|
||||||
messages: List[Dict[str, str]],
|
|
||||||
response_format=None
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Generate a response based on the given messages using XAI.
|
Generate a response based on the given messages using XAI.
|
||||||
|
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ class Completions:
|
|||||||
api_key=api_key,
|
api_key=api_key,
|
||||||
model_list=model_list,
|
model_list=model_list,
|
||||||
)
|
)
|
||||||
capture_client_event("mem0.chat.create", self)
|
capture_client_event("mem0.chat.create", self.mem0_client)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _prepare_messages(self, messages: List[dict]) -> List[dict]:
|
def _prepare_messages(self, messages: List[dict]) -> List[dict]:
|
||||||
|
|||||||
Reference in New Issue
Block a user