Skip to content

Commit

Permalink
python: replace deprecated pkg_resources with importlib (nomic-ai#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
cebtenzzre authored Oct 12, 2023
1 parent 3c45a55 commit 4d4275d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions gpt4all-bindings/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
REPL to communicate with a language model similar to the chat GUI application, but more basic.
"""

import importlib.metadata
import io
import pkg_resources # should be present as a dependency of gpt4all
import sys
import typer

from collections import namedtuple
from typing_extensions import Annotated

import typer
from gpt4all import GPT4All


Expand Down Expand Up @@ -79,7 +79,7 @@ def repl(

use_new_loop = False
try:
version = pkg_resources.Environment()['gpt4all'][0].version
version = importlib.metadata.version('gpt4all')
version_major = int(version.split('.')[0])
if version_major >= 1:
use_new_loop = True
Expand Down
22 changes: 12 additions & 10 deletions gpt4all-bindings/python/gpt4all/pyllmodel.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import atexit
import ctypes
import importlib.resources
import logging
import os
import platform
from queue import Queue
import re
import subprocess
import sys
import threading
from contextlib import ExitStack
from queue import Queue
from typing import Callable, Iterable, List

import pkg_resources

logger: logging.Logger = logging.getLogger(__name__)


# TODO: provide a config file to make this more robust
LLMODEL_PATH = os.path.join("llmodel_DO_NOT_MODIFY", "build").replace("\\", "\\\\")
MODEL_LIB_PATH = str(pkg_resources.resource_filename("gpt4all", LLMODEL_PATH)).replace("\\", "\\\\")
file_manager = ExitStack()
atexit.register(file_manager.close) # clean up files on exit

# 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():
system = platform.system()
Expand All @@ -36,9 +40,7 @@ def get_c_shared_lib_extension():

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)

Expand Down Expand Up @@ -131,7 +133,7 @@ class LLModelGPUDevice(ctypes.Structure):
llmodel.llmodel_threadCount.argtypes = [ctypes.c_void_p]
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.restype = ctypes.POINTER(LLModelGPUDevice)
Expand Down

0 comments on commit 4d4275d

Please sign in to comment.