roger33303 commited on
Commit
9a46e61
Β·
verified Β·
1 Parent(s): 9262dd1

Update patch_langmem.py

Browse files
Files changed (1) hide show
  1. patch_langmem.py +40 -45
patch_langmem.py CHANGED
@@ -1,48 +1,43 @@
1
  import os
2
- import re
3
- import sys
4
- from pathlib import Path
5
 
6
  def patch_langmem_for_python310():
7
- if sys.version_info >= (3, 11):
8
- print("βœ… Python >=3.11 detected, no patching needed.")
9
- return
10
-
11
- try:
12
- import langmem
13
- except ImportError:
14
- print("❌ langmem not installed.")
15
- return
16
-
17
- # Get langmem install path
18
- langmem_path = Path(langmem.__file__).parent
19
- extraction_path = langmem_path / "knowledge" / "extraction.py"
20
-
21
- if not extraction_path.exists():
22
- print("❌ Could not locate langmem extraction.py to patch.")
23
- return
24
-
25
- with open(extraction_path, "r", encoding="utf-8") as f:
26
- content = f.read()
27
-
28
- # Check if patch already applied
29
- if "from typing_extensions import NotRequired" in content:
30
- print("βœ… langmem already patched.")
31
- return
32
-
33
- # Apply patch
34
- content = re.sub(
35
- r"import typing(\n|.*)",
36
- r"import typing\nfrom typing_extensions import NotRequired\1",
37
- content,
38
- )
39
- content = content.replace(
40
- "existing: typing.NotRequired",
41
- "existing: NotRequired"
42
- )
43
-
44
- with open(extraction_path, "w", encoding="utf-8") as f:
45
- f.write(content)
46
-
47
- print("βœ… Patched langmem to support Python 3.10.")
48
-
 
1
  import os
 
 
 
2
 
3
  def patch_langmem_for_python310():
4
+ # Path where langmem is installed inside the Docker container on HF Spaces
5
+ base_path = "/usr/local/lib/python3.10/site-packages/langmem/knowledge"
6
+
7
+ extraction_path = os.path.join(base_path, "extraction.py")
8
+ tools_path = os.path.join(base_path, "tools.py")
9
+
10
+ # === PATCH extraction.py ===
11
+ if os.path.exists(extraction_path):
12
+ with open(extraction_path, "r", encoding="utf-8") as file:
13
+ content = file.read()
14
+
15
+ if "typing.NotRequired" in content:
16
+ content = content.replace("typing.NotRequired", "NotRequired")
17
+ if "from typing_extensions import NotRequired" not in content:
18
+ content = content.replace("import uuid", "import uuid\nfrom typing_extensions import NotRequired")
19
+
20
+ with open(extraction_path, "w", encoding="utf-8") as file:
21
+ file.write(content)
22
+ print("βœ… Patched extraction.py")
23
+ else:
24
+ print("βœ… extraction.py already patched or not using NotRequired")
25
+
26
+ # === PATCH tools.py ===
27
+ if os.path.exists(tools_path):
28
+ with open(tools_path, "r", encoding="utf-8") as file:
29
+ content = file.read()
30
+
31
+ if "typing.Literal[*actions_permitted]" in content:
32
+ fixed = '''typing.Optional[
33
+ tuple[typing.Literal["create", "update", "delete"], ...]
34
+ ] = ("create", "update", "delete"),'''
35
+ content = content.replace("typing.Literal[*actions_permitted]", fixed)
36
+
37
+ with open(tools_path, "w", encoding="utf-8") as file:
38
+ file.write(content)
39
+ print("βœ… Patched tools.py")
40
+ else:
41
+ print("βœ… tools.py already patched or does not need fix")
42
+
43
+ patch_langmem_for_python310()