python: replace deprecated pkg_resources with importlib (#1505)

This commit is contained in:
cebtenzzre 2023-10-12 13:35:27 -04:00 committed by GitHub
parent 3c45a555e9
commit 4d4275d1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View File

@ -4,13 +4,13 @@ The GPT4All CLI is a self-contained script based on the `gpt4all` and `typer` pa
REPL to communicate with a language model similar to the chat GUI application, but more basic. REPL to communicate with a language model similar to the chat GUI application, but more basic.
""" """
import importlib.metadata
import io import io
import pkg_resources # should be present as a dependency of gpt4all
import sys import sys
import typer
from collections import namedtuple from collections import namedtuple
from typing_extensions import Annotated from typing_extensions import Annotated
import typer
from gpt4all import GPT4All from gpt4all import GPT4All
@ -79,7 +79,7 @@ def repl(
use_new_loop = False use_new_loop = False
try: try:
version = pkg_resources.Environment()['gpt4all'][0].version version = importlib.metadata.version('gpt4all')
version_major = int(version.split('.')[0]) version_major = int(version.split('.')[0])
if version_major >= 1: if version_major >= 1:
use_new_loop = True use_new_loop = True

View File

@ -1,23 +1,27 @@
import atexit
import ctypes import ctypes
import importlib.resources
import logging import logging
import os import os
import platform import platform
from queue import Queue
import re import re
import subprocess import subprocess
import sys import sys
import threading import threading
from contextlib import ExitStack
from queue import Queue
from typing import Callable, Iterable, List from typing import Callable, Iterable, List
import pkg_resources
logger: logging.Logger = logging.getLogger(__name__) logger: logging.Logger = logging.getLogger(__name__)
# TODO: provide a config file to make this more robust file_manager = ExitStack()
LLMODEL_PATH = os.path.join("llmodel_DO_NOT_MODIFY", "build").replace("\\", "\\\\") atexit.register(file_manager.close) # clean up files on exit
MODEL_LIB_PATH = str(pkg_resources.resource_filename("gpt4all", LLMODEL_PATH)).replace("\\", "\\\\")
# TODO: provide a config file to make this more robust
MODEL_LIB_PATH = file_manager.enter_context(importlib.resources.as_file(
importlib.resources.files("gpt4all") / "llmodel_DO_NOT_MODIFY" / "build",
))
def load_llmodel_library(): def load_llmodel_library():
system = platform.system() system = platform.system()
@ -36,9 +40,7 @@ def load_llmodel_library():
llmodel_file = "libllmodel" + "." + c_lib_ext llmodel_file = "libllmodel" + "." + c_lib_ext
llmodel_dir = str(pkg_resources.resource_filename("gpt4all", os.path.join(LLMODEL_PATH, llmodel_file))).replace( llmodel_dir = str(MODEL_LIB_PATH / llmodel_file).replace("\\", r"\\")
"\\", "\\\\"
)
llmodel_lib = ctypes.CDLL(llmodel_dir) llmodel_lib = ctypes.CDLL(llmodel_dir)
@ -131,7 +133,7 @@ llmodel.llmodel_set_implementation_search_path.restype = None
llmodel.llmodel_threadCount.argtypes = [ctypes.c_void_p] llmodel.llmodel_threadCount.argtypes = [ctypes.c_void_p]
llmodel.llmodel_threadCount.restype = ctypes.c_int32 llmodel.llmodel_threadCount.restype = ctypes.c_int32
llmodel.llmodel_set_implementation_search_path(MODEL_LIB_PATH.encode("utf-8")) llmodel.llmodel_set_implementation_search_path(str(MODEL_LIB_PATH).replace("\\", r"\\").encode("utf-8"))
llmodel.llmodel_available_gpu_devices.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.POINTER(ctypes.c_int32)] llmodel.llmodel_available_gpu_devices.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.POINTER(ctypes.c_int32)]
llmodel.llmodel_available_gpu_devices.restype = ctypes.POINTER(LLModelGPUDevice) llmodel.llmodel_available_gpu_devices.restype = ctypes.POINTER(LLModelGPUDevice)