llama-index调用KnowledgeGraphIndex.from_documents报错

  • nebula 版本:3.6
  • 部署方式:单机
  • 安装方式:RPM
  • 是否上生产环境: N
edge_types, rel_prop_names = ["relationship"], ["relationship"] # default, could be omit if create from an empty kg
tags = ["entity"] # default, could be omit if create from an empty kg
loader = JSONReader()
documents = loader.load_data("./data/review_handled_small.json")
graph_store = NebulaGraphStore(space_name=space_name, edge_types=edge_types, rel_prop_names=rel_prop_names, tags=tags)
storage_context = StorageContext.from_defaults(graph_store=graph_store)

kg_index = KnowledgeGraphIndex.from_documents(
    documents,
    storage_context=storage_context,
    max_triplets_per_chunk=10,
    service_context=service_context,
    space_name=space_name,
    edge_types=edge_types,
    rel_prop_names=rel_prop_names,
    tags=tags,
    include_embeddings=True,
)
报错如下:
/home/luban/miniconda3/lib/python3.9/site-packages/tenacity/__init__.py:382 in __call__          │
│                                                                                                  │
│   379 │   │   │   do = self.iter(retry_state=retry_state)                                        │
│   380 │   │   │   if isinstance(do, DoAttempt):                                                  │
│   381 │   │   │   │   try:                                                                       │
│ ❱ 382 │   │   │   │   │   result = fn(*args, **kwargs)                                           │
│   383 │   │   │   │   except BaseException:  # noqa: B902                                        │
│   384 │   │   │   │   │   retry_state.set_exception(sys.exc_info())  # type: ignore[arg-type]    │
│   385 │   │   │   │   else:                                                                      │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/graph_stores/nebulagraph.py:275   │
│ in execute                                                                                       │
│                                                                                                  │
│   272 │   │   │   if result is None:                                                             │
│   273 │   │   │   │   raise ValueError(f"Query failed. Query: {query}, Param: {param_map}")      │
│   274 │   │   │   if not result.is_succeeded():                                                  │
│ ❱ 275 │   │   │   │   raise ValueError(                                                          │
│   276 │   │   │   │   │   f"Query failed. Query: {query}, Param: {param_map}"                    │
│   277 │   │   │   │   │   f"Error message: {result.error_msg()}"                                 │
│   278 │   │   │   │   )                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ValueError: Query failed. Query: INSERT VERTEX `entity`(name)   VALUES 
"略略略略玩手机(聊微信)":("略略玩手机(聊微信)");INSERT VERTEX `entity`(name)   VALUES 
"略略发生的主要原因":("略略发生的主要原因");INSERT EDGE `relationship`(`relationship`)   VALUES 
"略略玩手机(聊微信)"->"略略的主要原因"@-3743175742781155646:("是导致");, Param: {}Error message: 
SemanticError: No schema found for `entity'

The above exception was the direct cause of the following exception:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ in <module>:31                                                                                   │
│                                                                                                  │
│   28 graph_store = NebulaGraphStore(space_name=space_name, edge_types=edge_types, rel_prop_na    │
│   29 storage_context = StorageContext.from_defaults(graph_store=graph_store)                     │
│   30                                                                                             │
│ ❱ 31 kg_index = KnowledgeGraphIndex.from_documents(                                              │
│   32 │   documents,                                                                              │
│   33 │   storage_context=storage_context,                                                        │
│   34 │   max_triplets_per_chunk=10,                                                              │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/base.py:106 in            │
│ from_documents                                                                                   │
│                                                                                                  │
│   103 │   │   │   │   **kwargs,                                                                  │
│   104 │   │   │   )                                                                              │
│   105 │   │   │                                                                                  │
│ ❱ 106 │   │   │   return cls(                                                                    │
│   107 │   │   │   │   nodes=nodes,                                                               │
│   108 │   │   │   │   storage_context=storage_context,                                           │
│   109 │   │   │   │   service_context=service_context,                                           │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/knowledge_graph/base.py:8 │
│ 1 in __init__                                                                                    │
│                                                                                                  │
│    78 │   │   self._max_object_length = max_object_length                                        │
│    79 │   │   self._kg_triplet_extract_fn = kg_triplet_extract_fn                                │
│    80 │   │                                                                                      │
│ ❱  81 │   │   super().__init__(                                                                  │
│    82 │   │   │   nodes=nodes,                                                                   │
│    83 │   │   │   index_struct=index_struct,                                                     │
│    84 │   │   │   service_context=service_context,                                               │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/base.py:71 in __init__    │
│                                                                                                  │
│    68 │   │   with self._service_context.callback_manager.as_trace("index_construction"):        │
│    69 │   │   │   if index_struct is None:                                                       │
│    70 │   │   │   │   assert nodes is not None                                                   │
│ ❱  71 │   │   │   │   index_struct = self.build_index_from_nodes(nodes)                          │
│    72 │   │   │   self._index_struct = index_struct                                              │
│    73 │   │   │   self._storage_context.index_store.add_index_struct(self._index_struct)         │
│    74                                                                                            │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/base.py:175 in            │
│ build_index_from_nodes                                                                           │
│                                                                                                  │
│   172 │   def build_index_from_nodes(self, nodes: Sequence[BaseNode]) -> IS:                     │
│   173 │   │   """Build the index from nodes."""                                                  │
│   174 │   │   self._docstore.add_documents(nodes, allow_update=True)                             │
│ ❱ 175 │   │   return self._build_index_from_nodes(nodes)                                         │
│   176 │                                                                                          │
│   177 │   @abstractmethod                                                                        │
│   178 │   def _insert(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None:            │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/knowledge_graph/base.py:1 │
│ 73 in _build_index_from_nodes                                                                    │
│                                                                                                  │
│   170 │   │   │   logger.debug(f"> Extracted triplets: {triplets}")                              │
│   171 │   │   │   for triplet in triplets:                                                       │
│   172 │   │   │   │   subj, _, obj = triplet                                                     │
│ ❱ 173 │   │   │   │   self.upsert_triplet(triplet)                                               │
│   174 │   │   │   │   index_struct.add_node([subj, obj], n)                                      │
│   175 │   │   │                                                                                  │
│   176 │   │   │   if self.include_embeddings:                                                    │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/indices/knowledge_graph/base.py:2 │
│ 21 in upsert_triplet                                                                             │
│                                                                                                  │
│   218 │   │   │   triplet (str): Knowledge triplet                                               │
│   219 │   │                                                                                      │
│   220 │   │   """                                                                                │
│ ❱ 221 │   │   self._graph_store.upsert_triplet(*triplet)                                         │
│   222 │                                                                                          │
│   223 │   def add_node(self, keywords: List[str], node: BaseNode) -> None:                       │
│   224 │   │   """Add node.                                                                       │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/llama_index/graph_stores/nebulagraph.py:530   │
│ in upsert_triplet                                                                                │
│                                                                                                  │
│   527 │   │   │   f"@{rel_hash}:({QUOTE}{rel}{QUOTE});"                                          │
│   528 │   │   )                                                                                  │
│   529 │   │   logger.debug(f"upsert_triplet()\nDML query: {dml_query}")                          │
│ ❱ 530 │   │   result = self.execute(dml_query)                                                   │
│   531 │   │   assert (                                                                           │
│   532 │   │   │   result and result.is_succeeded()                                               │
│   533 │   │   ), f"Failed to upsert triplet: {subj} {rel} {obj}, query: {dml_query}"             │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/tenacity/__init__.py:289 in wrapped_f         │
│                                                                                                  │
│   286 │   │                                                                                      │
│   287 │   │   @functools.wraps(f)                                                                │
│   288 │   │   def wrapped_f(*args: t.Any, **kw: t.Any) -> t.Any:                                 │
│ ❱ 289 │   │   │   return self(f, *args, **kw)                                                    │
│   290 │   │                                                                                      │
│   291 │   │   def retry_with(*args: t.Any, **kwargs: t.Any) -> WrappedFn:                        │
│   292 │   │   │   return self.copy(*args, **kwargs).wraps(f)                                     │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/tenacity/__init__.py:379 in __call__          │
│                                                                                                  │
│   376 │   │                                                                                      │
│   377 │   │   retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)   │
│   378 │   │   while True:                                                                        │
│ ❱ 379 │   │   │   do = self.iter(retry_state=retry_state)                                        │
│   380 │   │   │   if isinstance(do, DoAttempt):                                                  │
│   381 │   │   │   │   try:                                                                       │
│   382 │   │   │   │   │   result = fn(*args, **kwargs)                                           │
│                                                                                                  │
│ /home/luban/miniconda3/lib/python3.9/site-packages/tenacity/__init__.py:326 in iter              │
│                                                                                                  │
│   323 │   │   │   retry_exc = self.retry_error_cls(fut)                                          │
│   324 │   │   │   if self.reraise:                                                               │
│   325 │   │   │   │   raise retry_exc.reraise()                                                  │
│ ❱ 326 │   │   │   raise retry_exc from fut.exception()                                           │
│   327 │   │                                                                                      │
│   328 │   │   if self.wait:                                                                      │
│   329 │   │   │   sleep = self.wait(retry_state) 

看报错是你没有创建 schema 呢,我那个文档里应该写了哈,可以参考

https://docs.llamaindex.ai/en/latest/examples/index_structs/knowledge_graph/KnowledgeGraphIndex_vs_VectorStoreIndex_vs_CustomIndex_combined.html#

这里边 “ Prepare for NebulaGraph” 中有一段注释提到了,要创建 tag/edge 哈

此话题已在最后回复的 30 天后被自动关闭。不再允许新回复。