Spaces:
Runtime error
Runtime error
Update patch_langmem.py
Browse files- 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 |
-
|
| 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 |
-
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()
|
|
|
|
|
|