Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pr_agent/algo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from pr_agent.config_loader import get_settings, global_settings
from pr_agent.log import get_logger

_MAX_HASH_INT_INV = 1.0 / ((1 << 256) - 1)


def get_model(model_type: str = "model_weak") -> str:
if model_type == "model_weak" and get_settings().get("config.model_weak"):
Expand Down Expand Up @@ -1308,12 +1310,11 @@ def string_to_uniform_number(s: str) -> float:
The uniform distribution is achieved by the nature of the SHA-256 hash function, which produces a uniformly distributed hash value over its output space.
"""
# Generate a hash of the string
hash_object = hashlib.sha256(s.encode())
# Convert the hash to an integer
hash_int = int(hash_object.hexdigest(), 16)
# Normalize the integer to the range [0, 1]
max_hash_int = 2 ** 256 - 1
uniform_number = float(hash_int) / max_hash_int
hash_bytes = hashlib.sha256(s.encode()).digest()
# Convert the hash to an integer directly from bytes
hash_int = int.from_bytes(hash_bytes, 'big')
# Normalize the integer to the range [0, 1] using precomputed inverse
uniform_number = hash_int * _MAX_HASH_INT_INV
return uniform_number


Expand Down