Spaces:
Runtime error
Runtime error
Commit
·
530a737
1
Parent(s):
4f7e10b
update: access in mobile
Browse files- rag_components.py +1 -102
- routers/user.py +2 -2
- services/auth_service.py +2 -2
- services/user_service.py +4 -4
rag_components.py
CHANGED
@@ -378,108 +378,7 @@ def create_llm_from_google_key_list(google_api_keys: List[str]):
|
|
378 |
|
379 |
|
380 |
|
381 |
-
|
382 |
-
# llm: Any,
|
383 |
-
# retriever: Any, # Nhận retriever nâng cao đã được khởi tạo
|
384 |
-
# process_input_llm: Any = None
|
385 |
-
# ):
|
386 |
-
# """
|
387 |
-
# PHIÊN BẢN CUỐI CÙNG: Tạo ra một RAG chain hoàn chỉnh, tối ưu hóa với:
|
388 |
-
# 1. Unified Pre-processing: Một lệnh gọi LLM để hiểu lịch sử, "dịch" thuật ngữ, và phân loại.
|
389 |
-
# 2. Multi-route: Định tuyến thông minh đến các nhánh xử lý chuyên biệt.
|
390 |
-
# 3. Advanced Retriever: Sử dụng retriever tùy chỉnh cho nhánh pháp luật.
|
391 |
-
# """
|
392 |
-
# if not all([llm, retriever]):
|
393 |
-
# logger.error("🔸 Thiếu LLM hoặc Retriever chính để tạo QA Chain.")
|
394 |
-
# return None
|
395 |
-
|
396 |
-
# try:
|
397 |
-
# logger.info("🔸 Bắt đầu tạo QA Chain Tối ưu (phiên bản cuối cùng)...")
|
398 |
-
|
399 |
-
# # LLM cho bước tiền xử lý (thường là model mạnh nhất)
|
400 |
-
# preprocessing_llm = process_input_llm or llm
|
401 |
-
|
402 |
-
# # ----- PROMPTS (Sử dụng các phiên bản đã cải tiến) -----
|
403 |
-
|
404 |
-
# # 1. Prompt tiền xử lý hợp nhất
|
405 |
-
# # Sử dụng phiên bản V5 mạnh mẽ nhất để "dịch" thuật ngữ hiệu quả
|
406 |
-
# unified_preprocessing_prompt = ChatPromptTemplate.from_template(
|
407 |
-
# prompt_templete.UNIFIED_PREPROCESSING_PROMPT
|
408 |
-
# )
|
409 |
-
|
410 |
-
# # 2. Prompt để tạo câu trả lời RAG từ context
|
411 |
-
# # Sử dụng phiên bản V4 để "dạy" LLM cách phân tích và ưu tiên thông tin
|
412 |
-
# qa_prompt = ChatPromptTemplate.from_template(
|
413 |
-
# prompt_templete.QA_PROMPT_TEMPLATE
|
414 |
-
# )
|
415 |
-
|
416 |
-
# # 3. Các prompt cho các nhánh khác
|
417 |
-
# persona_prompt = ChatPromptTemplate.from_messages([
|
418 |
-
# ("system", prompt_templete.GENERAL_PROMPT),
|
419 |
-
# ("human", "{input}")
|
420 |
-
# ])
|
421 |
-
|
422 |
-
|
423 |
-
# # ----- STEP 1: UNIFIED PREPROCESSING CHAIN -----
|
424 |
-
# # Đây là bộ não xử lý đầu vào, thay thế cho 3 lệnh gọi LLM cũ
|
425 |
-
# unified_preprocessing_chain = (
|
426 |
-
# unified_preprocessing_prompt
|
427 |
-
# | preprocessing_llm
|
428 |
-
# | JsonOutputParser()
|
429 |
-
# ).with_config({"run_name": "UnifiedQuestionPreprocessor"})
|
430 |
-
|
431 |
-
# # ----- STEP 2: DEFINE BRANCHES (CÁC NHÁNH XỬ LÝ) -----
|
432 |
-
|
433 |
-
# # --- Nhánh 1: LEGAL (RAG) ---
|
434 |
-
# # Sử dụng retriever nâng cao đã được truyền vào
|
435 |
-
# legal_chain = (
|
436 |
-
# # `retriever` nhận `rewritten_question` từ dict đầu vào
|
437 |
-
# RunnablePassthrough.assign(context=itemgetter("rewritten_question") | retriever)
|
438 |
-
# # Chuẩn bị input cho qa_prompt cuối cùng
|
439 |
-
# .assign(input=itemgetter("rewritten_question"))
|
440 |
-
# | {
|
441 |
-
# "answer": qa_prompt | llm | StrOutputParser(),
|
442 |
-
# "context": itemgetter("context") # Giữ lại context để có thể hiển thị nguồn
|
443 |
-
# }
|
444 |
-
# ).with_config({"run_name": "AdvancedLegalRAGChain"})
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
# # --- Nhánh 3: GENERAL CHAT ---
|
449 |
-
# general_chat_chain = (
|
450 |
-
# {"input": itemgetter("rewritten_question")}
|
451 |
-
# | persona_prompt
|
452 |
-
# | llm
|
453 |
-
# | StrOutputParser()
|
454 |
-
# | (lambda answer: {"answer": answer, "context": []})
|
455 |
-
# ).with_config({"run_name": "GeneralChatChain"})
|
456 |
-
|
457 |
-
# # ----- STEP 3: ROUTER -----
|
458 |
-
# # Định nghĩa các nhánh mà router có thể chọn
|
459 |
-
# branches = {
|
460 |
-
# "legal_rag": legal_chain,
|
461 |
-
# "general_chat": general_chat_chain,
|
462 |
-
# # Thêm nhánh legal_term_explanation ở đây nếu bạn triển khai nó
|
463 |
-
# }
|
464 |
-
|
465 |
-
# def route_branches(info: dict):
|
466 |
-
# """Hàm định tuyến, chọn chain phù hợp dựa trên kết quả phân loại."""
|
467 |
-
# classification = info.get("classification", "general_chat")
|
468 |
-
# logger.info(f"Routing to branch: '{classification}'")
|
469 |
-
# # Chọn chain, mặc định là general_chat nếu có lỗi
|
470 |
-
# return branches.get(classification, general_chat_chain)
|
471 |
-
|
472 |
-
# # ----- STEP 4: FULL CHAIN -----
|
473 |
-
# # Kết hợp thành một chuỗi xử lý duy nhất và liền mạch
|
474 |
-
# # Luồng: Input -> Tiền xử lý (Viết lại + Phân loại) -> Router -> Chạy nhánh được chọn
|
475 |
-
# full_chain = unified_preprocessing_chain | RunnableLambda(route_branches)
|
476 |
-
|
477 |
-
# logger.info("✅ Successfully created Final Optimized QA Chain.")
|
478 |
-
# return full_chain
|
479 |
-
|
480 |
-
# except Exception as e:
|
481 |
-
# logger.error(f"❌ Error creating QA Chain: {e}", exc_info=True)
|
482 |
-
# return None
|
483 |
|
484 |
|
485 |
#new update
|
|
|
378 |
|
379 |
|
380 |
|
381 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
|
383 |
|
384 |
#new update
|
routers/user.py
CHANGED
@@ -393,7 +393,7 @@ async def exchange_google_code_for_token(request: Request,response: Response, co
|
|
393 |
max_age=int(access_token_expires.total_seconds()), # Thời gian sống bằng access token
|
394 |
httponly=True,
|
395 |
secure=IS_PRODUCTION, # True trong production (HTTPS), False khi dev với HTTP
|
396 |
-
samesite="
|
397 |
path="/",
|
398 |
)
|
399 |
|
@@ -404,7 +404,7 @@ async def exchange_google_code_for_token(request: Request,response: Response, co
|
|
404 |
max_age=int(refresh_token_expires.total_seconds()),
|
405 |
httponly=True,
|
406 |
secure=IS_PRODUCTION, # True trong production, False khi dev với HTTP
|
407 |
-
samesite="
|
408 |
path="/api/user/refresh-token",
|
409 |
)
|
410 |
|
|
|
393 |
max_age=int(access_token_expires.total_seconds()), # Thời gian sống bằng access token
|
394 |
httponly=True,
|
395 |
secure=IS_PRODUCTION, # True trong production (HTTPS), False khi dev với HTTP
|
396 |
+
samesite="none", # Hoặc "strict"
|
397 |
path="/",
|
398 |
)
|
399 |
|
|
|
404 |
max_age=int(refresh_token_expires.total_seconds()),
|
405 |
httponly=True,
|
406 |
secure=IS_PRODUCTION, # True trong production, False khi dev với HTTP
|
407 |
+
samesite="none",
|
408 |
path="/api/user/refresh-token",
|
409 |
)
|
410 |
|
services/auth_service.py
CHANGED
@@ -127,8 +127,8 @@ async def logout_user(req: Request, credentials: HTTPAuthorizationCredentials =
|
|
127 |
value="",
|
128 |
max_age=0, # Expire cookie immediately
|
129 |
httponly=True,
|
130 |
-
secure=
|
131 |
-
samesite="
|
132 |
path="/",
|
133 |
)
|
134 |
|
|
|
127 |
value="",
|
128 |
max_age=0, # Expire cookie immediately
|
129 |
httponly=True,
|
130 |
+
secure=True, # Set to False for local development
|
131 |
+
samesite="none",
|
132 |
path="/",
|
133 |
)
|
134 |
|
services/user_service.py
CHANGED
@@ -492,7 +492,7 @@ async def verify_login_code(email: str, code: str, res: Response): # Bỏ kiểu
|
|
492 |
max_age=int(access_token_expires.total_seconds()), # Thời gian sống bằng access token
|
493 |
httponly=True,
|
494 |
secure=IS_PRODUCTION, # True trong production (HTTPS), False khi dev với HTTP
|
495 |
-
samesite="
|
496 |
path="/",
|
497 |
)
|
498 |
|
@@ -503,7 +503,7 @@ async def verify_login_code(email: str, code: str, res: Response): # Bỏ kiểu
|
|
503 |
max_age=int(refresh_token_expires.total_seconds()),
|
504 |
httponly=True,
|
505 |
secure=IS_PRODUCTION, # True trong production, False khi dev với HTTP
|
506 |
-
samesite="
|
507 |
path="/api/user/refresh-token",
|
508 |
)
|
509 |
# --------------------
|
@@ -660,7 +660,7 @@ async def refresh_access_token(req: Request , res: Response ) -> dict:
|
|
660 |
max_age= access_token_expires, # tính bằng giây
|
661 |
httponly=True,
|
662 |
secure=IS_PRODUCTION,
|
663 |
-
samesite='
|
664 |
path='/',
|
665 |
)
|
666 |
|
@@ -671,7 +671,7 @@ async def refresh_access_token(req: Request , res: Response ) -> dict:
|
|
671 |
max_age=7 * 24 * 60 * 60, # 7 days
|
672 |
httponly=True,
|
673 |
secure=IS_PRODUCTION, # Set to False for local development
|
674 |
-
samesite="
|
675 |
path="/api/user/refresh-token",
|
676 |
)
|
677 |
|
|
|
492 |
max_age=int(access_token_expires.total_seconds()), # Thời gian sống bằng access token
|
493 |
httponly=True,
|
494 |
secure=IS_PRODUCTION, # True trong production (HTTPS), False khi dev với HTTP
|
495 |
+
samesite="none", # Hoặc "strict"
|
496 |
path="/",
|
497 |
)
|
498 |
|
|
|
503 |
max_age=int(refresh_token_expires.total_seconds()),
|
504 |
httponly=True,
|
505 |
secure=IS_PRODUCTION, # True trong production, False khi dev với HTTP
|
506 |
+
samesite="none",
|
507 |
path="/api/user/refresh-token",
|
508 |
)
|
509 |
# --------------------
|
|
|
660 |
max_age= access_token_expires, # tính bằng giây
|
661 |
httponly=True,
|
662 |
secure=IS_PRODUCTION,
|
663 |
+
samesite='none', # type: ignore
|
664 |
path='/',
|
665 |
)
|
666 |
|
|
|
671 |
max_age=7 * 24 * 60 * 60, # 7 days
|
672 |
httponly=True,
|
673 |
secure=IS_PRODUCTION, # Set to False for local development
|
674 |
+
samesite="none", # CSRF protection
|
675 |
path="/api/user/refresh-token",
|
676 |
)
|
677 |
|