Create consensus/metrics.py
Browse files- consensus/metrics.py +23 -0
consensus/metrics.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float:
|
| 4 |
+
a = a / (np.linalg.norm(a) + 1e-12)
|
| 5 |
+
b = b / (np.linalg.norm(b) + 1e-12)
|
| 6 |
+
return float(a @ b)
|
| 7 |
+
|
| 8 |
+
def trinary_quantize(x: np.ndarray, threshold: float) -> np.ndarray:
|
| 9 |
+
"""
|
| 10 |
+
Map to {-1, 0, +1}. Symmetric threshold around 0.
|
| 11 |
+
"""
|
| 12 |
+
out = np.zeros_like(x, dtype=np.int8)
|
| 13 |
+
out[x > threshold] = 1
|
| 14 |
+
out[x < -threshold] = -1
|
| 15 |
+
return out
|
| 16 |
+
|
| 17 |
+
def weighted_average_text(responses: list[str], weights: np.ndarray) -> str:
|
| 18 |
+
"""
|
| 19 |
+
Simple consensus surface: pick the response with max weight (argmax).
|
| 20 |
+
Placeholder; could switch to RLAIF-style merge, majority vote over facts, etc.
|
| 21 |
+
"""
|
| 22 |
+
idx = int(np.argmax(weights))
|
| 23 |
+
return responses[idx]
|