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

feat(llm): timely execute vid embedding & enhance some HTTP logic #141

Merged
merged 21 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions hugegraph-llm/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pyarrow~=17.0.0 # TODO: a temporary dependency for pandas, figure out why Import
pandas~=2.2.2
openpyxl~=3.1.5
pydantic-settings~=2.6.1
apscheduler~=3.10.4
MrJs133 marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 33 additions & 2 deletions hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@


import argparse
from contextlib import asynccontextmanager

import gradio as gr
import uvicorn
from fastapi import FastAPI, Depends, APIRouter
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger

from hugegraph_llm.api.admin_api import admin_http_api
from hugegraph_llm.api.rag_api import rag_http_api
from hugegraph_llm.config import admin_settings, huge_settings, prompt
Expand All @@ -40,10 +44,10 @@
from hugegraph_llm.demo.rag_demo.vector_graph_block import create_vector_graph_block
from hugegraph_llm.resources.demo.css import CSS
from hugegraph_llm.utils.log import log
from hugegraph_llm.utils.graph_index_utils import fit_vid_index

sec = HTTPBearer()


def authenticate(credentials: HTTPAuthorizationCredentials = Depends(sec)):
correct_token = admin_settings.user_token
if credentials.credentials != correct_token:
Expand All @@ -55,6 +59,33 @@ def authenticate(credentials: HTTPAuthorizationCredentials = Depends(sec)):
headers={"WWW-Authenticate": "Bearer"},
)

def schedule_fit_vid_index():
try:
log.info("Executing fit_vid_index function...")
fit_vid_index()
log.info("fit_vid_index function executed successfully.")
except Exception as e: #pylint: disable=W0718
log.error(e)

scheduler = BackgroundScheduler()

scheduler.add_job(
schedule_fit_vid_index,
CronTrigger(hour=2, minute=0),
id="fit_vid_index_job",
replace_existing=True,
)

@asynccontextmanager
async def lifespan(app: FastAPI): #pylint: disable=W0621
if not scheduler.running:
scheduler.start()
log.info("Scheduler started successfully.")

yield

scheduler.shutdown()
log.info("Scheduler shut down.")

# pylint: disable=C0301
def init_rag_ui() -> gr.Interface:
Expand Down Expand Up @@ -158,7 +189,7 @@ def refresh_ui_config_prompt() -> tuple:
parser.add_argument("--host", type=str, default="0.0.0.0", help="host")
parser.add_argument("--port", type=int, default=8001, help="port")
args = parser.parse_args()
app = FastAPI()
app = FastAPI(lifespan=lifespan)

# we don't need to manually check the env now
# settings.check_env()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def create_vector_graph_block():
graph_index_btn1 = gr.Button("Clear Graph Data & Index", size="sm")

vector_import_bt = gr.Button("Import into Vector", variant="primary")
graph_index_rebuild_bt = gr.Button("Rebuild vid Index")
graph_extract_bt = gr.Button("Extract Graph Data (1)", variant="primary")
graph_loading_bt = gr.Button("Load into GraphDB (2)", interactive=True)
graph_index_rebuild_bt = gr.Button("Rebuild vid Index")

vector_index_btn0.click(get_vector_index_info, outputs=out).then(
store_prompt,
Expand Down Expand Up @@ -122,6 +122,9 @@ def create_vector_graph_block():
graph_loading_bt.click(import_graph_data, inputs=[out, input_schema], outputs=[out]).then(
store_prompt,
inputs=[input_schema, info_extract_template],
).then(fit_vid_index).then(
store_prompt,
inputs=[input_schema, info_extract_template],
)

def on_tab_select(input_f, input_t, evt: gr.SelectData):
Expand Down
Loading