LiamKhoaLe commited on
Commit
aa55081
·
1 Parent(s): 830acbf

Upd imports

Browse files
memory/memory.py CHANGED
@@ -10,8 +10,8 @@ import logging
10
  from models.summarizer import summarizer
11
 
12
  _LLM_SMALL = "gemini-2.5-flash-lite-preview-06-17"
13
- # Load embedding model
14
- EMBED = SentenceTransformer("/app/model_cache", device="cpu").half()
15
  logger = logging.getLogger("rag-agent")
16
  logging.basicConfig(level=logging.INFO, format="%(asctime)s — %(name)s — %(levelname)s — %(message)s", force=True) # Change INFO to DEBUG for full-ctx JSON loader
17
 
 
10
  from models.summarizer import summarizer
11
 
12
  _LLM_SMALL = "gemini-2.5-flash-lite-preview-06-17"
13
+ # Load embedding model - use standard model that downloads automatically
14
+ EMBED = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
15
  logger = logging.getLogger("rag-agent")
16
  logging.basicConfig(level=logging.INFO, format="%(asctime)s — %(name)s — %(levelname)s — %(message)s", force=True) # Change INFO to DEBUG for full-ctx JSON loader
17
 
models/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (351 Bytes)
 
models/__pycache__/llama.cpython-311.pyc CHANGED
Binary files a/models/__pycache__/llama.cpython-311.pyc and b/models/__pycache__/llama.cpython-311.pyc differ
 
models/__pycache__/summarizer.cpython-311.pyc CHANGED
Binary files a/models/__pycache__/summarizer.cpython-311.pyc and b/models/__pycache__/summarizer.cpython-311.pyc differ
 
models/llama.py CHANGED
@@ -11,7 +11,8 @@ class NVIDIALLamaClient:
11
  def __init__(self):
12
  self.api_key = os.getenv("NVIDIA_URI")
13
  if not self.api_key:
14
- raise ValueError("NVIDIA_URI environment variable not set")
 
15
 
16
  # Correct NVIDIA Integrate API base
17
  self.base_url = "https://integrate.api.nvidia.com/v1"
@@ -19,11 +20,15 @@ class NVIDIALLamaClient:
19
 
20
  def generate_keywords(self, user_query: str) -> List[str]:
21
  """Use Llama to generate search keywords from user query"""
 
 
 
 
22
  try:
23
- prompt = f"""Given this medical question: "{user_query}"
24
 
25
- Generate 3-5 specific search keywords that would help find relevant medical information online.
26
- Focus on medical terms, symptoms, conditions, treatments, or procedures mentioned.
27
  Return only the keywords separated by commas, no explanations.
28
 
29
  Keywords:"""
@@ -37,7 +42,30 @@ Keywords:"""
37
 
38
  except Exception as e:
39
  logger.error(f"Failed to generate keywords: {e}")
40
- return [user_query] # Fallback to original query
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def summarize_documents(self, documents: List[Dict], user_query: str) -> Tuple[str, Dict[int, str]]:
43
  """Use Llama to summarize documents and return summary with URL mapping"""
 
11
  def __init__(self):
12
  self.api_key = os.getenv("NVIDIA_URI")
13
  if not self.api_key:
14
+ logger.warning("NVIDIA_URI not set - summarization will use fallback methods")
15
+ self.api_key = None
16
 
17
  # Correct NVIDIA Integrate API base
18
  self.base_url = "https://integrate.api.nvidia.com/v1"
 
20
 
21
  def generate_keywords(self, user_query: str) -> List[str]:
22
  """Use Llama to generate search keywords from user query"""
23
+ if not self.api_key:
24
+ # Fallback: extract keywords from query
25
+ return self._extract_keywords_fallback(user_query)
26
+
27
  try:
28
+ prompt = f"""Given this cooking question: "{user_query}"
29
 
30
+ Generate 3-5 specific search keywords that would help find relevant cooking information online.
31
+ Focus on cooking terms, ingredients, techniques, recipes, or culinary methods mentioned.
32
  Return only the keywords separated by commas, no explanations.
33
 
34
  Keywords:"""
 
42
 
43
  except Exception as e:
44
  logger.error(f"Failed to generate keywords: {e}")
45
+ return self._extract_keywords_fallback(user_query)
46
+
47
+ def _extract_keywords_fallback(self, user_query: str) -> List[str]:
48
+ """Fallback keyword extraction when NVIDIA API is not available"""
49
+ # Simple keyword extraction from cooking terms
50
+ cooking_keywords = [
51
+ 'recipe', 'cooking', 'baking', 'roasting', 'grilling', 'frying', 'boiling', 'steaming',
52
+ 'ingredients', 'seasoning', 'spices', 'herbs', 'sauce', 'marinade', 'dressing',
53
+ 'technique', 'method', 'temperature', 'timing', 'preparation', 'cooking time',
54
+ 'oven', 'stovetop', 'grill', 'pan', 'pot', 'skillet', 'knife', 'cutting',
55
+ 'vegetarian', 'vegan', 'gluten-free', 'dairy-free', 'keto', 'paleo', 'diet',
56
+ 'appetizer', 'main course', 'dessert', 'breakfast', 'lunch', 'dinner',
57
+ 'cuisine', 'italian', 'chinese', 'mexican', 'french', 'indian', 'thai'
58
+ ]
59
+
60
+ query_lower = user_query.lower()
61
+ found_keywords = [kw for kw in cooking_keywords if kw in query_lower]
62
+
63
+ # If no cooking keywords found, use first few words
64
+ if not found_keywords:
65
+ words = user_query.split()[:5]
66
+ found_keywords = [w for w in words if len(w) > 2]
67
+
68
+ return found_keywords[:5] # Limit to 5 keywords
69
 
70
  def summarize_documents(self, documents: List[Dict], user_query: str) -> Tuple[str, Dict[int, str]]:
71
  """Use Llama to summarize documents and return summary with URL mapping"""
models/summarizer.py CHANGED
@@ -7,7 +7,11 @@ logger = logging.getLogger(__name__)
7
 
8
  class TextSummarizer:
9
  def __init__(self):
10
- self.llama_client = NVIDIALLamaClient()
 
 
 
 
11
 
12
  def clean_text(self, text: str) -> str:
13
  """Clean and normalize text for summarization"""
@@ -61,6 +65,9 @@ class TextSummarizer:
61
 
62
  def summarize_text(self, text: str, max_length: int = 200) -> str:
63
  """Summarize text using NVIDIA Llama model"""
 
 
 
64
  try:
65
  if not text or len(text.strip()) < 50:
66
  return text
@@ -94,13 +101,34 @@ Summary:"""
94
 
