truglpk3 commited on
Commit
d324b6f
·
1 Parent(s): 445252b

User SQRetriever fo r PolicyDoc + Optimize Prompt Meal Suggestion

Browse files
chatbot/agents/nodes/chatbot/classify_topic.py CHANGED
@@ -31,7 +31,7 @@ def classify_topic(state: AgentState):
31
  1. "meal_suggestion": Gợi ý thực đơn ăn uống.
32
  2. "food_suggestion": Tìm món ăn cụ thể.
33
  3. "food_query": Hỏi thông tin dinh dưỡng món ăn.
34
- 4. "policy": Khi người dùng hỏi về quy định, chính sách, hướng dẫn sử dụng MỚI mà chưa có trong lịch sử.
35
  5. "general_chat":
36
  - Chào hỏi xã giao.
37
  - Các câu hỏi sức khỏe chung chung.
 
31
  1. "meal_suggestion": Gợi ý thực đơn ăn uống.
32
  2. "food_suggestion": Tìm món ăn cụ thể.
33
  3. "food_query": Hỏi thông tin dinh dưỡng món ăn.
34
+ 4. "policy": Khi người dùng hỏi về thông tin, đặc điểm, quy định, chính sách, hướng dẫn sử dụng MỚI mà chưa có trong lịch sử (liên quan đến app).
35
  5. "general_chat":
36
  - Chào hỏi xã giao.
37
  - Các câu hỏi sức khỏe chung chung.
chatbot/agents/nodes/chatbot/food_suggestion.py CHANGED
@@ -23,8 +23,8 @@ def food_suggestion(state: AgentState):
23
  Hãy tìm các món ăn phù hợp với khẩu phần và yêu cầu này, cho phép sai lệch không quá 20%.
24
  """
25
 
26
- query_ans = query_constructor.invoke(prompt)
27
- logger.info(f"🔍 Dạng truy vấn: {food_retriever.structured_query_translator.visit_structured_query(structured_query=query_ans)}")
28
  foods = food_retriever.invoke(prompt)
29
  logger.info(f"🔍 Kết quả truy vấn: ")
30
  for i, food in enumerate(foods):
 
23
  Hãy tìm các món ăn phù hợp với khẩu phần và yêu cầu này, cho phép sai lệch không quá 20%.
24
  """
25
 
26
+ # query_ans = query_constructor.invoke(prompt)
27
+ # logger.info(f"🔍 Dạng truy vấn: {food_retriever.structured_query_translator.visit_structured_query(structured_query=query_ans)}")
28
  foods = food_retriever.invoke(prompt)
29
  logger.info(f"🔍 Kết quả truy vấn: ")
30
  for i, food in enumerate(foods):
chatbot/agents/nodes/chatbot/policy.py CHANGED
@@ -39,6 +39,7 @@ QUY TẮC AN TOÀN:
39
  2. Không được tự bịa ra chính sách hoặc đoán mò.
40
  3. Trả lời ngắn gọn, đi thẳng vào vấn đề.
41
  """
 
42
 
43
  try:
44
  response = llm.invoke([
 
39
  2. Không được tự bịa ra chính sách hoặc đoán mò.
40
  3. Trả lời ngắn gọn, đi thẳng vào vấn đề.
41
  """
42
+ logger.info(f"Prompt gửi đến LLM cho chính sách: {system_prompt}")
43
 
44
  try:
