fix: batch Zep set_ontology calls to respect 10-item API limit

Zep Graph API accepts a maximum of 10 entity types or relation types
per set_ontology request. When the LLM extracts more than 10 types,
the backend sends them all at once, causing a 400 Bad Request.

The fix batches entity_types and edge_definitions into chunks of 10
and calls set_ontology multiple times.

Fixes #631
This commit is contained in:
Md_Mushfiqur Rahim 2026-05-27 04:10:23 +00:00
parent 96096ea0ff
commit 2b50f39d5d
1 changed files with 20 additions and 4 deletions

View File

@ -284,11 +284,27 @@ class GraphBuilderService:
edge_definitions[name] = (edge_class, source_targets)
# 调用Zep API设置本体
# Zep API 限制每批次最多10个实体/关系类型
BATCH_SIZE = 10
if entity_types or edge_definitions:
entity_items = list(entity_types.items())
edge_items = list(edge_definitions.items())
max_batches = max(
(len(entity_items) + BATCH_SIZE - 1) // BATCH_SIZE if entity_items else 0,
(len(edge_items) + BATCH_SIZE - 1) // BATCH_SIZE if edge_items else 0,
1,
)
for i in range(max_batches):
batch_entities = dict(entity_items[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]) if entity_items else None
batch_edges = dict(edge_items[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]) if edge_items else None
self.client.graph.set_ontology(
graph_ids=[graph_id],
entities=entity_types if entity_types else None,
edges=edge_definitions if edge_definitions else None,
entities=batch_entities if batch_entities else None,
edges=batch_edges if batch_edges else None,
)
def add_text_batches(