Skip to content
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

fix: pylint for ci #86

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions hugegraph-llm/src/hugegraph_llm/api/rag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,20 @@ def graph_rag_recall_api(req: GraphRAGRequest):
# TODO/FIXME: handle QianFanClient error (not dict..critical)
# log.critical(f"## {type(result)}, {json.dumps(result)}")
if isinstance(result, dict):
log.critical(f"##1. {type(result)}")
log.critical("##1. %s", type(result))
return {"graph_recall": result}
else:
log.critical(f"##2. {type(result)}")
return {"graph_recall": json.dumps(result)}

log.critical("##2. %s", type(result))
return {"graph_recall": json.dumps(result)}

except TypeError as e:
log.error(f"TypeError in graph_rag_recall_api: {e}")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
log.error("TypeError in graph_rag_recall_api: %s", e)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) from e
except Exception as e:
log.error(f"Unexpected error occurred: {e}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred.")
log.error("Unexpected error occurred: %s", e)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred."
) from e


@router.post("/config/graph", status_code=status.HTTP_201_CREATED)
Expand Down
14 changes: 7 additions & 7 deletions hugegraph-llm/src/hugegraph_llm/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
dir_name = os.path.dirname
package_path = dir_name(dir_name(dir_name(dir_name(os.path.abspath(__file__)))))
env_path = os.path.join(package_path, ".env")
f_name = "config_prompt.yaml"
yaml_file_path = os.path.join(package_path, f"src/hugegraph_llm/resources/demo/{f_name}")
F_NAME = "config_prompt.yaml"
yaml_file_path = os.path.join(package_path, f"src/hugegraph_llm/resources/demo/{F_NAME}")


@dataclass
Expand Down Expand Up @@ -94,15 +94,15 @@ def __init__(self):

def ensure_yaml_file_exists(self):
if os.path.exists(yaml_file_path):
log.info(f"Loading prompt file '{f_name}' successfully.")
with open(yaml_file_path, "r") as file:
log.info("Loading prompt file '%s' successfully.", F_NAME)
with open(yaml_file_path, "r", encoding="utf-8") as file:
data = yaml.safe_load(file)
# Load existing values from the YAML file into the class attributes
for key, value in data.items():
setattr(self, key, value)
else:
self.save_to_yaml()
log.info(f"Prompt file '{yaml_file_path}' doesn't exist, create it.")
log.info("Prompt file '%s' doesn't exist, create it.", yaml_file_path)


def save_to_yaml(self):
Expand Down Expand Up @@ -131,10 +131,10 @@ def save_to_yaml(self):
{indented_default_answer_template}

"""
with open(yaml_file_path, "w") as file:
with open(yaml_file_path, "w", encoding="utf-8") as file:
file.write(yaml_content)


def update_yaml_file(self):
self.save_to_yaml()
log.info(f"Prompt file '{f_name}' updated successfully.")
log.info("Prompt file '%s' updated successfully.", F_NAME)
10 changes: 5 additions & 5 deletions hugegraph-llm/src/hugegraph_llm/config/config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ class ConfigData:
graph_user: Optional[str] = "admin"
graph_pwd: Optional[str] = "xxx"
graph_space: Optional[str] = None


# Additional static content like PromptConfig
class PromptData:

# Data is detached from hugegraph-llm/src/hugegraph_llm/operators/llm_op/answer_synthesize.py
answer_prompt = f"""You are an expert in knowledge graphs and natural language processing.
# Data is detached from llm_op/answer_synthesize.py
answer_prompt = """You are an expert in knowledge graphs and natural language processing.
Your task is to provide a precise and accurate answer based on the given context.

Context information is below.
---------------------
{{context_str}}
{context_str}
---------------------

Given the context information and without using fictive knowledge,
answer the following query in a concise and professional manner.
Query: {{query_str}}
Query: {query_str}
Answer:
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_api_connection(url, method="GET", headers=None, params=None, body=None,
if origin_call is None:
try:
raise gr.Error(json.loads(resp.text).get("message", msg))
except json.decoder.JSONDecodeError and AttributeError:
raise gr.Error(resp.text)
except (json.decoder.JSONDecodeError, AttributeError) as e:
raise gr.Error(resp.text) from e
return resp.status_code


Expand Down Expand Up @@ -180,6 +180,7 @@ def apply_llm_config(arg1, arg2, arg3, arg4, origin_call=None) -> int:


def create_configs_block():
# pylint: disable=R0915 (too-many-statements)
with gr.Accordion("1. Set up the HugeGraph server.", open=False):
with gr.Row():
graph_config_input = [
Expand Down
3 changes: 3 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/demo/rag_demo/rag_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101

import os
from typing import Tuple, Literal, Optional

Expand Down Expand Up @@ -88,6 +90,7 @@ def rag_answer(


def create_rag_block():
# pylint: disable=R0915 (too-many-statements)
gr.Markdown("""## 2. RAG with HugeGraph""")
with gr.Row():
with gr.Column(scale=2):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=E1101

