Spaces:
Sleeping
Sleeping
Commit ·
c3317d5
1
Parent(s): 43b4779
Cập nhật hàm final_answer, bỏ luồng chat_pdf_rag
Browse files
core.py
CHANGED
|
@@ -4,11 +4,14 @@ Core agent orchestration — entry point dùng chung cho API và UI.
|
|
| 4 |
|
| 5 |
import base64
|
| 6 |
import json
|
|
|
|
| 7 |
import mimetypes
|
| 8 |
import time
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Optional
|
| 11 |
|
|
|
|
|
|
|
| 12 |
from langchain_core.messages import HumanMessage, ToolMessage
|
| 13 |
|
| 14 |
from src.graph import run
|
|
@@ -26,7 +29,6 @@ def final_answer(
|
|
| 26 |
pdf_path: Optional[str] = None,
|
| 27 |
image_path: Optional[str] = None,
|
| 28 |
pdf_name: Optional[str] = None,
|
| 29 |
-
use_rag: bool = False,
|
| 30 |
) -> tuple[str, str, str | None, str | None]:
|
| 31 |
"""
|
| 32 |
Khởi tạo AgentState, chạy graph, trả về 4 giá trị.
|
|
@@ -53,23 +55,20 @@ def final_answer(
|
|
| 53 |
custom_prompt = get_custom_prompt(sender_id)
|
| 54 |
|
| 55 |
if pdf_path is not None:
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
index_pdf(pdf_path, pdf_name or "document.pdf", conversation_id)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
tool_content = (
|
| 70 |
-
f"[Nội dung PDF]\n{pdf_content}"
|
| 71 |
-
f"\n\n[Lịch sử trò chuyện]\n{chat_text}"
|
| 72 |
-
)
|
| 73 |
|
| 74 |
state: AgentState = {
|
| 75 |
"conversation_id": conversation_id,
|
|
|
|
| 4 |
|
| 5 |
import base64
|
| 6 |
import json
|
| 7 |
+
import logging
|
| 8 |
import mimetypes
|
| 9 |
import time
|
| 10 |
from datetime import datetime
|
| 11 |
from typing import Optional
|
| 12 |
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
from langchain_core.messages import HumanMessage, ToolMessage
|
| 16 |
|
| 17 |
from src.graph import run
|
|
|
|
| 29 |
pdf_path: Optional[str] = None,
|
| 30 |
image_path: Optional[str] = None,
|
| 31 |
pdf_name: Optional[str] = None,
|
|
|
|
| 32 |
) -> tuple[str, str, str | None, str | None]:
|
| 33 |
"""
|
| 34 |
Khởi tạo AgentState, chạy graph, trả về 4 giá trị.
|
|
|
|
| 55 |
custom_prompt = get_custom_prompt(sender_id)
|
| 56 |
|
| 57 |
if pdf_path is not None:
|
| 58 |
+
# Auto-index vào Qdrant để dùng với rag_search sau này (idempotent)
|
| 59 |
+
try:
|
| 60 |
+
from src.pdf_rag import index_pdf
|
| 61 |
index_pdf(pdf_path, pdf_name or "document.pdf", conversation_id)
|
| 62 |
+
except Exception:
|
| 63 |
+
logger.exception("Auto-index PDF thất bại.")
|
| 64 |
+
|
| 65 |
+
pdf_content = pdf_to_markdown(pdf_path)
|
| 66 |
+
chat_history = redis_client.get_chat_history(conversation_id)
|
| 67 |
+
chat_text = format_chat_history(chat_history)
|
| 68 |
+
tool_content = (
|
| 69 |
+
f"[Nội dung PDF]\n{pdf_content}"
|
| 70 |
+
f"\n\n[Lịch sử trò chuyện]\n{chat_text}"
|
| 71 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
state: AgentState = {
|
| 74 |
"conversation_id": conversation_id,
|