45
  response = llm.invoke([
chatbot/agents/tools/__pycache__/food_retriever.cpython-310.pyc CHANGED
Binary files a/chatbot/agents/tools/__pycache__/food_retriever.cpython-310.pyc and b/chatbot/agents/tools/__pycache__/food_retriever.cpython-310.pyc differ
 
chatbot/agents/tools/info_app_retriever.py CHANGED
@@ -1,8 +1,174 @@
 
 
 
 
 
 
1
  from langchain_elasticsearch import ElasticsearchStore
 
 
 
2
 
3
  from chatbot.models.embeddings import embeddings
 
4
  from chatbot.config import ELASTIC_CLOUD_URL, ELASTIC_API_KEY, POLICY_DB_INDEX
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  policy_search = ElasticsearchStore(
7
  es_url=ELASTIC_CLOUD_URL,
8
  es_api_key=ELASTIC_API_KEY,
@@ -10,8 +176,15 @@ policy_search = ElasticsearchStore(
10
  embedding=embeddings,
11
  )
12
 
13
- policy_retriever = policy_search.as_retriever(
14
- search_kwargs={"k": 3}
 
 
 
 
 
 
 
15
  )
16
 
17
  __all__ = ["policy_retriever", "policy_search"]
 
1
+ from langchain.chains.query_constructor.base import (
2
+ AttributeInfo,
3
+ get_query_constructor_prompt,
4
+ StructuredQueryOutputParser,
5
+ )
6
+ from langchain_deepseek import ChatDeepSeek
7
  from langchain_elasticsearch import ElasticsearchStore
8
+ from langchain.retrievers.self_query.elasticsearch import ElasticsearchTranslator
9
+ from langchain.chains.query_constructor.base import load_query_constructor_runnable
10
+ from langchain.retrievers.self_query.base import SelfQueryRetriever
11
 
12
  from chatbot.models.embeddings import embeddings
13
+ from chatbot.models.llm_setup import llm
14
  from chatbot.config import ELASTIC_CLOUD_URL, ELASTIC_API_KEY, POLICY_DB_INDEX
15
 
16
+ # ========================================
17
+ # 1️⃣ Định nghĩa metadata field info
18
+ # ========================================
19
+ metadata_field_info = [
20
+ AttributeInfo(
21
+ name="doc_type",
22
+ description=(
23
+ "Thể loại tài liệu của ứng dụng. "
24
+ "Ví dụ: "
25
+ "'product_overview' (giới thiệu ứng dụng), "
26
+ "'product_features' (chức năng & cách sử dụng), "
27
+ "'policy_and_disclaimer' (chính sách, điều khoản, miễn trừ), "
28
+ "'organization_info' (thông tin đội ngũ, liên hệ)."
29
+ ),
30
+ type="string",
31
+ ),
32
+ AttributeInfo(
33
+ name="source",
34
+ description=(
35
+ "Nguồn nội dung của tài liệu."
36
+ ),
37
+ type="string",
38
+ ),
39
+ ]
40
+
41
+ document_content_description = (
42
+ "Nội dung tài liệu mô tả ứng dụng gợi ý món ăn và dinh dưỡng, "
43
+ "bao gồm giới thiệu ứng dụng, chức năng, chính sách sử dụng, "
44
+ "tuyên bố miễn trừ trách nhiệm, thông tin đội ngũ phát triển "
45
+ "và các câu hỏi thường gặp."
46
+ )
47
+
48
+ # ========================================
49
+ # 2️⃣ Định nghĩa toán tử hỗ trợ và ví dụ
50
+ # ========================================
51
+ allowed_comparators = [
52
+ "eq", # Equal
53
+ "gt", # Greater than
54
+ "gte", # Greater than or equal
55
+ "lt", # Less than
56
+ "lte", # Less than or equal
57
+ "contain", # Chứa (dùng cho list)
58
+ "like" # Giống (dùng cho string pattern)
59
+ ]
60
+
61
+ examples = [
62
+
63
+ # --- NHÓM 1: GIỚI THIỆU ỨNG DỤNG ---
64
+ (
65
+ "Ứng dụng này dùng để làm gì?",
66
+ {
67
+ "query": "giới thiệu và mục đích của ứng dụng gợi ý món ăn",
68
+ "filter": 'eq("doc_type", "product_overview")',
69
+ },
70
+ ),
71
+ (
72
+ "App này dành cho đối tượng nào?",
73
+ {
74
+ "query": "đối tượng sử dụng và phạm vi ứng dụng",
75
+ "filter": 'eq("doc_type", "product_overview")',
76
+ },
77
+ ),
78
+
79
+ # --- NHÓM 2: CHỨC NĂNG & CÁCH SỬ DỤNG ---
80
+ (
81
+ "Ứng dụng có những chức năng gì?",
82
+ {
83
+ "query": "các chức năng chính của ứng dụng",
84
+ "filter": 'eq("doc_type", "product_features")',
85
+ },
86
+ ),
87
+ (
88
+ "Chatbot AI trong app dùng như thế nào?",
89
+ {
90
+ "query": "cách sử dụng chatbot AI trong ứng dụng",
91
+ "filter": 'eq("doc_type", "product_features")',
92
+ },
93
+ ),
94
+
95
+ # --- NHÓM 3: CHÍNH SÁCH & AN TOÀN ---
96
+ (
97
+ "Ứng dụng có bảo mật dữ liệu cá nhân không?",
98
+ {
99
+ "query": "chính sách quyền riêng tư và bảo mật dữ liệu",
100
+ "filter": 'eq("doc_type", "policy_and_disclaimer")',
101
+ },
102
+ ),
103
+ (
104
+ "Chatbot có thay thế chuyên gia dinh dưỡng không?",
105
+ {
106
+ "query": "tuyên bố miễn trừ trách nhiệm về AI và dinh dưỡng",
107
+ "filter": 'eq("doc_type", "policy_and_disclaimer")',
108
+ },
109
+ ),
110
+ (
111
+ "Thông tin trong app có đáng tin không?",
112
+ {
113
+ "query": "giới hạn trách nhiệm và phạm vi sử dụng thông tin",
114
+ "filter": 'eq("doc_type", "policy_and_disclaimer")',
115
+ },
116
+ ),
117
+
118
+ # --- NHÓM 4: ĐỘI NGŨ & LIÊN HỆ ---
119
+ (
120
+ "Ai là người phát triển ứng dụng này?",
121
+ {
122
+ "query": "thông tin người sáng lập và đội ngũ phát triển",
123
+ "filter": 'eq("doc_type", "organization_info")',
124
+ },
125
+ ),
126
+ (
127
+ "Tôi cần liên hệ hỗ trợ ở đâu?",
128
+ {
129
+ "query": "thông tin liên hệ và hỗ trợ người dùng",
130
+ "filter": 'eq("doc_type", "organization_info")',
131
+ },
132
+ ),
133
+ (
134
+ "Ứng dụng này có uy tín không?",
135
+ {
136
+ "query": "giới thiệu đội ngũ phát triển và mục tiêu ứng dụng",
137
+ "filter": 'eq("doc_type", "organization_info")',
138
+ },
139
+ ),
140
+
141
+ ]
142
+
143
+ # ========================================
144
+ # 3️⃣ Tạo Query Constructor
145
+ # ========================================
146
+ prompt_query = get_query_constructor_prompt(
147
+ document_content_description,
148
+ metadata_field_info,
149
+ allowed_comparators=allowed_comparators,
150
+ examples=examples,
151
+ )
152
+
153
+ llm = ChatDeepSeek(
154
+ model="deepseek-chat",
155
+ temperature=0,
156
+ max_tokens=None,
157
+ timeout=None,
158
+ max_retries=2
159
+ )
160
+
161
+ query_constructor = load_query_constructor_runnable(
162
+ llm=llm,
163
+ document_contents=document_content_description,
164
+ attribute_info=metadata_field_info,
165
+ examples=examples,
166
+ allowed_comparators=allowed_comparators
167
+ )
168
+
169
+ # ========================================
170
+ # 4️⃣ Kết nối Elasticsearch
171
+ # ========================================
172
  policy_search = ElasticsearchStore(
173
  es_url=ELASTIC_CLOUD_URL,
174
  es_api_key=ELASTIC_API_KEY,
 
176
  embedding=embeddings,
177
  )
178
 
179
+ # ========================================
180
+ # 5️⃣ Tạo retrievers
181
+ # ========================================
182
+
183
+ policy_retriever = SelfQueryRetriever(
184
+ query_constructor=query_constructor,
185
+ vectorstore=policy_search,
186
+ structured_query_translator=ElasticsearchTranslator(),
187
+ search_kwargs={"k": 3},
188
  )
189
 
190
  __all__ = ["policy_retriever", "policy_search"]