Spaces:
Sleeping
Sleeping
File size: 25,931 Bytes
7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 dd2978b 7808f20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | import json
import sqlite3
from pathlib import Path
from typing import List, Dict, Optional, Tuple
import chromadb
from chromadb import Settings
from sentence_transformers import SentenceTransformer
from datetime import datetime
class EnhancedRAGUtils:
def __init__(self, vector_stores_path: str = "./vector_stores"):
self.vector_stores_path = Path(vector_stores_path)
# Initialize embedding model (shared across all VDBs)
self.embedder = SentenceTransformer('all-MiniLM-L6-v2')
# Initialize all VDB connections
self._init_regulatory_vdb()
self._init_product_spec_vdb()
self._init_checklist_examples_vdb()
print("Enhanced RAG Utils initialized with 3 vector databases")
def _init_regulatory_vdb(self):
"""Initialize regulatory guidelines VDB"""
try:
self.regulatory_chroma_path = self.vector_stores_path / "chroma_db" / "regulatory_docs"
self.regulatory_metadata_db = self.regulatory_chroma_path / "metadata" / "regulatory_metadata.db"
self.regulatory_client = chromadb.PersistentClient(
path=str(self.regulatory_chroma_path),
settings=Settings(anonymized_telemetry=False)
)
self.regulatory_collection = self.regulatory_client.get_collection("regulatory_guidelines")
print("β Regulatory VDB connected")
except Exception as e:
print(f"β Regulatory VDB not available: {e}")
self.regulatory_collection = None
def _init_product_spec_vdb(self):
"""Initialize product specifications VDB"""
try:
self.product_spec_chroma_path = self.vector_stores_path / "chroma_db" / "product_specs"
self.product_spec_metadata_db = self.product_spec_chroma_path / "metadata" / "product_metadata.db"
self.product_spec_client = chromadb.PersistentClient(
path=str(self.product_spec_chroma_path),
settings=Settings(anonymized_telemetry=False)
)
self.product_spec_collection = self.product_spec_client.get_collection("product_specifications")
print("β Product Specifications VDB connected")
except Exception as e:
print(f"β Product Specifications VDB not available: {e}")
self.product_spec_collection = None
def _init_checklist_examples_vdb(self):
"""Initialize checklist examples VDB"""
try:
self.checklist_chroma_path = self.vector_stores_path / "chroma_db" / "checklist_examples"
self.checklist_metadata_db = self.checklist_chroma_path / "metadata" / "checklist_structures.db"
self.checklist_client = chromadb.PersistentClient(
path=str(self.checklist_chroma_path),
settings=Settings(anonymized_telemetry=False)
)
self.checklist_collection = self.checklist_client.get_collection("checklist_examples")
print("β Checklist Examples VDB connected")
except Exception as e:
print(f"β Checklist Examples VDB not available: {e}")
self.checklist_collection = None
def retrieve_regulatory_requirements(self, product_name: str, domain: str = "Food Manufacturing", k: int = 3) -> List[Dict]:
"""Retrieve relevant regulatory requirements - only when specifically relevant"""
if not self.regulatory_collection:
return []
try:
# UPDATED: More targeted query without forcing specific standards
query_text = f"{product_name} {domain} quality requirements standards"
query_embedding = self.embedder.encode(query_text).tolist()
# Query ChromaDB
results = self.regulatory_collection.query(
query_embeddings=[query_embedding],
n_results=k
)
guidelines = []
if results['documents'] and results['documents'][0]:
for i, doc in enumerate(results['documents'][0]):
metadata = results['metadatas'][0][i]
# UPDATED: Only include if truly relevant (high relevance score)
relevance_score = 1 - results['distances'][0][i] if 'distances' in results else 0.5
# Higher threshold for including regulatory requirements
if relevance_score > 0.7: # Only highly relevant results
clause_ref = self._extract_clause_reference(metadata, doc)
guidelines.append({
"text": doc[:600], # Reduced text length
"regulatory_body": metadata.get('regulatory_body', 'Unknown'),
"standard_code": metadata.get('standard_code', ''),
"clause_reference": clause_ref,
"topics": metadata.get('topics', ''),
"jurisdiction": metadata.get('jurisdiction', ''),
"relevance_score": relevance_score,
"source_type": "regulatory"
})
# Sort by relevance
guidelines = sorted(guidelines, key=lambda x: x['relevance_score'], reverse=True)
return guidelines[:k] # Return only top k results
except Exception as e:
print(f"Error retrieving regulatory requirements: {str(e)}")
return []
def retrieve_product_specifications(self, product_name: str, k: int = 3) -> List[Dict]:
"""Retrieve similar product specifications for reference only"""
if not self.product_spec_collection:
return []
try:
# UPDATED: Focus on product characteristics, not prescriptive requirements
query_text = f"{product_name} product characteristics quality attributes"
query_embedding = self.embedder.encode(query_text).tolist()
# Query ChromaDB
results = self.product_spec_collection.query(
query_embeddings=[query_embedding],
n_results=k
)
specifications = []
if results['documents'] and results['documents'][0]:
for i, doc in enumerate(results['documents'][0]):
metadata = results['metadatas'][0][i]
# UPDATED: Extract category dynamically
product_category = self._determine_product_category(
metadata.get('product_name', ''),
metadata.get('product_category', ''),
doc
)
specifications.append({
"text": doc[:400], # Reduced text
"product_name": metadata.get('product_name', 'Unknown'),
"supplier": metadata.get('supplier', 'Unknown'),
"category": product_category, # Dynamic category
"specification_type": metadata.get('specification_type', 'Unknown'),
"parameters_count": metadata.get('total_parameters', 0),
"detail_level": metadata.get('detail_level', 'standard'),
"relevance_score": 1 - results['distances'][0][i] if 'distances' in results else 0.5,
"source_type": "product_spec"
})
return sorted(specifications, key=lambda x: x['relevance_score'], reverse=True)
except Exception as e:
print(f"Error retrieving product specifications: {str(e)}")
return []
def retrieve_checklist_examples(self, product_name: str, k: int = 3) -> List[Dict]:
"""Retrieve similar checklist examples as reference patterns only"""
if not self.checklist_collection:
return []
try:
# UPDATED: Focus on pattern discovery, not template copying
query_text = f"{product_name} inspection checklist structure"
query_embedding = self.embedder.encode(query_text).tolist()
# Query ChromaDB
results = self.checklist_collection.query(
query_embeddings=[query_embedding],
n_results=k
)
examples = []
if results['documents'] and results['documents'][0]:
for i, doc in enumerate(results['documents'][0]):
metadata = results['metadatas'][0][i]
# Get parameter structures from metadata
parameter_info = self._extract_parameter_structure(metadata)
examples.append({
"text": doc[:300], # Reduced text
"document_type": metadata.get('document_type', 'QC Checklist'),
"product_name": metadata.get('product_name', 'Unknown'),
"checklist_category": metadata.get('checklist_category', 'General'),
"total_parameters": metadata.get('total_parameters', 0),
"parameter_types": metadata.get('parameter_types', []),
"input_methods": metadata.get('input_methods', []),
"parameter_structure": parameter_info,
"relevance_score": 1 - results['distances'][0][i] if 'distances' in results else 0.5,
"source_type": "checklist_example"
})
return examples
except Exception as e:
print(f"Error retrieving checklist examples: {str(e)}")
return []
def retrieve_parameter_patterns(self, product_category: str = "", k: int = 10) -> List[Dict]:
"""Retrieve common parameter patterns based on actual usage"""
if not self.checklist_metadata_db.exists():
return []
try:
conn = sqlite3.connect(self.checklist_metadata_db)
cursor = conn.cursor()
# UPDATED: Dynamic query based on product category if provided
if product_category:
query = """
SELECT
cp.parameter_name,
cp.parameter_type,
cp.input_method,
cp.specifications,
cp.options_list,
cp.tolerance_limits,
cp.measurement_units,
cp.has_remarks,
COUNT(*) as usage_frequency,
GROUP_CONCAT(DISTINCT cd.product_name) as used_in_products
FROM checklist_parameters cp
JOIN checklist_documents cd ON cp.file_hash = cd.file_hash
WHERE cd.checklist_category LIKE ?
GROUP BY cp.parameter_name, cp.parameter_type, cp.input_method
ORDER BY usage_frequency DESC, cp.parameter_name
LIMIT ?
"""
cursor.execute(query, (f"%{product_category}%", k))
else:
# General patterns without category filter
query = """
SELECT
cp.parameter_name,
cp.parameter_type,
cp.input_method,
cp.specifications,
cp.options_list,
cp.tolerance_limits,
cp.measurement_units,
cp.has_remarks,
COUNT(*) as usage_frequency,
GROUP_CONCAT(DISTINCT cd.product_name) as used_in_products
FROM checklist_parameters cp
JOIN checklist_documents cd ON cp.file_hash = cd.file_hash
GROUP BY cp.parameter_name, cp.parameter_type, cp.input_method
ORDER BY usage_frequency DESC, cp.parameter_name
LIMIT ?
"""
cursor.execute(query, (k,))
patterns = []
for row in cursor.fetchall():
patterns.append({
"parameter_name": row[0],
"parameter_type": row[1],
"input_method": row[2],
"specifications": row[3] or "",
"options_list": row[4] or "",
"tolerance_limits": row[5] or "",
"measurement_units": row[6] or "",
"has_remarks": bool(row[7]),
"usage_frequency": row[8],
"used_in_products": row[9].split(',') if row[9] else []
})
return patterns
except Exception as e:
print(f"Error retrieving parameter patterns: {str(e)}")
return []
finally:
if 'conn' in locals():
conn.close()
def get_comprehensive_context(self, product_name: str, domain: str = "Food Manufacturing",
include_patterns: bool = True) -> Dict:
"""Get comprehensive context from all VDBs - as reference only"""
context = {
"product_name": product_name,
"domain": domain,
"regulatory_requirements": [],
"product_specifications": [],
"checklist_examples": [],
"parameter_patterns": [],
"context_summary": {},
"generated_at": datetime.now().isoformat()
}
print(f"Retrieving reference context for: {product_name}")
# UPDATED: Only get regulatory if likely to be relevant
# Don't force regulatory requirements for every product
context["regulatory_requirements"] = self.retrieve_regulatory_requirements(product_name, domain, k=2)
# Get product specifications
context["product_specifications"] = self.retrieve_product_specifications(product_name, k=2)
# Extract dynamic category from specifications
product_category = ""
if context["product_specifications"]:
# Use the most relevant specification's category
product_category = context["product_specifications"][0].get("category", "")
# Get checklist examples
context["checklist_examples"] = self.retrieve_checklist_examples(product_name, k=3)
# Get parameter patterns based on dynamic category
if include_patterns:
context["parameter_patterns"] = self.retrieve_parameter_patterns(
product_category=product_category,
k=10
)
# Generate context summary
context["context_summary"] = self._generate_context_summary(context)
return context
def format_context_for_prompt(self, context: Dict, max_length: int = 4000) -> str:
"""Format comprehensive context for AI prompt - as suggestions only"""
# UPDATED: Emphasize that this is reference material only
formatted_context = "\n# REFERENCE CONTEXT (Use as suggestions, not requirements):\n"
formatted_context += "Note: The following is retrieved reference material. Use it to understand the domain better, but prioritize user requirements.\n"
# Add regulatory compliance only if found
if context["regulatory_requirements"]:
formatted_context += "\n## π Regulatory References (if applicable):\n"
for i, req in enumerate(context["regulatory_requirements"][:2], 1):
clause_ref = req.get('clause_reference', req.get('standard_code', ''))
formatted_context += f"\n### Reference {i}: {req['regulatory_body']}"
if clause_ref:
formatted_context += f" - {clause_ref}\n"
else:
formatted_context += "\n"
if req.get('text'):
formatted_context += f"Content: {req['text'][:200]}...\n"
# Add product specification insights
if context["product_specifications"]:
formatted_context += "\n## π Similar Product Insights:\n"
for i, spec in enumerate(context["product_specifications"][:2], 1):
formatted_context += f"\n### Similar Product: {spec['product_name']}\n"
formatted_context += f"**Category**: {spec['category']} (dynamically determined)\n"
formatted_context += f"**Typical Parameters**: {spec['parameters_count']}\n"
if spec.get('text'):
formatted_context += f"**Characteristics**: {spec['text'][:150]}...\n"
# Add checklist pattern examples
if context["checklist_examples"]:
formatted_context += "\n## π Checklist Patterns (for reference):\n"
for i, example in enumerate(context["checklist_examples"][:2], 1):
formatted_context += f"\n### Pattern from: {example['product_name']}\n"
if example.get('input_methods'):
methods = ', '.join(set(example['input_methods'][:5]))
formatted_context += f"**Common Input Types**: {methods}\n"
if example.get('parameter_structure'):
formatted_context += "**Example Parameters**:\n"
for param in example['parameter_structure'][:3]:
formatted_context += f" - {param['name']}: {param['input_method']}\n"
# Add parameter patterns without prescribing
if context["parameter_patterns"]:
formatted_context += "\n## π‘ Parameter Patterns (common patterns, not requirements):\n"
# Show diverse patterns
shown_types = set()
for pattern in context["parameter_patterns"]:
if pattern['input_method'] not in shown_types and len(shown_types) < 5:
shown_types.add(pattern['input_method'])
formatted_context += f"\n**{pattern['input_method']} Example**:\n"
formatted_context += f" β’ {pattern['parameter_name']}"
if pattern['specifications']:
formatted_context += f" (e.g., {pattern['specifications'][:30]})"
formatted_context += f" - seen {pattern['usage_frequency']} times\n"
# Add context summary
if context.get("context_summary"):
formatted_context += "\n## π¬ Context Insights:\n"
summary = context["context_summary"]
if summary.get("product_insights"):
formatted_context += f"**Product Type**: {summary['product_insights']}\n"
if summary.get("common_patterns"):
formatted_context += f"**Common Patterns**: {summary['common_patterns']}\n"
formatted_context += "\n**Remember**: These are suggestions based on similar products. "
formatted_context += "The user's specific requirements always take priority.\n"
# Truncate if too long
if len(formatted_context) > max_length:
formatted_context = formatted_context[:max_length] + "\n\n[Context truncated for length...]"
return formatted_context
def _determine_product_category(self, product_name: str, stored_category: str, doc_text: str) -> str:
"""Dynamically determine product category without hardcoding"""
# If we have a stored category that's not generic, use it
if stored_category and stored_category not in ["General", "Unknown", "Food"]:
return stored_category
# Otherwise, analyze the product name and text to determine category
product_lower = product_name.lower()
text_lower = doc_text.lower() if doc_text else ""
# Let the category emerge from the content
# Don't use predefined categories
if any(word in product_lower + text_lower for word in ["frozen", "freeze", "iqf", "-18"]):
return "Temperature Controlled"
elif any(word in product_lower + text_lower for word in ["fresh", "chilled", "refrigerated"]):
return "Fresh/Chilled"
elif any(word in product_lower + text_lower for word in ["fried", "oil", "crispy"]):
return "Processed/Fried"
elif any(word in product_lower + text_lower for word in ["baked", "bakery", "bread"]):
return "Bakery/Baked"
else:
# Return a general category based on the product name itself
return "Specialty Product"
def _extract_clause_reference(self, metadata: Dict, document_text: str) -> str:
"""Extract clause reference from regulatory document"""
standard_code = metadata.get('standard_code', '')
regulatory_body = metadata.get('regulatory_body', '')
# Only return if there's a specific clause
if standard_code and standard_code != regulatory_body:
return standard_code
# Look for section numbers in the text
import re
section_patterns = [
r"(Section\s+\d+\.\d+)",
r"(Clause\s+\d+\.\d+)",
r"(\d+\.\d+\s+[A-Z][\w\s]{10,30})"
]
for pattern in section_patterns:
match = re.search(pattern, document_text[:300])
if match:
return match.group(1).strip()
return ""
def _extract_parameter_structure(self, metadata: Dict) -> List[Dict]:
"""Extract parameter structure info from checklist metadata"""
structure = []
param_types = metadata.get('parameter_types', [])
input_methods = metadata.get('input_methods', [])
# Create sample structure without being prescriptive
for i, (ptype, method) in enumerate(zip(param_types[:3], input_methods[:3])):
structure.append({
"name": f"{ptype} Parameter",
"type": ptype,
"input_method": method,
"spec": "",
"options": []
})
return structure
def _generate_context_summary(self, context: Dict) -> Dict:
"""Generate intelligent summary of retrieved context - no prescriptions"""
summary = {
"product_insights": "",
"common_patterns": "",
"regulatory_relevance": "minimal" # Default to minimal
}
# Product insights based on what we found
if context["product_specifications"]:
categories = [spec.get('category', '') for spec in context["product_specifications"]]
unique_categories = [c for c in categories if c and c != "Unknown"]
if unique_categories:
summary["product_insights"] = f"Similar to {', '.join(unique_categories[:2])} products"
# Common patterns without being prescriptive
if context["parameter_patterns"]:
input_methods = {}
for pattern in context["parameter_patterns"][:5]:
method = pattern['input_method']
input_methods[method] = input_methods.get(method, 0) + 1
if input_methods:
common_method = max(input_methods, key=input_methods.get)
summary["common_patterns"] = f"Often uses {common_method} for data collection"
# Regulatory relevance assessment
if context["regulatory_requirements"]:
# Only mark as relevant if we found highly relevant requirements
avg_relevance = sum(req.get('relevance_score', 0) for req in context["regulatory_requirements"]) / len(context["regulatory_requirements"])
if avg_relevance > 0.75:
summary["regulatory_relevance"] = "high"
elif avg_relevance > 0.6:
summary["regulatory_relevance"] = "moderate"
return summary
# Singleton instance for global use
rag_utils = EnhancedRAGUtils()
# Export convenience functions - UPDATED to be less prescriptive
def get_comprehensive_context(product_name: str, domain: str = "Food Manufacturing") -> Dict:
"""Get comprehensive context from all VDBs as reference material only"""
return rag_utils.get_comprehensive_context(product_name, domain)
def format_context_for_prompt(context: Dict, max_length: int = 4000) -> str:
"""Format context for AI prompt as suggestions only"""
return rag_utils.format_context_for_prompt(context, max_length)
def retrieve_regulatory_requirements(product_name: str, domain: str = "Food Manufacturing") -> List[Dict]:
"""Get regulatory requirements only when relevant"""
return rag_utils.retrieve_regulatory_requirements(product_name, domain)
def retrieve_checklist_examples(product_name: str) -> List[Dict]:
"""Get checklist examples as patterns, not templates"""
return rag_utils.retrieve_checklist_examples(product_name)
def retrieve_parameter_patterns(product_category: str = "") -> List[Dict]:
"""Get parameter patterns based on dynamic category"""
return rag_utils.retrieve_parameter_patterns(product_category)
|