Copilot commented on code in PR #351: URL: https://github.com/apache/hugegraph-ai/pull/351#discussion_r3353258311
########## hugegraph-llm/src/hugegraph_llm/api/models/graph_extract_requests.py: ########## @@ -0,0 +1,105 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import json +from typing import Any, Dict, List, Literal, Optional, Union + +from fastapi import Query +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator + + +class GraphExtractClientConfig(BaseModel): + model_config = ConfigDict(extra="forbid") + + graph: Optional[str] = None + user: Optional[str] = None + pwd: Optional[str] = None + gs: Optional[str] = None + + +class GraphExtractRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + texts: Union[str, List[str]] = Field(..., description="Text or list of texts to extract a graph from.") + graph_schema: Union[str, Dict[str, Any]] = Field( + ..., + alias="schema", + description="Graph schema as a JSON string/object, or an existing graph name.", + ) + example_prompt: Optional[str] = Query(None, description="Optional graph extraction prompt header.") + extract_type: Literal["property_graph"] = Query("property_graph", description="Extraction type.") + language: Literal["zh", "en"] = Query("zh", description="Language for chunk splitting.") + split_type: Literal["document", "paragraph", "sentence"] = Query("document", description="Chunk split granularity.") + include_meta: bool = Query(False, description="Include vertex/edge/text counts in the response.") + client_config: Optional[GraphExtractClientConfig] = Field(None, description="Request-scoped HugeGraph connection.") + + @field_validator("texts") + @classmethod + def normalize_texts(cls, v): + items = [v] if isinstance(v, str) else list(v) + items = [t for t in items if t and t.strip()] + if not items: + raise ValueError("texts must not be empty.") + return items + + @field_validator("graph_schema") + @classmethod + def normalize_schema(cls, v): + def validate_schema_obj(schema_obj): + if not isinstance(schema_obj, dict): + raise ValueError("schema JSON must be an object.") + if "vertexlabels" not in schema_obj or "edgelabels" not in schema_obj: + raise ValueError("schema must contain 'vertexlabels' and 'edgelabels'.") + if not isinstance(schema_obj["vertexlabels"], list) or not isinstance(schema_obj["edgelabels"], list): + raise ValueError("'vertexlabels' and 'edgelabels' must be lists.") Review Comment: `schema` validation is too shallow: it only checks that `vertexlabels`/`edgelabels` exist and are lists, but it does not validate the per-label structure (`name`, `properties`, `source_label`, `target_label`, etc.). Invalid inline schemas that pass this validator will later be rejected by `CheckSchema` in `SchemaNode` and end up as a scheduler/pipeline error (500) instead of the intended 422 validation error. ########## hugegraph-llm/src/hugegraph_llm/state/ai_state.py: ########## @@ -26,6 +26,8 @@ class WkFlowInput(GParam): split_type: Optional[str] = None # split type used by ChunkSplit Node example_prompt: Optional[str] = None # need by graph information extract schema: Optional[str] = None # Schema information requeired by SchemaNode + # Request-scoped HugeGraph connection; None falls back to global huge_settings. + graph_client_config: Optional[Dict[str, Any]] = None data_json: Optional[Dict[str, Any]] = None Review Comment: `WkFlowInput` adds `graph_client_config`, but `reset()` does not clear it. If the underlying `pycgraph` runtime reuses `GParam` instances and calls `reset()`, this field could retain request-scoped connection info across runs. Please update `reset()` to set `self.graph_client_config = None` alongside the other cleared fields. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
