Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
3f73db0
1
Parent(s):
38d12c2
delete vectara
Browse files- main.py +2 -2
- workflow_nodes.py +16 -3
main.py
CHANGED
|
@@ -87,7 +87,7 @@ class AdaptiveRAGSystem:
|
|
| 87 |
"generate",
|
| 88 |
self.workflow_nodes.grade_generation_v_documents_and_question,
|
| 89 |
{
|
| 90 |
-
"not supported": "
|
| 91 |
"useful": END,
|
| 92 |
"not useful": "transform_query",
|
| 93 |
},
|
|
@@ -115,7 +115,7 @@ class AdaptiveRAGSystem:
|
|
| 115 |
print(f"\n🔍 处理问题: {question}")
|
| 116 |
print("=" * 50)
|
| 117 |
|
| 118 |
-
inputs = {"question": question}
|
| 119 |
final_generation = None
|
| 120 |
|
| 121 |
# 设置配置,增加递归限制
|
|
|
|
| 87 |
"generate",
|
| 88 |
self.workflow_nodes.grade_generation_v_documents_and_question,
|
| 89 |
{
|
| 90 |
+
"not supported": "transform_query", # 修复:有幻觉时重新转换查询,而不是再次生成
|
| 91 |
"useful": END,
|
| 92 |
"not useful": "transform_query",
|
| 93 |
},
|
|
|
|
| 115 |
print(f"\n🔍 处理问题: {question}")
|
| 116 |
print("=" * 50)
|
| 117 |
|
| 118 |
+
inputs = {"question": question, "retry_count": 0} # 初始化重试计数器
|
| 119 |
final_generation = None
|
| 120 |
|
| 121 |
# 设置配置,增加递归限制
|
workflow_nodes.py
CHANGED
|
@@ -30,10 +30,12 @@ class GraphState(TypedDict):
|
|
| 30 |
question: 问题
|
| 31 |
generation: LLM生成
|
| 32 |
documents: 文档列表
|
|
|
|
| 33 |
"""
|
| 34 |
question: str
|
| 35 |
generation: str
|
| 36 |
documents: List[str]
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
class WorkflowNodes:
|
|
@@ -74,6 +76,7 @@ class WorkflowNodes:
|
|
| 74 |
"""
|
| 75 |
print("---检索---")
|
| 76 |
question = state["question"]
|
|
|
|
| 77 |
|
| 78 |
# 使用增强检索方法,支持混合检索、查询扩展和多模态
|
| 79 |
try:
|
|
@@ -116,7 +119,7 @@ class WorkflowNodes:
|
|
| 116 |
print(f"❌ 回退检索也失败: {fallback_e}")
|
| 117 |
documents = []
|
| 118 |
|
| 119 |
-
return {"documents": documents, "question": question}
|
| 120 |
|
| 121 |
def generate(self, state):
|
| 122 |
"""
|
|
@@ -176,10 +179,13 @@ class WorkflowNodes:
|
|
| 176 |
print("---转换查询---")
|
| 177 |
question = state["question"]
|
| 178 |
documents = state["documents"]
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# 重写问题
|
| 181 |
better_question = self.graders["query_rewriter"].rewrite(question)
|
| 182 |
-
return {"documents": documents, "question": better_question}
|
| 183 |
|
| 184 |
def web_search(self, state):
|
| 185 |
"""
|
|
@@ -260,6 +266,13 @@ class WorkflowNodes:
|
|
| 260 |
question = state["question"]
|
| 261 |
documents = state["documents"]
|
| 262 |
generation = state["generation"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
score = self.graders["hallucination_grader"].grade(generation, documents)
|
| 265 |
grade = score
|
|
@@ -278,7 +291,7 @@ class WorkflowNodes:
|
|
| 278 |
print("---决策:生成没有解决问题---")
|
| 279 |
return "not useful"
|
| 280 |
else:
|
| 281 |
-
print("
|
| 282 |
return "not supported"
|
| 283 |
|
| 284 |
|
|
|
|
| 30 |
question: 问题
|
| 31 |
generation: LLM生成
|
| 32 |
documents: 文档列表
|
| 33 |
+
retry_count: 重试计数器,防止无限循环
|
| 34 |
"""
|
| 35 |
question: str
|
| 36 |
generation: str
|
| 37 |
documents: List[str]
|
| 38 |
+
retry_count: int
|
| 39 |
|
| 40 |
|
| 41 |
class WorkflowNodes:
|
|
|
|
| 76 |
"""
|
| 77 |
print("---检索---")
|
| 78 |
question = state["question"]
|
| 79 |
+
retry_count = state.get("retry_count", 0)
|
| 80 |
|
| 81 |
# 使用增强检索方法,支持混合检索、查询扩展和多模态
|
| 82 |
try:
|
|
|
|
| 119 |
print(f"❌ 回退检索也失败: {fallback_e}")
|
| 120 |
documents = []
|
| 121 |
|
| 122 |
+
return {"documents": documents, "question": question, "retry_count": retry_count}
|
| 123 |
|
| 124 |
def generate(self, state):
|
| 125 |
"""
|
|
|
|
| 179 |
print("---转换查询---")
|
| 180 |
question = state["question"]
|
| 181 |
documents = state["documents"]
|
| 182 |
+
retry_count = state.get("retry_count", 0) + 1
|
| 183 |
+
|
| 184 |
+
print(f" 重试次数: {retry_count}")
|
| 185 |
|
| 186 |
# 重写问题
|
| 187 |
better_question = self.graders["query_rewriter"].rewrite(question)
|
| 188 |
+
return {"documents": documents, "question": better_question, "retry_count": retry_count}
|
| 189 |
|
| 190 |
def web_search(self, state):
|
| 191 |
"""
|
|
|
|
| 266 |
question = state["question"]
|
| 267 |
documents = state["documents"]
|
| 268 |
generation = state["generation"]
|
| 269 |
+
retry_count = state.get("retry_count", 0)
|
| 270 |
+
|
| 271 |
+
# 检查是否超过最大重试次数
|
| 272 |
+
MAX_RETRIES = 3
|
| 273 |
+
if retry_count >= MAX_RETRIES:
|
| 274 |
+
print(f"⚠️ 已达到最大重试次数 ({MAX_RETRIES}),返回当前生成结果")
|
| 275 |
+
return "useful"
|
| 276 |
|
| 277 |
score = self.graders["hallucination_grader"].grade(generation, documents)
|
| 278 |
grade = score
|
|
|
|
| 291 |
print("---决策:生成没有解决问题---")
|
| 292 |
return "not useful"
|
| 293 |
else:
|
| 294 |
+
print("---决策:生成不基于文档,重新转换查询---")
|
| 295 |
return "not supported"
|
| 296 |
|
| 297 |
|