forked from apache/hugegraph-ai
-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor: Refactor Scheduler to Support Dynamic Workflow Scheduling and Pipeline Pooling #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weijinglin
wants to merge
9
commits into
main
Choose a base branch
from
agentic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0ec820
replace workflow execution framework to CGraph
weijinglin 73033f2
improve framework
weijinglin 226d805
Merge branch 'main' into agentic
weijinglin 5e129c0
fixed some mistakes
weijinglin b497d14
format
weijinglin a353eec
apply ai's suggestion
weijinglin bba237c
refactor scheduler
weijinglin b31eca3
add licenses
weijinglin f5f6f8e
minor fix
weijinglin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # 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. |
55 changes: 55 additions & 0 deletions
55
hugegraph-llm/src/hugegraph_llm/flows/build_vector_index.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # 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. | ||
|
|
||
| from hugegraph_llm.flows.common import BaseFlow | ||
|
weijinglin marked this conversation as resolved.
|
||
| from hugegraph_llm.state.ai_state import WkFlowInput | ||
|
|
||
| import json | ||
| from PyCGraph import GPipeline | ||
|
|
||
| from hugegraph_llm.operators.document_op.chunk_split import ChunkSplitNode | ||
| from hugegraph_llm.operators.index_op.build_vector_index import BuildVectorIndexNode | ||
| from hugegraph_llm.state.ai_state import WkFlowState | ||
|
|
||
|
|
||
| class BuildVectorIndexFlow(BaseFlow): | ||
| def __init__(self): | ||
| pass | ||
|
|
||
| def prepare(self, prepared_input: WkFlowInput, texts): | ||
| prepared_input.texts = texts | ||
| prepared_input.language = "zh" | ||
| prepared_input.split_type = "paragraph" | ||
| return | ||
|
|
||
| def build_flow(self, texts): | ||
| pipeline = GPipeline() | ||
| # prepare for workflow input | ||
| prepared_input = WkFlowInput() | ||
| self.prepare(prepared_input, texts) | ||
|
|
||
| pipeline.createGParam(prepared_input, "wkflow_input") | ||
| pipeline.createGParam(WkFlowState(), "wkflow_state") | ||
|
|
||
| chunk_split_node = ChunkSplitNode() | ||
| build_vector_node = BuildVectorIndexNode() | ||
| pipeline.registerGElement(chunk_split_node, set(), "chunk_split") | ||
| pipeline.registerGElement(build_vector_node, {chunk_split_node}, "build_vector") | ||
|
|
||
| return pipeline | ||
|
|
||
| def post_deal(self, pipeline=None): | ||
| res = pipeline.getGParamWithNoEmpty("wkflow_state").to_json() | ||
| return json.dumps(res, ensure_ascii=False, indent=2) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # 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. | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
weijinglin marked this conversation as resolved.
|
||
|
|
||
| from hugegraph_llm.state.ai_state import WkFlowInput | ||
|
|
||
|
|
||
| class BaseFlow(ABC): | ||
| """ | ||
| Base class for flows, defines three interface methods: prepare, build_flow, and post_deal. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def prepare(self, prepared_input: WkFlowInput, *args, **kwargs): | ||
| """ | ||
| Pre-processing interface. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def build_flow(self, *args, **kwargs): | ||
| """ | ||
| Interface for building the flow. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def post_deal(self, *args, **kwargs): | ||
| """ | ||
| Post-processing interface. | ||
| """ | ||
| pass | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # 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 | ||
|
weijinglin marked this conversation as resolved.
|
||
| from PyCGraph import GPipeline | ||
| from hugegraph_llm.flows.common import BaseFlow | ||
| from hugegraph_llm.state.ai_state import WkFlowInput, WkFlowState | ||
| from hugegraph_llm.operators.common_op.check_schema import CheckSchemaNode | ||
| from hugegraph_llm.operators.document_op.chunk_split import ChunkSplitNode | ||
| from hugegraph_llm.operators.hugegraph_op.schema_manager import SchemaManagerNode | ||
| from hugegraph_llm.operators.llm_op.info_extract import InfoExtractNode | ||
| from hugegraph_llm.operators.llm_op.property_graph_extract import ( | ||
| PropertyGraphExtractNode, | ||
| ) | ||
| from hugegraph_llm.utils.log import log | ||
|
|
||
|
|
||
| class GraphExtractFlow(BaseFlow): | ||
| def __init__(self): | ||
| pass | ||
|
|
||
| def _import_schema( | ||
| self, | ||
| from_hugegraph=None, | ||
| from_extraction=None, | ||
| from_user_defined=None, | ||
| ): | ||
| if from_hugegraph: | ||
| return SchemaManagerNode() | ||
| elif from_user_defined: | ||
| return CheckSchemaNode() | ||
| elif from_extraction: | ||
| raise NotImplementedError("Not implemented yet") | ||
| else: | ||
| raise ValueError("No input data / invalid schema type") | ||
|
|
||
| def prepare( | ||
| self, prepared_input: WkFlowInput, schema, texts, example_prompt, extract_type | ||
| ): | ||
| # prepare input data | ||
| prepared_input.texts = texts | ||
| prepared_input.language = "zh" | ||
| prepared_input.split_type = "document" | ||
| prepared_input.example_prompt = example_prompt | ||
| prepared_input.schema = schema | ||
| schema = schema.strip() | ||
| if schema.startswith("{"): | ||
| try: | ||
| schema = json.loads(schema) | ||
| prepared_input.schema = schema | ||
| except json.JSONDecodeError as exc: | ||
| log.error("Invalid JSON format in schema. Please check it again.") | ||
| raise ValueError("Invalid JSON format in schema.") from exc | ||
| else: | ||
| log.info("Get schema '%s' from graphdb.", schema) | ||
| prepared_input.graph_name = schema | ||
| return | ||
|
|
||
| def build_flow(self, schema, texts, example_prompt, extract_type): | ||
| pipeline = GPipeline() | ||
| prepared_input = WkFlowInput() | ||
| # prepare input data | ||
| self.prepare(prepared_input, schema, texts, example_prompt, extract_type) | ||
|
|
||
| pipeline.createGParam(prepared_input, "wkflow_input") | ||
| pipeline.createGParam(WkFlowState(), "wkflow_state") | ||
| schema = schema.strip() | ||
| schema_node = None | ||
| if schema.startswith("{"): | ||
| try: | ||
| schema = json.loads(schema) | ||
| schema_node = self._import_schema(from_user_defined=schema) | ||
| except json.JSONDecodeError as exc: | ||
| log.error("Invalid JSON format in schema. Please check it again.") | ||
| raise ValueError("Invalid JSON format in schema.") from exc | ||
| else: | ||
| log.info("Get schema '%s' from graphdb.", schema) | ||
| schema_node = self._import_schema(from_hugegraph=schema) | ||
|
|
||
| chunk_split_node = ChunkSplitNode() | ||
| graph_extract_node = None | ||
| if extract_type == "triples": | ||
| graph_extract_node = InfoExtractNode() | ||
| elif extract_type == "property_graph": | ||
| graph_extract_node = PropertyGraphExtractNode() | ||
| else: | ||
| raise ValueError(f"Unsupported extract_type: {extract_type}") | ||
| pipeline.registerGElement(schema_node, set(), "schema_node") | ||
| pipeline.registerGElement(chunk_split_node, set(), "chunk_split") | ||
| pipeline.registerGElement( | ||
| graph_extract_node, {schema_node, chunk_split_node}, "graph_extract" | ||
| ) | ||
|
|
||
| return pipeline | ||
|
|
||
| def post_deal(self, pipeline=None): | ||
| res = pipeline.getGParamWithNoEmpty("wkflow_state").to_json() | ||
| vertices = res.get("vertices", []) | ||
| edges = res.get("edges", []) | ||
| if not vertices and not edges: | ||
| log.info("Please check the schema.(The schema may not match the Doc)") | ||
| return json.dumps( | ||
| { | ||
| "vertices": vertices, | ||
| "edges": edges, | ||
| "warning": "The schema may not match the Doc", | ||
| }, | ||
| ensure_ascii=False, | ||
| indent=2, | ||
| ) | ||
| return json.dumps( | ||
| {"vertices": vertices, "edges": edges}, | ||
| ensure_ascii=False, | ||
| indent=2, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # 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 threading | ||
| from typing import Dict, Any | ||
| from PyCGraph import GPipelineManager | ||
| from hugegraph_llm.flows.build_vector_index import BuildVectorIndexFlow | ||
| from hugegraph_llm.flows.common import BaseFlow | ||
| from hugegraph_llm.flows.graph_extract import GraphExtractFlow | ||
| from hugegraph_llm.utils.log import log | ||
|
|
||
|
|
||
| class Scheduler: | ||
| pipeline_pool: Dict[str, Any] = None | ||
| max_pipeline: int | ||
|
|
||
| def __init__(self, max_pipeline: int = 10): | ||
| self.pipeline_pool = {} | ||
| # pipeline_pool act as a manager of GPipelineManager which used for pipeline management | ||
| self.pipeline_pool["build_vector_index"] = { | ||
| "manager": GPipelineManager(), | ||
| "flow": BuildVectorIndexFlow(), | ||
| } | ||
| self.pipeline_pool["graph_extract"] = { | ||
| "manager": GPipelineManager(), | ||
| "flow": GraphExtractFlow(), | ||
| } | ||
| self.max_pipeline = max_pipeline | ||
|
|
||
| # TODO: Implement Agentic Workflow | ||
| def agentic_flow(self): | ||
| pass | ||
|
|
||
| def schedule_flow(self, flow: str, *args, **kwargs): | ||
| if flow not in self.pipeline_pool: | ||
| raise ValueError(f"Unsupported workflow {flow}") | ||
| manager = self.pipeline_pool[flow]["manager"] | ||
| flow: BaseFlow = self.pipeline_pool[flow]["flow"] | ||
| pipeline = manager.fetch() | ||
| if pipeline is None: | ||
| # call coresponding flow_func to create new workflow | ||
| pipeline = flow.build_flow(*args, **kwargs) | ||
| status = pipeline.init() | ||
| if status.isErr(): | ||
| error_msg = f"Error in flow init: {status.getInfo()}" | ||
| log.error(error_msg) | ||
| raise RuntimeError(error_msg) | ||
| status = pipeline.run() | ||
| if status.isErr(): | ||
| error_msg = f"Error in flow execution: {status.getInfo()}" | ||
| log.error(error_msg) | ||
| raise RuntimeError(error_msg) | ||
| res = flow.post_deal(pipeline) | ||
| manager.add(pipeline) | ||
| return res | ||
| else: | ||
| # fetch pipeline & prepare input for flow | ||
| prepared_input = pipeline.getGParamWithNoEmpty("wkflow_input") | ||
| flow.prepare(prepared_input, *args, **kwargs) | ||
| status = pipeline.run() | ||
| if status.isErr(): | ||
| raise RuntimeError(f"Error in flow execution {status.getInfo()}") | ||
| res = flow.post_deal(pipeline) | ||
| manager.release(pipeline) | ||
| return res | ||
|
|
||
|
|
||
| class SchedulerSingleton: | ||
| _instance = None | ||
| _instance_lock = threading.Lock() | ||
|
|
||
| @classmethod | ||
| def get_instance(cls): | ||
| if cls._instance is None: | ||
| with cls._instance_lock: | ||
| if cls._instance is None: | ||
| cls._instance = Scheduler() | ||
| return cls._instance |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
[tool.uv.sources] 的 Linux-only pycgraph 源会造成分发不一致;建议删除或与上方依赖声明对齐
"pycgraph"增加相同的sys_platform == 'linux'标记,并将rev从不稳定的main固定到具体 commit/tag,保证可复现构建与合规审计。建议直接移除此映射(配合上方修改):
[tool.uv.sources] hugegraph-python-client = { workspace = true } -pycgraph = { git = "https://github.com/ChunelFeng/CGraph.git", subdirectory = "python", rev = "main", marker = "sys_platform == 'linux'" }附:请确认 CGraph 的许可证与 ASF 发布合规(需要在 NOTICE/第三方依赖清单中体现)。我可以帮忙自动获取上游仓库的许可证与最新提交信息:
🏁 Script executed:
Length of output: 207
移除或对齐 Linux-only pycgraph 源并固定 rev(必需)
tool.uv.sources 中对 pycgraph 的 Linux-only git 源会导致 uv 与 pip/PEP 508 解析不一致。优先从 hugegraph-llm/pyproject.toml 中移除该行;若保留,则必须在 dependencies 中为 "pycgraph" 增加相同的 marker 且将 rev 固定为具体 commit:04fc967f58622be04f2e9bcb1d7b8b7ac27e1d8c(已核验)。CGraph 许可证为 MIT(https://github.com/ChunelFeng/CGraph/blob/main/LICENSE),需在 NOTICE/第三方依赖清单中列明。
位置:hugegraph-llm/pyproject.toml(约第89行)
[tool.uv.sources] hugegraph-python-client = { workspace = true } -pycgraph = { git = "https://github.com/ChunelFeng/CGraph.git", subdirectory = "python", rev = "main", marker = "sys_platform == 'linux'" }📝 Committable suggestion
🤖 Prompt for AI Agents