imbajin commented on code in PR #110:
URL:
https://github.com/apache/incubator-hugegraph-ai/pull/110#discussion_r1836213163
##########
hugegraph-llm/src/hugegraph_llm/api/models/rag_requests.py:
##########
@@ -70,3 +70,7 @@ class RerankerConfigRequest(BaseModel):
reranker_type: str
api_key: str
cohere_base_url: Optional[str] = None
+
+class LogStreamRequest(BaseModel):
+ log_token: Optional[str] = None
+ log_file: Optional[str] = 'llm-server.log'
Review Comment:
```suggestion
log_file: Optional[str] = 'llm-server.log'
```
add a empty line in EOF (better to set it in IDE settings)
##########
hugegraph-llm/src/hugegraph_llm/api/rag_api.py:
##########
@@ -49,7 +52,7 @@ def graph_rag_recall(
def rag_http_api(
- router: APIRouter, rag_answer_func, apply_graph_conf, apply_llm_conf,
apply_embedding_conf, apply_reranker_conf
+ router: APIRouter, rag_answer_func, apply_graph_conf, apply_llm_conf,
apply_embedding_conf, apply_reranker_conf, log_stream
Review Comment:
line chars > 120 (better to move it outside) refer ↓
##########
hugegraph-llm/src/hugegraph_llm/api/rag_api.py:
##########
@@ -126,3 +129,14 @@ def rerank_config_api(req: RerankerConfigRequest):
else:
res = status.HTTP_501_NOT_IMPLEMENTED
return generate_response(RAGResponse(status_code=res, message="Missing
Value"))
+
+ @router.post("/logs", status_code=status.HTTP_200_OK)
Review Comment:
prefer to separate it in a single api file (like `util_api.py` or
`admin_api.py`)
##########
hugegraph-llm/src/hugegraph_llm/demo/rag_demo/log_block.py:
##########
@@ -0,0 +1,167 @@
+# 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 gradio as gr
+from hugegraph_llm.config import settings
+from hugegraph_llm.utils.log import log
+import os
+import asyncio
+import requests
+from fastapi import Request
+
+
+async def log_stream(log_path: str):
+ """
+ Stream the content of a log file like `tail -f`.
+ """
+ try:
+ with open(log_path, 'r') as file:
+ while True:
+ line = file.readline()
+ if line:
+ yield line
+ else:
+ await asyncio.sleep(0.1) # Non-blocking sleep
+ except FileNotFoundError:
+ raise Exception(f"Log file not found: {log_path}")
+ except Exception as e:
+ raise Exception(f"An error occurred while reading the log: {str(e)}")
+
+# Functions to read each log file
+def read_llm_server_log():
+ try:
+ with open("logs/llm-server.log", "r") as f:
+ return f.read()
+ except FileNotFoundError:
+ return "LLM Server log file not found."
+
+def read_output_log():
+ try:
+ with open("logs/output.log", "r") as f:
+ return f.read()
+ except FileNotFoundError:
+ return "Output log file not found."
+
+# Functions to clear each log file
+def clear_llm_server_log():
+ with open("logs/llm-server.log", "w") as f:
+ f.truncate(0) # Clear contents of the file
+ return "LLM Server log cleared."
+
+def clear_output_log():
+ with open("logs/output.log", "w") as f:
+ f.truncate(0) # Clear contents of the file
+ return "Output log cleared."
+
+# Function to validate password and control access to logs
+def check_password(password, request:Request = None):
+ client_ip = request.client.host if request else "Unknown IP"
+ if password == settings.log_token:
+ # Return logs and update visibility
+ llm_log, output_log = read_llm_server_log(), read_output_log()
+ visible_update = gr.update(visible=True)
+ hidden_update = gr.update(visible=False)
+
+ # Log the successful access with the IP address
+ log.info(f"Logs accessed successfully from IP: {client_ip}")
+
+ return llm_log, output_log, visible_update, visible_update,
visible_update, hidden_update
+ else:
+ # Log the failed attempt with IP address
+ log.error(f"Incorrect password attempt from IP: {client_ip}")
+ return (
+ "",
+ "",
+ gr.update(visible=False),
+ gr.update(visible=False),
+ gr.update(visible=False),
+ gr.update(value="Incorrect password. Access denied.", visible=True)
+ )
+
+def create_log_block():
+ with gr.Blocks() as demo:
Review Comment:
unused `demo` here?
--
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]