Generate a deterministic pseudo-vector from the detected directions.
(core_directions: Dict[str, float])
| 1621 | |
| 1622 | |
| 1623 | def generate_interest_vector(core_directions: Dict[str, float]) -> List[float]: |
| 1624 | """Generate a deterministic pseudo-vector from the detected directions.""" |
| 1625 | try: |
| 1626 | target_dim = max(1, int(str(os.environ.get("EMBEDDING_DIMENSIONS", "768")).strip())) |
| 1627 | except ValueError: |
| 1628 | target_dim = 768 |
| 1629 | |
| 1630 | direction_str = "_".join(sorted(core_directions.keys())) |
| 1631 | hash_bytes = hashlib.sha256(direction_str.encode()).digest() |
| 1632 | vector = [] |
| 1633 | |
| 1634 | for index in range(target_dim): |
| 1635 | byte_index = index % 32 |
| 1636 | sign = 1 if (hash_bytes[byte_index] & (1 << (index % 8))) else -1 |
| 1637 | value = sign * ((hash_bytes[byte_index] >> (index % 8)) & 1) |
| 1638 | vector.append(float(value)) |
| 1639 | |
| 1640 | norm = sum(value * value for value in vector) ** 0.5 |
| 1641 | if norm > 0: |
| 1642 | vector = [value / norm for value in vector] |
| 1643 | |
| 1644 | return vector |
| 1645 | |
| 1646 | |
| 1647 | def format_profile_card(profile: Dict[str, Any], user_id: str = "user_001") -> str: |
no test coverage detected