File size: 1,721 Bytes
5a3ba78
 
 
9a46e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4207847
9a46e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4207847
9a46e61
 
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
import os

def patch_langmem_for_python310():
    # Path where langmem is installed inside the Docker container on HF Spaces
    base_path = "/usr/local/lib/python3.10/site-packages/langmem/knowledge"

    extraction_path = os.path.join(base_path, "extraction.py")
    tools_path = os.path.join(base_path, "tools.py")

    # === PATCH extraction.py ===
    if os.path.exists(extraction_path):
        with open(extraction_path, "r", encoding="utf-8") as file:
            content = file.read()

        if "typing.NotRequired" in content:
            content = content.replace("typing.NotRequired", "NotRequired")
            if "from typing_extensions import NotRequired" not in content:
                content = content.replace("import uuid", "import uuid\nfrom typing_extensions import NotRequired")

            with open(extraction_path, "w", encoding="utf-8") as file:
                file.write(content)
            print("✅ Patched extraction.py")
        else:
            print("✅ extraction.py already patched")

    # === PATCH tools.py ===
    if os.path.exists(tools_path):
        with open(tools_path, "r", encoding="utf-8") as file:
            content = file.read()

        if "typing.Literal[*actions_permitted]" in content:
            fixed = '''typing.Optional[
        tuple[typing.Literal["create", "update", "delete"], ...]
    ] = ("create", "update", "delete"),'''
            content = content.replace("typing.Literal[*actions_permitted]", fixed)

            with open(tools_path, "w", encoding="utf-8") as file:
                file.write(content)
            print("✅ Patched tools.py")
        else:
            print("✅ tools.py already patched")

patch_langmem_for_python310()