KhwaishK commited on
Commit
b341489
·
verified ·
1 Parent(s): bfdcc17

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +16 -1
database.py CHANGED
@@ -45,17 +45,32 @@ from langchain_huggingface import HuggingFaceEmbeddings
45
  from langchain_chroma import Chroma
46
 
47
  def run_database_pipeline(text_chunks, text_sums, tables, table_sums, images, image_sums):
 
48
  embedding_function = HuggingFaceEmbeddings(
49
  model_name="sentence-transformers/all-MiniLM-L6-v2"
50
  )
51
 
 
52
  all_chunks = text_chunks + tables + images
53
  all_summaries = text_sums + table_sums + image_sums
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  db = Chroma.from_texts(
56
  texts=all_summaries,
57
  embedding=embedding_function,
58
- metadatas=[chunk.metadata for chunk in all_chunks]
59
  )
60
 
61
  return db.as_retriever()
 
45
  from langchain_chroma import Chroma
46
 
47
  def run_database_pipeline(text_chunks, text_sums, tables, table_sums, images, image_sums):
48
+ # 1. Initialize embedding engine
49
  embedding_function = HuggingFaceEmbeddings(
50
  model_name="sentence-transformers/all-MiniLM-L6-v2"
51
  )
52
 
53
+ # 2. Combine all data
54
  all_chunks = text_chunks + tables + images
55
  all_summaries = text_sums + table_sums + image_sums
56
 
57
+ # 3. CONVERSION FIX: Transform ElementMetadata objects into simple dicts
58
+ clean_metadatas = []
59
+ for chunk in all_chunks:
60
+ # Convert the object to a dictionary and ensure it's JSON serializable
61
+ # We use .to_dict() if available, or vars() as a fallback
62
+ metadata_dict = chunk.metadata.to_dict() if hasattr(chunk.metadata, 'to_dict') else vars(chunk.metadata)
63
+
64
+ # Cleanup: Remove non-serializable objects (like lists or custom types)
65
+ # Chroma/ChromaDB only likes strings, ints, floats, and bools
66
+ clean_entry = {k: str(v) for k, v in metadata_dict.items() if isinstance(v, (str, int, float, bool))}
67
+ clean_metadatas.append(clean_entry)
68
+
69
+ # 4. Import into Database
70
  db = Chroma.from_texts(
71
  texts=all_summaries,
72
  embedding=embedding_function,
73
+ metadatas=clean_metadatas
74
  )
75
 
76
  return db.as_retriever()