Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
38d12c2
1
Parent(s):
11e6cae
delete vectara
Browse files- hallucination_detector.py +69 -0
hallucination_detector.py
CHANGED
|
@@ -95,6 +95,38 @@ class VectaraHallucinationDetector:
|
|
| 95 |
except Exception as e:
|
| 96 |
print(f"❌ Vectara 检测失败: {e}")
|
| 97 |
return {"has_hallucination": False, "hallucination_score": 0.0, "factuality_score": 1.0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
class NLIHallucinationDetector:
|
|
@@ -250,6 +282,43 @@ class NLIHallucinationDetector:
|
|
| 250 |
"entailment_count": entailment_count,
|
| 251 |
"problematic_sentences": problematic_sentences
|
| 252 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
class HybridHallucinationDetector:
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
print(f"❌ Vectara 检测失败: {e}")
|
| 97 |
return {"has_hallucination": False, "hallucination_score": 0.0, "factuality_score": 1.0}
|
| 98 |
+
|
| 99 |
+
def grade(self, generation: str, documents) -> str:
|
| 100 |
+
"""
|
| 101 |
+
兼容原有接口的检测方法
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
generation: LLM 生成的内容
|
| 105 |
+
documents: 参考文档(可以是字符串或列表)
|
| 106 |
+
|
| 107 |
+
Returns:
|
| 108 |
+
"yes" 表示无幻觉,"no" 表示有幻觉
|
| 109 |
+
"""
|
| 110 |
+
# 处理文档格式
|
| 111 |
+
if isinstance(documents, list):
|
| 112 |
+
doc_text = "\n\n".join([
|
| 113 |
+
doc.page_content if hasattr(doc, 'page_content') else str(doc)
|
| 114 |
+
for doc in documents
|
| 115 |
+
])
|
| 116 |
+
else:
|
| 117 |
+
doc_text = str(documents)
|
| 118 |
+
|
| 119 |
+
# 检测幻觉
|
| 120 |
+
result = self.detect(generation, doc_text)
|
| 121 |
+
|
| 122 |
+
# 打印详细信息
|
| 123 |
+
if result['has_hallucination']:
|
| 124 |
+
print(f"⚠️ Vectara 检测到幻觉 (得分: {result['hallucination_score']:.2f})")
|
| 125 |
+
else:
|
| 126 |
+
print(f"✅ Vectara 未检测到幻觉 (真实性得分: {result['factuality_score']:.2f})")
|
| 127 |
+
|
| 128 |
+
# 返回兼容格式
|
| 129 |
+
return "no" if result['has_hallucination'] else "yes"
|
| 130 |
|
| 131 |
|
| 132 |
class NLIHallucinationDetector:
|
|
|
|
| 282 |
"entailment_count": entailment_count,
|
| 283 |
"problematic_sentences": problematic_sentences
|
| 284 |
}
|
| 285 |
+
|
| 286 |
+
def grade(self, generation: str, documents) -> str:
|
| 287 |
+
"""
|
| 288 |
+
兼容原有接口的检测方法
|
| 289 |
+
|
| 290 |
+
Args:
|
| 291 |
+
generation: LLM 生成的内容
|
| 292 |
+
documents: 参考文档(可以是字符串或列表)
|
| 293 |
+
|
| 294 |
+
Returns:
|
| 295 |
+
"yes" 表示无幻觉,"no" 表示有幻觉
|
| 296 |
+
"""
|
| 297 |
+
# 处理文档格式
|
| 298 |
+
if isinstance(documents, list):
|
| 299 |
+
doc_text = "\n\n".join([
|
| 300 |
+
doc.page_content if hasattr(doc, 'page_content') else str(doc)
|
| 301 |
+
for doc in documents
|
| 302 |
+
])
|
| 303 |
+
else:
|
| 304 |
+
doc_text = str(documents)
|
| 305 |
+
|
| 306 |
+
# 检测幻觉
|
| 307 |
+
result = self.detect(generation, doc_text)
|
| 308 |
+
|
| 309 |
+
# 打印详细信息
|
| 310 |
+
if result['has_hallucination']:
|
| 311 |
+
print(f"⚠️ NLI 检测到幻觉")
|
| 312 |
+
print(f" 矛盾句子: {result['contradiction_count']}")
|
| 313 |
+
print(f" 中立句子: {result['neutral_count']}")
|
| 314 |
+
print(f" 蕴含句子: {result['entailment_count']}")
|
| 315 |
+
if result['problematic_sentences']:
|
| 316 |
+
print(f" 问题句子: {result['problematic_sentences'][:2]}")
|
| 317 |
+
else:
|
| 318 |
+
print(f"✅ NLI 未检测到幻觉")
|
| 319 |
+
|
| 320 |
+
# 返回兼容格式
|
| 321 |
+
return "no" if result['has_hallucination'] else "yes"
|
| 322 |
|
| 323 |
|
| 324 |
class HybridHallucinationDetector:
|