fix(knext): reasoner command add default cfg config (#377)

This commit is contained in:
royzhao 2024-10-28 11:20:25 +08:00 committed by GitHub
parent 2b40c33977
commit e460b8d73d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 9 deletions

View File

@ -11,7 +11,10 @@
# or implied.
import os
import sys
from pathlib import Path
import click
from knext.common.env import init_kag_config
from knext.reasoner.client import ReasonerClient
@ -19,17 +22,29 @@ from knext.reasoner.client import ReasonerClient
@click.option("--file", help="Path of DSL file.")
@click.option("--dsl", help="DSL string enclosed in double quotes.")
@click.option("--output", help="Output file.")
def execute_reasoner_job(file, dsl, output=None):
@click.option("--proj_path", help="Path of config.", default="./")
def execute_reasoner_job(file, dsl, output=None, proj_path="./"):
"""
Submit asynchronous reasoner jobs to server by providing DSL file or string.
"""
with_server = eval(os.getenv("KAG_PROJECT_WITH_SERVER", "False"))
if not with_server:
config_path = os.path.join(proj_path, "kag_config.cfg")
if not Path(config_path).exists():
# find *.cfg file
cfg_files = list(Path(proj_path).glob("*.cfg"))
if len(cfg_files) == 0:
click.secho("ERROR: No .cfg file found.", fg="bright_red")
sys.exit()
config_path = cfg_files[0]
init_kag_config(config_path)
host_addr = os.getenv("KAG_PROJECT_ADDR", "http://127.0.0.1:8887")
project_id = os.getenv("KAG_PROJECT_ID")
if not project_id:
click.secho(
"ERROR: Reasoner must be executed with SPG Server.", fg="bright_red"
"ERROR: Reasoner must be executed with SPG Server. Need assign proj_path",
fg="bright_red",
)
sys.exit()
client = ReasonerClient()
client = ReasonerClient(host_addr=host_addr, project_id=int(project_id))
if file and not dsl:
with open(file, "r") as f:
dsl_content = f.read()
@ -38,4 +53,4 @@ def execute_reasoner_job(file, dsl, output=None):
else:
click.secho("ERROR: Please choose either --file or --dsl.", fg="bright_red")
sys.exit()
client.execute(dsl_content, output)
client.execute(dsl_content, output_file=output)

View File

@ -10,11 +10,12 @@
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.
import os
import datetime
from knext.reasoner.rest.models.reason_task_response import ReasonTaskResponse
import knext.common.cache
from knext.common.base.client import Client
from knext.common.rest import ApiClient, Configuration
from knext.project.client import ProjectClient
from knext.reasoner import ReasonTask
from knext.reasoner import rest
from knext.reasoner.rest import SpgTypeQueryRequest
@ -100,8 +101,22 @@ class ReasonerClient(Client):
"""
Execute a synchronous builder job in local runner.
"""
task = self.syn_execute(dsl_content)
print(task)
task_response: ReasonTaskResponse = self.syn_execute(dsl_content)
task: ReasonTask = task_response.task
if task.status != "FINISH":
print(f"RUN {task.status} {dsl_content}")
else:
default_output_file = output_file or (
f"./{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.csv"
)
show_data = [
task.result_table_result.header
] + task.result_table_result.rows
import pandas as pd
df = pd.DataFrame(show_data)
print(df)
df.to_csv(default_output_file, index=False)
def query_node(self, label, id_value):
req = SpgTypeQueryRequest(