import os

import gradio as gr
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-llm/src/hugegraph_llm/indices/vector_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging

import os
import pickle as pkl
from copy import deepcopy
Expand Down
8 changes: 7 additions & 1 deletion hugegraph-llm/src/hugegraph_llm/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,11 @@ async def dispatch(self, request: Request, call_next):

response.headers["X-Process-Time"] = f"{process_time:.2f} {unit}"
log.info("Request process time: %.2f ms, code=%d", process_time, response.status_code)
log.info(f"{request.method} - Args: {request.query_params}, IP: {request.client.host}, URL: {request.url}")
log.info(
"%s - Args: %s, IP: %s, URL: %s",
request.method,
request.query_params,
request.client.host,
request.url
)
return response
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
verbose = context.get("verbose") or False
if verbose:
from hugegraph_llm.utils.log import log
log.info(f"KEYWORDS: {context['keywords']}")
log.info("KEYWORDS: %s", context['keywords'])

return context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def run(self, data: dict) -> Dict[str, Any]:
return data

def load_into_graph(self, vertices, edges, schema):
# pylint: disable=R0912 (too-many-branches)
vertex_label_map = {v_label["name"]: v_label for v_label in schema["vertexlabels"]}
edge_label_map = {e_label["name"]: e_label for e_label in schema["edgelabels"]}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(self, max_deep: int = 2, max_items: int = 30, prop_to_match: Option
self._schema = ""

def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
# pylint: disable=R0915 (too-many-statements)
if self._client is None:
if isinstance(context.get("graph_client"), PyHugeClient):
self._client = context["graph_client"]
Expand Down Expand Up @@ -119,7 +120,7 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:

gremlin_query = VERTEX_QUERY_TPL.format(keywords=match_vids)
result: List[Any] = self._client.gremlin().exec(gremlin=gremlin_query)["data"]
log.debug(f"Vids query: {gremlin_query}")
log.debug("Vids query: %s", gremlin_query)

vertex_knowledge = self._format_graph_from_vertex(query_result=result)
gremlin_query = ID_QUERY_NEIGHBOR_TPL.format(
Expand All @@ -128,7 +129,7 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
max_items=self._max_items,
edge_labels=edge_labels_str,
)
log.debug(f"Kneighbor query: {gremlin_query}")
log.debug("Kneighbor query: %s", gremlin_query)

result: List[Any] = self._client.gremlin().exec(gremlin=gremlin_query)["data"]
graph_chain_knowledge, vertex_degree_list, knowledge_with_degree = self._format_graph_from_query_result(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=W0621

import asyncio
from typing import Any, Dict, Optional

from hugegraph_llm.config import prompt
from hugegraph_llm.models.llms.base import BaseLLM
from hugegraph_llm.models.llms.init_llm import LLMs
from hugegraph_llm.config import prompt
from hugegraph_llm.utils.log import log

DEFAULT_ANSWER_TEMPLATE = prompt.answer_prompt
Expand Down Expand Up @@ -70,11 +71,8 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
f"{self._context_body}\n"
f"{context_tail_str}".strip("\n"))

prompt = self._prompt_template.format(
context_str=context_str,
query_str=self._question,
)
response = self._llm.generate(prompt=prompt)
final_prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
response = self._llm.generate(prompt=final_prompt)
return {"answer": response}

vector_result = context.get("vector_result")
Expand Down Expand Up @@ -102,26 +100,27 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
async def async_generate(self, context: Dict[str, Any], context_head_str: str,
context_tail_str: str, vector_result_context: str,
graph_result_context: str):
# pylint: disable=R0912 (too-many-branches)
verbose = context.get("verbose") or False
# TODO: replace task_cache with a better name
task_cache = {}
if self._raw_answer:
prompt = self._question
task_cache["raw_task"] = asyncio.create_task(self._llm.agenerate(prompt=prompt))
final_prompt = self._question
task_cache["raw_task"] = asyncio.create_task(self._llm.agenerate(prompt=final_prompt))
if self._vector_only_answer:
context_str = (f"{context_head_str}\n"
f"{vector_result_context}\n"
f"{context_tail_str}".strip("\n"))

prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
task_cache["vector_only_task"] = asyncio.create_task(self._llm.agenerate(prompt=prompt))
final_prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
task_cache["vector_only_task"] = asyncio.create_task(self._llm.agenerate(prompt=final_prompt))
if self._graph_only_answer:
context_str = (f"{context_head_str}\n"
f"{graph_result_context}\n"
f"{context_tail_str}".strip("\n"))

prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
task_cache["graph_only_task"] = asyncio.create_task(self._llm.agenerate(prompt=prompt))
final_prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
task_cache["graph_only_task"] = asyncio.create_task(self._llm.agenerate(prompt=final_prompt))
if self._graph_vector_answer:
context_body_str = f"{vector_result_context}\n{graph_result_context}"
if context.get("graph_ratio", 0.5) < 0.5:
Expand All @@ -130,9 +129,9 @@ async def async_generate(self, context: Dict[str, Any], context_head_str: str,
f"{context_body_str}\n"
f"{context_tail_str}".strip("\n"))

prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
final_prompt = self._prompt_template.format(context_str=context_str, query_str=self._question)
task_cache["graph_vector_task"] = asyncio.create_task(
self._llm.agenerate(prompt=prompt)
self._llm.agenerate(prompt=final_prompt)
)
# TODO: use log.debug instead of print
if task_cache.get("raw_task"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from hugegraph_llm.models.llms.base import BaseLLM
from hugegraph_llm.operators.llm_op.info_extract import extract_triples_by_regex
from hugegraph_llm.utils.log import log


def generate_disambiguate_prompt(triples):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]:
verbose = context.get("verbose") or False
if verbose:
from hugegraph_llm.utils.log import log
log.info(f"KEYWORDS: {context['keywords']}")
log.info("KEYWORDS: %s", context['keywords'])

# extracting keywords & expanding synonyms increase the call count by 2
context["call_count"] = context.get("call_count", 0) + 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

# pylint: disable=W0621

import json
import re
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-llm/src/hugegraph_llm/utils/graph_index_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def import_graph_data(data: str, schema: str) -> Union[str, Dict[str, Any]]:
context = builder.commit_to_hugegraph().run(data_json)
gr.Info("Import graph data successfully!")
return json.dumps(context, ensure_ascii=False, indent=2)
except Exception as e:
except Exception as e: # pylint: disable=W0718
log.error(e)
traceback.print_exc()
# Note: can't use gr.Error here
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-python-client/src/pyhugegraph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def version(self) -> "VersionManager":
return VersionManager

def __repr__(self) -> str:
return f"{self.cfg}"
return str(self.cfg)
3 changes: 2 additions & 1 deletion style/pylint.conf
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ disable=raw-checker-failed,
R1725, # Consider using Python 3 style super() without arguments (super-with-arguments)
W0622, # Redefining built-in 'id' (redefined-builtin)
R0904, # Too many public methods (27/20) (too-many-public-methods)
E1120 # TODO: unbound-method-call-no-value-for-parameter
E1120, # TODO: unbound-method-call-no-value-for-parameter
R0917, # Too many positional arguments (6/5) (too-many-positional-arguments)

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
Loading