You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hugegraph.apache.org by "vichayturen (via GitHub)" <gi...@apache.org> on 2024/03/01 03:23:06 UTC
Re: [PR] feat: Introduce gradio for creating interactive and visual demo [incubator-hugegraph-ai]
vichayturen commented on code in PR #30:
URL: https://github.com/apache/incubator-hugegraph-ai/pull/30#discussion_r1508408286
##########
hugegraph-llm/src/hugegraph_llm/llms/ernie_bot.py:
##########
@@ -61,7 +61,7 @@ def generate(
raise Exception(
f"Request failed with code {response.status_code}, message: {response.text}"
)
- return response.text
Review Comment:
Considering invalid access token, below may be better.
```
response_json = json.loads(response.text)
if "error_code" in response_json:
raise Exception(
f"Error {response_json['error_code']}: {response_json['error_msg']}"
)
return response_json["result"]
```
##########
hugegraph-llm/src/hugegraph_llm/utils/gradio_demo.py:
##########
@@ -0,0 +1,254 @@
+# 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
+import os
+
+import gradio as gr
+import uvicorn
+from fastapi import FastAPI
+
+from hugegraph_llm.llms.init_llm import LLMs
+from hugegraph_llm.operators.graph_rag_task import GraphRAG
+from hugegraph_llm.operators.kg_construction_task import KgBuilder
+from hugegraph_llm.utils.config import Config
+from hugegraph_llm.utils.constants import Constants
+from pyhugegraph.client import PyHugeClient
+
+
+def init_hg_test_data():
+ client = get_hg_client()
+ schema = client.schema()
+ schema.propertyKey("name").asText().ifNotExist().create()
+ schema.propertyKey("birthDate").asText().ifNotExist().create()
+ schema.vertexLabel("Person").properties(
+ "name", "birthDate"
+ ).useCustomizeStringId().ifNotExist().create()
+ schema.vertexLabel("Movie").properties("name").useCustomizeStringId().ifNotExist().create()
+ schema.edgeLabel("ActedIn").sourceLabel("Person").targetLabel("Movie").ifNotExist().create()
+
+ schema.indexLabel("PersonByName").onV("Person").by("name").secondary().ifNotExist().create()
+ schema.indexLabel("MovieByName").onV("Movie").by("name").secondary().ifNotExist().create()
+
+ graph = client.graph()
+ graph.addVertex("Person", {"name": "Al Pacino", "birthDate": "1940-04-25"}, id="Al Pacino")
+ graph.addVertex(
+ "Person",
+ {"name": "Robert De Niro", "birthDate": "1943-08-17"},
+ id="Robert De Niro",
+ )
+ graph.addVertex("Movie", {"name": "The Godfather"}, id="The Godfather")
+ graph.addVertex("Movie", {"name": "The Godfather Part II"}, id="The Godfather Part II")
+ graph.addVertex(
+ "Movie",
+ {"name": "The Godfather Coda The Death of Michael Corleone"},
+ id="The Godfather Coda The Death of Michael Corleone",
+ )
+
+ graph.addEdge("ActedIn", "Al Pacino", "The Godfather", {})
+ graph.addEdge("ActedIn", "Al Pacino", "The Godfather Part II", {})
+ graph.addEdge("ActedIn", "Al Pacino", "The Godfather Coda The Death of Michael Corleone", {})
+ graph.addEdge("ActedIn", "Robert De Niro", "The Godfather Part II", {})
+ schema.getSchema()
+ graph.close()
+ return {
+ "vertex": ["Person", "Movie"],
+ "edge": ["ActedIn"],
+ "property": ["name", "birthDate"],
+ "index": ["PersonByName", "MovieByName"],
+ }
+
+
+def graph_rag(text):
+ res = (
+ GraphRAG()
+ .extract_keyword(text=text)
+ .query_graph_for_rag()
+ .synthesize_answer()
+ .run(verbose=True)
+ )
+ return res
+
+
+def build_kg(text, schema, disambiguate_word_sense, commit_to_hugegraph):
+ builder = KgBuilder(LLMs().get_llm())
+ # 判断 schema 是不是json结构
+ if schema:
+ try:
+ schema = json.loads(schema.strip())
+ builder.import_schema(from_user_defined=schema)
+ except json.JSONDecodeError as e:
+ print(e)
+ builder.import_schema(from_hugegraph=schema)
Review Comment:
When we want to import schema from huge graph, we input the graph name as `schema`.
However, `schema` is `str` can be `json.loads` without exception.
##########
hugegraph-llm/src/hugegraph_llm/operators/kg_construction_task.py:
##########
@@ -58,7 +60,8 @@ def print_result(self):
self.operators.append(PrintResult())
return self
- def run(self):
- result = ""
+ def run(self) -> Dict[str, Any]:
+ context = ""
Review Comment:
`context=None` may be better.
--
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: issues-unsubscribe@hugegraph.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org