anhkhoiphan commited on
Commit
c3317d5
·
1 Parent(s): 43b4779

Cập nhật hàm final_answer, bỏ luồng chat_pdf_rag

Browse files
Files changed (1) hide show
  1. core.py +16 -17
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
- if use_rag:
57
- from src.pdf_rag import hybrid_search, index_pdf
 
58
  index_pdf(pdf_path, pdf_name or "document.pdf", conversation_id)
59
- chunks = hybrid_search(query, conversation_id, top_k=5)
60
- rag_context = (
61
- "\n\n---\n\n".join(chunks)
62
- if chunks else "(Không tìm thấy nội dung liên quan trong các PDF đã gửi)"
63
- )
64
- tool_content = f"[Nội dung liên quan từ PDF]\n{rag_context}"
65
- else:
66
- pdf_content = pdf_to_markdown(pdf_path)
67
- chat_history = redis_client.get_chat_history(conversation_id)
68
- chat_text = format_chat_history(chat_history)
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,