fix(python): huggingface TEI auth, procedural-memory content handling, and proxy pip auto-install (#6947)

This commit is contained in:
Kartik
2026-08-20 15:56:55 +05:30
committed by GitHub
parent 530d802b55
commit ed38ddf873
11 changed files with 264 additions and 18 deletions
+3 -1
View File
@@ -1,4 +1,5 @@
import logging
import os
from typing import Literal, Optional
from openai import OpenAI
@@ -17,7 +18,8 @@ class HuggingFaceEmbedding(EmbeddingBase):
super().__init__(config)
if self.config.huggingface_base_url:
self.client = OpenAI(base_url=self.config.huggingface_base_url, api_key=self.config.api_key)
api_key = self.config.api_key or os.getenv("HUGGINGFACE_API_KEY") or "hf"
self.client = OpenAI(base_url=self.config.huggingface_base_url, api_key=api_key)
self.config.model = self.config.model or "tei"
else:
self.config.model = self.config.model or "multi-qa-MiniLM-L6-cos-v1"
+13 -1
View File
@@ -2017,6 +2017,12 @@ class Memory(MemoryBase):
logger.error(f"Error generating procedural memory summary: {e}")
raise
if not procedural_memory:
raise ValueError(
"The LLM returned no content for the procedural memory summary. "
"The model may have declined the request or returned an empty response."
)
if metadata is None:
raise ValueError("Metadata cannot be done for procedural memory.")
@@ -3701,11 +3707,17 @@ class AsyncMemory(MemoryBase):
else:
procedural_memory = await asyncio.to_thread(self.llm.generate_response, messages=parsed_messages)
procedural_memory = remove_code_blocks(procedural_memory)
except Exception as e:
logger.error(f"Error generating procedural memory summary: {e}")
raise
if not procedural_memory:
raise ValueError(
"The LLM returned no content for the procedural memory summary. "
"The model may have declined the request or returned an empty response."
)
if metadata is None:
raise ValueError("Metadata cannot be done for procedural memory.")
+9 -1
View File
@@ -121,7 +121,15 @@ def remove_code_blocks(content: str) -> str:
- If a code block is detected, it returns only the inner content, stripping out the markers.
- If no code block markers are found, the original content is returned as-is.
"""
if content is None:
if isinstance(content, list):
parts = []
for block in content:
if isinstance(block, str):
parts.append(block)
elif isinstance(block, dict):
parts.append(block.get("text", ""))
content = "".join(parts)
if not isinstance(content, str):
return ""
pattern = r"^```[a-zA-Z0-9]*\n([\s\S]*?)\n```$"
match = re.match(pattern, content.strip())
+1 -8
View File
@@ -1,6 +1,4 @@
import logging
import subprocess
import sys
import threading
from typing import List, Optional, Union
@@ -11,12 +9,7 @@ import mem0
try:
import litellm
except ImportError:
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)
raise ImportError("The 'litellm' library is required. Please install it using 'pip install litellm'.")
from mem0 import Memory, MemoryClient
from mem0.configs.prompts import MEMORY_ANSWER_PROMPT