File size: 1,050 Bytes
8e874f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Hashing utilities for QAFD-RAG.

Provides MD5-based hashing functions for content identification and caching.
"""

from hashlib import md5


def compute_args_hash(*args) -> str:
    """
    Compute MD5 hash from arbitrary arguments.

    Used for cache key generation from function arguments.

    Parameters:
    -----------
    *args : Any
        Arguments to hash

    Returns:
    --------
    str
        Hexadecimal MD5 hash string
    """
    return md5(str(args).encode()).hexdigest()


def compute_mdhash_id(content: str, prefix: str = "") -> str:
    """
    Compute MD5 hash ID for content with optional prefix.

    Used for generating unique identifiers for text chunks and entities.

    Parameters:
    -----------
    content : str
        Content to hash
    prefix : str, optional
        Prefix to prepend to the hash

    Returns:
    --------
    str
        Prefixed hexadecimal MD5 hash string
    """
    return prefix + md5(content.encode()).hexdigest()


__all__ = [
    "compute_args_hash",
    "compute_mdhash_id",
]