95
  except Exception as e:
96
  logger.error(f"Summarization failed: {e}")
97
- # Fallback to simple truncation
98
- return self.clean_text(text)[:max_length]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  def summarize_for_query(self, text: str, query: str, max_length: int = 220) -> str:
101
  """Summarize text focusing strictly on information relevant to the query.
102
  Returns an empty string if nothing relevant is found.
103
  """
 
 
 
104
  try:
105
  if not text:
106
  return ""
@@ -125,7 +153,41 @@ Summary:"""
125
  return summary
126
  except Exception as e:
127
  logger.warning(f"Query-focused summarization failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  def summarize_documents(self, documents: List[Dict], user_query: str) -> Tuple[str, Dict[int, str]]:
131
  """Summarize multiple documents with URL mapping"""
 
7
 
8
  class TextSummarizer:
9
  def __init__(self):
10
+ try:
11
+ self.llama_client = NVIDIALLamaClient()
12
+ except Exception as e:
13
+ logger.warning(f"Failed to initialize NVIDIA Llama client: {e}")
14
+ self.llama_client = None
15
 
16
  def clean_text(self, text: str) -> str:
17
  """Clean and normalize text for summarization"""
 
65
 
66
  def summarize_text(self, text: str, max_length: int = 200) -> str:
67
  """Summarize text using NVIDIA Llama model"""
68
+ if not self.llama_client:
69
+ return self._summarize_fallback(text, max_length)
70
+
71
  try:
72
  if not text or len(text.strip()) < 50:
73
  return text
 
101
 
102
  except Exception as e:
103
  logger.error(f"Summarization failed: {e}")
104
+ return self._summarize_fallback(text, max_length)
105
+
106
+ def _summarize_fallback(self, text: str, max_length: int = 200) -> str:
107
+ """Fallback summarization when NVIDIA API is not available"""
108
+ if not text:
109
+ return ""
110
+
111
+ cleaned_text = self.clean_text(text)
112
+ if len(cleaned_text) <= max_length:
113
+ return cleaned_text
114
+
115
+ # Simple truncation with sentence boundary detection
116
+ sentences = cleaned_text.split('. ')
117
+ result = ""
118
+ for sentence in sentences:
119
+ if len(result + sentence) > max_length:
120
+ break
121
+ result += sentence + ". "
122
+
123
+ return result.strip() or cleaned_text[:max_length] + "..."
124
 
125
  def summarize_for_query(self, text: str, query: str, max_length: int = 220) -> str:
126
  """Summarize text focusing strictly on information relevant to the query.
127
  Returns an empty string if nothing relevant is found.
128
  """
129
+ if not self.llama_client:
130
+ return self._summarize_for_query_fallback(text, query, max_length)
131
+
132
  try:
133
  if not text:
134
  return ""
 
153
  return summary
154
  except Exception as e:
155
  logger.warning(f"Query-focused summarization failed: {e}")
156
+ return self._summarize_for_query_fallback(text, query, max_length)
157
+
158
+ def _summarize_for_query_fallback(self, text: str, query: str, max_length: int = 220) -> str:
159
+ """Fallback query-focused summarization when NVIDIA API is not available"""
160
+ if not text:
161
+ return ""
162
+
163
+ cleaned_text = self.clean_text(text)
164
+ if not cleaned_text:
165
+ return ""
166
+
167
+ # Simple keyword matching for relevance
168
+ query_words = set(query.lower().split())
169
+ text_words = set(cleaned_text.lower().split())
170
+
171
+ # Check if there's any overlap
172
+ overlap = query_words.intersection(text_words)
173
+ if not overlap:
174
  return ""
175
+
176
+ # Return first few sentences that contain query words
177
+ sentences = cleaned_text.split('. ')
178
+ relevant_sentences = []
179
+ for sentence in sentences:
180
+ sentence_words = set(sentence.lower().split())
181
+ if query_words.intersection(sentence_words):
182
+ relevant_sentences.append(sentence)
183
+ if len('. '.join(relevant_sentences)) > max_length:
184
+ break
185
+
186
+ result = '. '.join(relevant_sentences)
187
+ if len(result) > max_length:
188
+ result = result[:max_length-3] + "..."
189
+
190
+ return result
191
 
192
  def summarize_documents(self, documents: List[Dict], user_query: str) -> Tuple[str, Dict[int, str]]:
193
  """Summarize multiple documents with URL mapping"""