This repository was archived by the owner on Apr 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathingest.py
More file actions
255 lines (203 loc) · 7.03 KB
/
ingest.py
File metadata and controls
255 lines (203 loc) · 7.03 KB
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
import json
import os
import time
from datetime import datetime
import lancedb
import pandas as pd
from dotenv import load_dotenv
from lancedb.embeddings import get_registry
from lancedb.pydantic import LanceModel, Vector
from langchain_text_splitters import (
MarkdownHeaderTextSplitter,
RecursiveCharacterTextSplitter,
)
def wait_for_index(table, index_name):
POLL_INTERVAL = 10
while True:
indices = table.list_indices()
if indices and any(index.name == index_name for index in indices):
break
print(f"⏳ Waiting for {index_name} to be ready...")
time.sleep(POLL_INTERVAL)
print(f"✅ {index_name} is ready!")
load_dotenv()
headers_to_split_on = [
("#", "header_1"),
("##", "header_2"),
("###", "header_3"),
("####", "header_4"),
("#####", "header_5"),
("######", "header_6"),
]
markdown_splitter = MarkdownHeaderTextSplitter(
headers_to_split_on=headers_to_split_on, strip_headers=False
)
files = [
{
"path": "data/molecule_docs.json",
"source": "Molecule Documentation",
},
{
"path": "data/molecule_blog.json",
"source": "Molecule Blog",
},
{
"path": "data/desci_codes.json",
"source": "DeSci.Codes",
},
]
parsed_data = []
for file in files:
# Open and parse the JSON file
with open(file["path"], "r", encoding="utf-8") as f:
data = json.load(f)
# Extract URL and title from each item
for item in data:
title = item.get("title", "")
content = item.get("markdown", "")
url = item.get("url", "N/A")
metadata = {"url": url, "source": file["source"]}
parsed_data.append({"title": title, "content": content, "metadata": metadata})
# Create a pandas DataFrame
df = pd.DataFrame(parsed_data)
chunk_size = 2000
chunk_overlap = 200
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size, chunk_overlap=chunk_overlap
)
chunks = []
for i, row in df.iterrows():
md_header_splits = markdown_splitter.split_text(row["content"])
splits = text_splitter.split_documents(md_header_splits)
for i, split in enumerate(splits):
split.metadata["page_title"] = row["title"]
split.metadata["url"] = row["metadata"]["url"]
split.metadata["source"] = row["metadata"]["source"]
chunks.append(split)
print(f"Number of chunks: {len(chunks)}")
# print(chunks[0])
# --------------------------------------------------------------
# Create a LanceDB database and table
# --------------------------------------------------------------
# Create a LanceDB database
db = lancedb.connect(
"s3://mol-mira-v0",
storage_options={
"aws_access_key_id": os.getenv("DO_SPACES_ACCESS_KEY_ID"),
"aws_secret_access_key": os.getenv("DO_SPACES_SECRET_ACCESS_KEY"),
"aws_endpoint": "https://fra1.digitaloceanspaces.com",
"aws_region": "fra1",
},
)
# Get the OpenAI embedding function
func = get_registry().get("openai").create(name="text-embedding-3-large")
# Define a simplified metadata schema
class ChunkMetadata(LanceModel):
"""
You must order the fields in alphabetical order.
This is a requirement of the Pydantic implementation.
"""
header_1: str | None
header_2: str | None
header_3: str | None
header_4: str | None
page_title: str | None
source: str | None
url: str | None
# Define the config table schema
class Config(LanceModel):
key: str
value: str
# Define the main Schema
class Chunks(LanceModel):
text: str = func.SourceField()
vector: Vector(func.ndims()) = func.VectorField() # type: ignore
metadata: ChunkMetadata
table = db.create_table("molrag", schema=Chunks, mode="overwrite")
# --------------------------------------------------------------
# Create and populate config table
# --------------------------------------------------------------
# Check if config table exists, if not create it
try:
config_table = db.open_table("config")
except Exception:
config_table = db.create_table("config", schema=Config)
# Upsert knowledge_version entry
config_data = [
{"key": "knowledge_version", "value": datetime.now().strftime("%Y-%m-%d")},
]
# First, try to delete existing entry with the same key (if any)
try:
config_table.delete("key = 'knowledge_version'")
except Exception:
pass # Key might not exist yet
# Add the new entry
config_table.add(config_data)
print(
f"✅ Config table created and knowledge_version set to '{datetime.now().strftime('%Y-%m-%d')}'"
)
# --------------------------------------------------------------
# Prepare the chunks for the table
# --------------------------------------------------------------
# Create table with processed chunks
processed_chunks = [
{
"text": chunk.page_content,
"metadata": {
"header_1": chunk.metadata["header_1"]
if "header_1" in chunk.metadata
else None,
"header_2": chunk.metadata["header_2"]
if "header_2" in chunk.metadata
else None,
"header_3": chunk.metadata["header_3"]
if "header_3" in chunk.metadata
else None,
"header_4": chunk.metadata["header_4"]
if "header_4" in chunk.metadata
else None,
"page_title": chunk.metadata["page_title"],
"source": chunk.metadata["source"],
"url": chunk.metadata["url"],
},
}
for chunk in chunks
]
# --------------------------------------------------------------
# Add the chunks to the table (automatically embeds the text)
# --------------------------------------------------------------
table.add(processed_chunks)
table.create_fts_index("text", replace=True, use_tantivy=False)
# Wait for indexes to be ready
# wait_for_index(table, "text_idx")
# Create index with cosine similarity
# Note: vector_column_name only needed for multiple vector columns or non-default names
# Supported index types: IVF_PQ (default) and IVF_HNSW_SQ
table.create_index(metric="cosine")
# --------------------------------------------------------------
# Load the table
# --------------------------------------------------------------
print("--- start table snippet ---")
print(table.to_pandas())
print("--- end table snippet ---")
print("DB rows: ", table.count_rows())
# --------------------------------------------------------------
# Verify config table entry
# --------------------------------------------------------------
# Query the config table to get the knowledge_version value
knowledge_version_result = (
config_table.search().where("key = 'knowledge_version'").to_pandas()
)
if not knowledge_version_result.empty:
knowledge_version_value = knowledge_version_result.iloc[0]["value"]
print(f"📋 Retrieved knowledge_version from DB: '{knowledge_version_value}'")
else:
print("⚠️ No knowledge_version found in config table")
# table = db.open_table("molrag")
# results = table.search(
# "ipnft",
# query_type="hybrid",
# vector_column_name="vector",
# fts_columns="text",
# )
# print(results.to_pandas())