基于Oracle的图存储模块
| 324 | |
| 325 | @dataclass |
| 326 | class OracleGraphStorage(BaseGraphStorage): |
| 327 | """基于Oracle的图存储模块""" |
| 328 | |
| 329 | def __post_init__(self): |
| 330 | """从graphml文件加载图""" |
| 331 | self._max_batch_size = self.global_config["embedding_batch_num"] |
| 332 | |
| 333 | #################### insert method ################ |
| 334 | |
| 335 | async def upsert_node(self, node_id: str, node_data: dict[str, str]): |
| 336 | """插入或更新节点""" |
| 337 | # print("go into upsert node method") |
| 338 | entity_name = node_id |
| 339 | entity_type = node_data["entity_type"] |
| 340 | description = node_data["description"] |
| 341 | source_id = node_data["source_id"] |
| 342 | logger.debug(f"entity_name:{entity_name}, entity_type:{entity_type}") |
| 343 | |
| 344 | content = entity_name + description |
| 345 | contents = [content] |
| 346 | batches = [ |
| 347 | contents[i : i + self._max_batch_size] |
| 348 | for i in range(0, len(contents), self._max_batch_size) |
| 349 | ] |
| 350 | embeddings_list = await asyncio.gather( |
| 351 | *[self.embedding_func(batch) for batch in batches] |
| 352 | ) |
| 353 | embeddings = np.concatenate(embeddings_list) |
| 354 | content_vector = embeddings[0] |
| 355 | merge_sql = SQL_TEMPLATES["merge_node"] |
| 356 | data = { |
| 357 | "workspace": self.db.workspace, |
| 358 | "name": entity_name, |
| 359 | "entity_type": entity_type, |
| 360 | "description": description, |
| 361 | "source_chunk_id": source_id, |
| 362 | "content": content, |
| 363 | "content_vector": content_vector, |
| 364 | } |
| 365 | # print(merge_sql) |
| 366 | await self.db.execute(merge_sql, data) |
| 367 | # self._graph.add_node(node_id, **node_data) |
| 368 | |
| 369 | async def upsert_edge( |
| 370 | self, source_node_id: str, target_node_id: str, edge_data: dict[str, str] |
| 371 | ): |
| 372 | """插入或更新边""" |
| 373 | # print("go into upsert edge method") |
| 374 | source_name = source_node_id |
| 375 | target_name = target_node_id |
| 376 | weight = edge_data["weight"] |
| 377 | keywords = edge_data["keywords"] |
| 378 | description = edge_data["description"] |
| 379 | source_chunk_id = edge_data["source_id"] |
| 380 | logger.debug( |
| 381 | f"source_name:{source_name}, target_name:{target_name}, keywords: {keywords}" |
| 382 | ) |
| 383 |
nothing calls this directly
no outgoing calls
no test coverage detected