-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_verity.py
More file actions
176 lines (148 loc) · 8.94 KB
/
demo_verity.py
File metadata and controls
176 lines (148 loc) · 8.94 KB
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
#!/usr/bin/env python3
"""
Verity — Anti-Hallucination & Integrity Engine Demo
===================================================
A premium, terminal-based interactive showcase demonstrating the inner
workings of the Verity pipeline, claim extraction, multi-verifier execution,
and the self-correcting auto-retry feedback loop.
"""
import os
import sys
import time
# Ensure we can import verity
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from verity import VeritySession, VerityConfig, GateDecision, VerificationStatus
# ANSI Colors for premium terminal aesthetics
class Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
BG_DARK = '\033[40m'
def make_bar(score: float, width: int = 15) -> str:
"""Creates a visual progress bar representation of confidence."""
filled = int(round(score * width))
bar = "█" * filled + "░" * (width - filled)
if score >= 0.65:
color = Colors.GREEN
elif score >= 0.35:
color = Colors.WARNING
else:
color = Colors.FAIL
return f"{color}[{bar}] {score:.0%}{Colors.ENDC}"
def print_header(title: str):
print(f"\n{Colors.HEADER}{Colors.BOLD}╔═════════════════════════════════════════════════════════════════════════╗")
print(f"║ {title.center(71)} ║")
print(f"╚═════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}")
def print_section(title: str):
print(f"\n{Colors.CYAN}{Colors.BOLD}─── {title} ─────────────────────────────────────────────────────────────{Colors.ENDC}")
def show_result_card(result):
"""Prints a premium, structured visualization of the VerityResult."""
# Decision Tag
decision = result.decision.upper()
if decision == "PASS":
tag = f"{Colors.GREEN}{Colors.BOLD}[ PASS ]{Colors.ENDC}"
elif decision == "WARN":
tag = f"{Colors.WARNING}{Colors.BOLD}[ WARN ]{Colors.ENDC}"
else:
tag = f"{Colors.FAIL}{Colors.BOLD}[ BLOCKED ]{Colors.ENDC}"
print(f"\n ┌── {Colors.BOLD}VERITY DECISION: {tag} {Colors.ENDC}──────────────────────────────────────────┐")
print(f" │ Confidence Score : {make_bar(result.confidence, 20):<38} │")
print(f" │ Factual Claims : {result.score_report.n_claims_total:<39} │")
print(f" │ - Verified : {Colors.GREEN}{result.score_report.n_verified:<37}{Colors.ENDC} │")
print(f" │ - Unverified : {Colors.BLUE}{result.score_report.n_unverified:<37}{Colors.ENDC} │")
print(f" │ - Fabrication : {Colors.FAIL}{result.score_report.n_fabrication:<37}{Colors.ENDC} │")
# Bonuses / Penalties
if result.score_report.overconfidence_penalty > 0:
print(f" │ Penalties : {Colors.FAIL}-{result.score_report.overconfidence_penalty:.2f} (Overconfident language){Colors.ENDC:<30} │")
if result.score_report.uncertainty_bonus > 0:
print(f" │ Honesty Bonuses : {Colors.GREEN}+{result.score_report.uncertainty_bonus:.2f} (Admitted uncertainty){Colors.ENDC:<30} │")
if result.score_report.structured_bonus > 0:
print(f" │ Format Bonuses : {Colors.CYAN}+{result.score_report.structured_bonus:.2f} (Strict layout adherence){Colors.ENDC:<30} │")
print(f" │ Analysis Time : {result.elapsed_s:.3f} seconds{' ':<31} │")
print(f" └───────────────────────────────────────────────────────────────────────┘")
print_section("ANNOTATED & GATED RESPONSE")
print(result.output)
print("───────────────────────────────────────────────────────────────────────────")
def run_preset_demo():
print_header("VERITY VERIFICATION PIPELINE SHOWCASE")
print(f"Initializing dynamic {Colors.BOLD}VeritySession{Colors.ENDC} (Mock Simulator Mode)...")
# Enable all verifications for demo
config = VerityConfig(log_to_disk=True)
session = VeritySession(config=config)
# --- Scenario 1: Standard Factual Question ---
print_header("SCENARIO 1: Standard Factual Question (Expected: PASS)")
print(f"{Colors.BOLD}User:{Colors.ENDC} What is the boiling point of water?")
print(f"\n{Colors.BLUE}Processing through Verity Pipeline (Extracting, Verifying, Scoring)...{Colors.ENDC}")
time.sleep(1)
result = session.ask("What is the boiling point of water?")
show_result_card(result)
# --- Scenario 2: Dangerous Code Block Execution ---
print_header("SCENARIO 2: Dangerous Python Code Block (Expected: Safety Bypass / SKIPPED)")
print(f"{Colors.BOLD}User:{Colors.ENDC} Write some Python code to execute commands on the OS.")
print(f"\n{Colors.BLUE}Processing... Sandbox protection should catch unsafe libraries/calls...{Colors.ENDC}")
time.sleep(1.2)
result = session.ask("Write some Python code to execute commands on the OS.")
show_result_card(result)
# --- Scenario 3: Auto-Correction Feedback Loop ---
print_header("SCENARIO 3: Fact Refinement & Auto-Correction (Expected: BLOCK -> RETRY -> PASS)")
print(f"{Colors.BOLD}User:{Colors.ENDC} Tell me about Kyoto. What is its exact population?")
print(f"\n{Colors.BLUE}Sending request... The mock LLM will intentionally return an exaggerated population of 40 million. ")
print(f"Verity will intercept this, detect a fabrication risk, and trigger self-correction...{Colors.ENDC}\n")
time.sleep(1.5)
result = session.ask("Tell me about Kyoto. What is its exact population?")
show_result_card(result)
def run_interactive_chat():
print_header("VERITY INTERACTIVE CHAT MODE")
print("You are now chatting with an AI operating under the strict Verity Protocol.")
print("If you ask factual questions, the system will extract claims and audit them in real time.")
print("Type 'exit', 'quit', or 'back' to return to the main menu.\n")
session = VeritySession()
while True:
try:
query = input(f"\n{Colors.BOLD}You >{Colors.ENDC} ").strip()
if not query:
continue
if query.lower() in ("exit", "quit", "back"):
break
print(f"\n{Colors.BLUE}Analyzing response with Verity engine...{Colors.ENDC}")
result = session.ask(query)
show_result_card(result)
except KeyboardInterrupt:
print("\nExiting chat mode...")
break
except Exception as e:
print(f"\n{Colors.FAIL}Error: {e}{Colors.ENDC}")
def main():
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print(f"""{Colors.CYAN}{Colors.BOLD}
██╗ ██╗███████╗██████╗ ██╗████████╗██╗ ██╗
██║ ██║██╔════╝██╔══██╗██║╚══██╔══╝╚██╗ ██╔╝
██║ ██║█████╗ ██████╔╝██║ ██║ ╚████╔╝
╚██╗ ██╔╝██╔══╝ ██╔══██╗██║ ██║ ╚██╔╝
╚████╔╝ ███████╗██║ ██║██║ ██║ ██║
╚═══╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
HONESTY, INTEGRITY & ANTI-HALLUCINATION
{Colors.ENDC}""")
print(f" {Colors.BOLD}METAPRIME Quantum & AI Integrity Ecosystem{Colors.ENDC}\n")
print(" Select an option:")
print(f" [1] {Colors.BOLD}Run Full Pipeline Showcase{Colors.ENDC} (Recommended - preset scenarios)")
print(f" [2] {Colors.BOLD}Open Interactive Chat{Colors.ENDC} (Custom queries & live audit)")
print(f" [3] {Colors.BOLD}Exit{Colors.ENDC}\n")
choice = input(" Choice > ").strip()
if choice == "1":
run_preset_demo()
input(f"\n{Colors.GREEN}Showcase complete. Press Enter to return to main menu...{Colors.ENDC}")
elif choice == "2":
run_interactive_chat()
elif choice == "3":
print("\nExiting. Stay truthful.")
break
if __name__ == "__main__":
main()