-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_hallucination_detection.py
399 lines (323 loc) · 13.4 KB
/
advanced_hallucination_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import pandas as pd
import networkx as nx
import spacy
import wikipedia
import requests
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
BertModel,
BertTokenizer
)
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Any, Tuple
from sklearn.metrics.pairwise import cosine_similarity
class AdvancedHallucinationDetector:
def __init__(self):
# Advanced NLP models
self.nlp = spacy.load('en_core_web_trf')
# Knowledge Graph and Fact-Checking Components
self.knowledge_graph = self._build_knowledge_graph()
self.fact_checker = AdvancedFactChecker()
# Custom Hallucination Detection Transformer
self.hallucination_model = self._build_custom_hallucination_model()
# Linguistic Analysis Tools
self.linguistic_analyzer = AdvancedLinguisticAnalyzer()
def _build_knowledge_graph(self) -> nx.DiGraph:
"""
Build a comprehensive knowledge graph
:return: NetworkX Directed Graph
"""
G = nx.DiGraph()
# Integrate multiple knowledge sources
knowledge_sources = [
self._extract_wikipedia_knowledge,
self._extract_wikidata_knowledge,
self._extract_scholarly_knowledge
]
for source_extractor in knowledge_sources:
try:
source_graph = source_extractor()
G = nx.compose(G, source_graph)
except Exception as e:
print(f"Knowledge extraction error: {e}")
return G
def _extract_wikipedia_knowledge(self) -> nx.DiGraph:
"""
Extract knowledge from Wikipedia
:return: NetworkX Directed Graph
"""
G = nx.DiGraph()
# Top categories to explore
categories = [
'Science', 'History', 'Geography',
'Technology', 'Biology', 'Physics'
]
for category in categories:
try:
# Get Wikipedia pages for the category
category_pages = wikipedia.search(category, results=50)
for page_title in category_pages:
try:
page = wikipedia.page(page_title)
# Add nodes and relationships
G.add_node(page_title, content=page.summary)
# Extract and add relationships
for link in page.links[:20]: # Limit to prevent explosion
G.add_edge(page_title, link)
except (wikipedia.DisambiguationError, wikipedia.PageError):
continue
except Exception as e:
print(f"Wikipedia extraction error for {category}: {e}")
return G
def _extract_wikidata_knowledge(self) -> nx.DiGraph:
"""
Extract structured knowledge from Wikidata
:return: NetworkX Directed Graph
"""
G = nx.DiGraph()
# Wikidata SPARQL endpoint
sparql_endpoint = "https://query.wikidata.org/sparql"
# Example query for scientific concepts
query = """
SELECT ?item ?itemLabel ?description WHERE {
?item wdt:P31 wd:Q17444909. # Scientific concept
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} LIMIT 1000
"""
try:
response = requests.get(
sparql_endpoint,
params={'query': query, 'format': 'json'}
)
data = response.json()
for item in data['results']['bindings']:
concept = item['itemLabel']['value']
description = item.get('description', {}).get('value', '')
G.add_node(concept, description=description)
except Exception as e:
print(f"Wikidata knowledge extraction error: {e}")
return G
def _extract_scholarly_knowledge(self) -> nx.DiGraph:
"""
Extract knowledge from scholarly sources
:return: NetworkX Directed Graph
"""
# Simulate scholarly knowledge extraction
# In a real-world scenario, this would integrate with academic databases
G = nx.DiGraph()
scholarly_domains = [
'Quantum Physics', 'Machine Learning',
'Neuroscience', 'Climate Change'
]
for domain in scholarly_domains:
G.add_node(domain, type='scholarly_domain')
return G
def _build_custom_hallucination_model(self) -> nn.Module:
"""
Build a custom transformer model for hallucination detection
:return: PyTorch neural network model
"""
class HallucinationDetectionModel(nn.Module):
def __init__(self, input_size=768, hidden_size=256):
super().__init__()
self.bert = BertModel.from_pretrained('bert-large-uncased')
# Freeze BERT layers
for param in self.bert.parameters():
param.requires_grad = False
# Custom hallucination detection layers
self.classifier = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(hidden_size, 64),
nn.ReLU(),
nn.Linear(64, 2), # Binary classification
nn.Softmax(dim=1)
)
def forward(self, input_ids, attention_mask):
# Get BERT embeddings
outputs = self.bert(
input_ids=input_ids,
attention_mask=attention_mask
)
# Use CLS token embedding
pooled_output = outputs.pooler_output
# Classify
return self.classifier(pooled_output)
return HallucinationDetectionModel()
def detect_hallucinations(self, text: str) -> Dict[str, Any]:
"""
Comprehensive hallucination detection
:param text: Input text to analyze
:return: Detailed hallucination analysis
"""
# Knowledge graph fact-checking
kg_fact_check = self._knowledge_graph_fact_check(text)
# Linguistic analysis
linguistic_analysis = self.linguistic_analyzer.analyze(text)
# Custom transformer hallucination detection
hallucination_prob = self._custom_transformer_detection(text)
# Combine multiple signals
hallucination_score = self._combine_hallucination_signals(
kg_fact_check,
linguistic_analysis,
hallucination_prob
)
return {
'text': text,
'knowledge_graph_fact_check': kg_fact_check,
'linguistic_analysis': linguistic_analysis,
'hallucination_probability': hallucination_score
}
def _knowledge_graph_fact_check(self, text: str) -> float:
"""
Fact-check text against knowledge graph
:param text: Input text
:return: Fact-checking confidence score
"""
# Extract key entities
doc = self.nlp(text)
entities = [ent.text for ent in doc.ents]
# Check entity validity in knowledge graph
validity_scores = []
for entity in entities:
# Check if entity exists in knowledge graph
if self.knowledge_graph.has_node(entity):
# Additional checks for relationships and context
validity = len(list(self.knowledge_graph.neighbors(entity))) / len(self.knowledge_graph)
validity_scores.append(validity)
return np.mean(validity_scores) if validity_scores else 0.5
def _custom_transformer_detection(self, text: str) -> float:
"""
Use custom transformer for hallucination detection
:param text: Input text
:return: Hallucination probability
"""
# Tokenize input
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')
inputs = tokenizer(
text,
return_tensors='pt',
truncation=True,
max_length=512
)
# Predict hallucination probability
with torch.no_grad():
outputs = self.hallucination_model(
input_ids=inputs['input_ids'],
attention_mask=inputs['attention_mask']
)
# Return probability of hallucination
return outputs[0][1].item()
def _combine_hallucination_signals(
self,
kg_score: float,
linguistic_score: Dict[str, float],
transformer_score: float
) -> float:
"""
Combine multiple hallucination detection signals
:return: Final hallucination probability
"""
# Weighted combination of signals
signals = [
(kg_score, 0.3),
(linguistic_score.get('coherence', 0.5), 0.2),
(linguistic_score.get('semantic_consistency', 0.5), 0.2),
(transformer_score, 0.3)
]
return np.average(
[signal for signal, _ in signals],
weights=[weight for _, weight in signals]
)
class AdvancedFactChecker:
def __init__(self):
# Initialize fact-checking resources
self.semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
def cross_reference_sources(self, claim: str) -> float:
"""
Cross-reference claim across multiple sources
:param claim: Statement to fact-check
:return: Fact-checking confidence
"""
# Simulate cross-referencing (would integrate with multiple APIs)
sources = [
wikipedia.search(claim),
# Add more sources like academic databases, news archives
]
# Semantic similarity checking
reference_embeddings = [
self.semantic_model.encode(source) for source in sources[0]
]
claim_embedding = self.semantic_model.encode(claim)
# Calculate similarity
similarities = cosine_similarity(
claim_embedding.reshape(1, -1),
reference_embeddings
)
return np.max(similarities)
class AdvancedLinguisticAnalyzer:
def __init__(self):
self.nlp = spacy.load('en_core_web_trf')
def analyze(self, text: str) -> Dict[str, float]:
"""
Perform advanced linguistic analysis
:param text: Input text
:return: Linguistic analysis metrics
"""
doc = self.nlp(text)
return {
'coherence': self._analyze_coherence(doc),
'semantic_consistency': self._semantic_consistency(doc),
'syntactic_complexity': self._syntactic_complexity(doc),
'entity_consistency': self._entity_consistency(doc)
}
def _analyze_coherence(self, doc) -> float:
# Analyze discourse markers and transitions
discourse_markers = [
'however', 'therefore', 'consequently',
'furthermore', 'moreover'
]
marker_count = sum(1 for token in doc if token.text.lower() in discourse_markers)
return min(marker_count / len(doc), 1.0)
def _semantic_consistency(self, doc) -> float:
# Check semantic role consistency
verb_frames = [token.lemma_ for token in doc if token.pos_ == 'VERB']
unique_frames = len(set(verb_frames))
return 1 - min(unique_frames / len(verb_frames), 1.0)
def _syntactic_complexity(self, doc) -> float:
# Analyze syntactic depth and complexity
depths = [token.depth for token in doc]
return np.mean(depths) / len(doc)
def _entity_consistency(self, doc) -> float:
# Check named entity type consistency
entity_types = [ent.label_ for ent in doc.ents]
unique_types = len(set(entity_types))
return 1 - min(unique_types / len(entity_types), 1.0)
def main():
# Initialize advanced hallucination detector
hallucination_detector = AdvancedHallucinationDetector()
# Sample texts for hallucination analysis
sample_texts = [
"Quantum computers can solve all mathematical problems instantly.",
"The human brain is essentially a biological quantum computer.",
"Climate change is completely unrelated to human industrial activities.",
"Artificial intelligence will replace all human jobs by 2030."
]
# Analyze hallucinations
results = []
for text in sample_texts:
result = hallucination_detector.detect_hallucinations(text)
results.append(result)
# Display results
for result in results:
print(f"Text: {result['text']}")
print(f"Hallucination Probability: {result['hallucination_probability']}")
print("---")
if __name__ == "__main__":
main()