Skip to content

Commit 4b3d9d9

Browse files
feat: adapt read, chunk, build_kg operators to new optypes
1 parent c26bd9d commit 4b3d9d9

3 files changed

Lines changed: 28 additions & 20 deletions

File tree

graphgen/graphgen.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ async def read(self, read_config: Dict):
125125
async def chunk(self, chunk_config: Dict, input_stream: Iterator):
126126
"""
127127
chunk documents into smaller pieces from full_docs_storage if not already present
128+
input_stream: document IDs from full_docs_storage
129+
yield: chunk IDs inserted into chunks_storage
128130
"""
129131
count = 0
130132
for doc_id in input_stream:
@@ -174,15 +176,22 @@ async def chunk(self, chunk_config: Dict, input_stream: Iterator):
174176
async def build_kg(self, inputs: List):
175177
"""
176178
build knowledge graph from text chunks
179+
inputs: chunk IDs from chunks_storage
180+
return: None
177181
"""
182+
count = 0
178183
# Step 1: get chunks
184+
inserting_chunks: Dict[str, Dict] = {}
185+
for _chunk_id in inputs:
186+
chunk = await self.chunks_storage.get_by_id(_chunk_id)
187+
if chunk:
188+
inserting_chunks[_chunk_id] = chunk
189+
190+
count += len(inserting_chunks)
191+
logger.info(
192+
"[Build KG] Inserting %d chunks, total %d", len(inserting_chunks), count
193+
)
179194

180-
inserting_chunks = await self.meta_storage.get_new_data(self.chunks_storage)
181-
if len(inserting_chunks) == 0:
182-
logger.warning("All chunks are already in the storage")
183-
return
184-
185-
logger.info("[New Chunks] inserting %d chunks", len(inserting_chunks))
186195
# Step 2: build knowledge graph from new chunks
187196
_add_entities_and_relations = await build_kg(
188197
llm_client=self.synthesizer_llm_client,
@@ -194,12 +203,8 @@ async def build_kg(self, inputs: List):
194203
logger.warning("No entities or relations extracted from text chunks")
195204
return
196205

197-
# Step 3: mark meta
206+
# Step 3: store the new entities and relations
198207
await self.graph_storage.index_done_callback()
199-
await self.meta_storage.mark_done(self.chunks_storage)
200-
await self.meta_storage.index_done_callback()
201-
202-
return _add_entities_and_relations
203208

204209
@op("search", deps=["read"], op_type=OpType.STREAMING)
205210
@async_to_sync_method
@@ -231,7 +236,7 @@ async def search(self, search_config: Dict, input_stream: Iterator):
231236

232237
@op("quiz_and_judge", deps=["build_kg"], op_type=OpType.BARRIER)
233238
@async_to_sync_method
234-
async def quiz_and_judge(self, quiz_and_judge_config: Dict, inputs: None):
239+
async def quiz_and_judge(self, quiz_and_judge_config: Dict):
235240
logger.warning(
236241
"Quiz and Judge operation needs trainee LLM client."
237242
" Make sure to provide one."
@@ -270,7 +275,7 @@ async def quiz_and_judge(self, quiz_and_judge_config: Dict, inputs: None):
270275

271276
@op("partition", deps=["build_kg"], op_type=OpType.BARRIER)
272277
@async_to_sync_method
273-
async def partition(self, partition_config: Dict, inputs: None):
278+
async def partition(self, partition_config: Dict):
274279
batches = await partition_kg(
275280
self.graph_storage,
276281
self.chunks_storage,
@@ -283,8 +288,11 @@ async def partition(self, partition_config: Dict, inputs: None):
283288
@op("extract", deps=["chunk"], op_type=OpType.STREAMING)
284289
@async_to_sync_method
285290
async def extract(self, extract_config: Dict, input_stream: Iterator):
286-
logger.info("Extracting information from given chunks...")
287-
291+
"""
292+
Extract information from chunks in chunks_storage
293+
input_stream: chunk IDs from chunks_storage
294+
return: None
295+
"""
288296
results = await extract_info(
289297
self.synthesizer_llm_client,
290298
self.chunks_storage,

graphgen/models/storage/json_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def all_keys(self) -> list[str]:
2424
async def index_done_callback(self):
2525
write_json(self._data, self._file_name)
2626

27-
async def get_by_id(self, id):
27+
async def get_by_id(self, id) -> dict | None:
2828
return self._data.get(id, None)
2929

3030
async def get_by_ids(self, ids, fields=None) -> list:

graphgen/operators/read/read_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def read_files(
4242
) -> Iterator[list[dict]]:
4343
path = Path(input_file).expanduser()
4444
if not path.exists():
45-
raise FileNotFoundError(f"[Reader] input_path not found: {input_file}")
45+
raise FileNotFoundError(f"[Read] input_path not found: {input_file}")
4646

4747
if allowed_suffix is None:
4848
support_suffix = set(_MAPPING.keys())
@@ -54,19 +54,19 @@ def read_files(
5454
suffix = path.suffix.lstrip(".").lower()
5555
if suffix not in support_suffix:
5656
logger.warning(
57-
"[Reader] Skip file %s (suffix '%s' not in allowed_suffix %s)",
57+
"[Read] Skip file %s (suffix '%s' not in allowed_suffix %s)",
5858
path,
5959
suffix,
6060
support_suffix,
6161
)
6262
return
6363
reader = _build_reader(suffix, cache_dir)
64-
logger.info("[Reader] Reading file %s", path)
64+
logger.info("[Read] Reading file %s", path)
6565
yield reader.read(str(path))
6666
return
6767

6868
# folder
69-
logger.info("[Reader] Streaming directory %s", path)
69+
logger.info("[Read] Streaming directory %s", path)
7070
for p in path.rglob("*"):
7171
if p.is_file() and p.suffix.lstrip(".").lower() in support_suffix:
7272
try:

0 commit comments

Comments
 (0)