2023-06-26 11:34:03 +03:00
|
|
|
# Importing various modules and classes from a custom library 'langchain' likely used for natural language processing
|
|
|
|
from langchain.llms import GPT4All
|
|
|
|
from langchain.llms.base import LLM
|
|
|
|
from llm.brainpicking import BrainPicking
|
|
|
|
from logger import get_logger
|
2023-06-30 11:10:59 +03:00
|
|
|
from models.settings import LLMSettings
|
2023-06-26 11:34:03 +03:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PrivateBrainPicking(BrainPicking):
|
|
|
|
"""
|
|
|
|
This subclass of BrainPicking is used to specifically work with a private language model.
|
|
|
|
"""
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
# Initialize class settings
|
|
|
|
llm_settings = LLMSettings()
|
|
|
|
|
2023-06-26 11:34:03 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
model: str,
|
|
|
|
chat_id: str,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id:str,
|
2023-06-26 11:34:03 +03:00
|
|
|
temperature: float,
|
|
|
|
max_tokens: int,
|
|
|
|
user_openai_api_key: str,
|
|
|
|
) -> "PrivateBrainPicking":
|
|
|
|
"""
|
|
|
|
Initialize the PrivateBrainPicking class by calling the parent class's initializer.
|
|
|
|
:param model: Language model name to be used.
|
2023-06-28 20:39:27 +03:00
|
|
|
:param brain_id: The user id to be used for CustomSupabaseVectorStore.
|
2023-06-26 11:34:03 +03:00
|
|
|
:return: PrivateBrainPicking instance
|
|
|
|
"""
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-06-26 11:34:03 +03:00
|
|
|
super().__init__(
|
|
|
|
model=model,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id=brain_id,
|
2023-06-26 11:34:03 +03:00
|
|
|
chat_id=chat_id,
|
|
|
|
max_tokens=max_tokens,
|
|
|
|
temperature=temperature,
|
|
|
|
user_openai_api_key=user_openai_api_key,
|
|
|
|
)
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
def _create_llm(self, model_name, streaming=False, callbacks=None) -> LLM:
|
2023-06-26 11:34:03 +03:00
|
|
|
"""
|
2023-06-30 11:10:59 +03:00
|
|
|
Override the _create_llm method to enforce the use of a private model.
|
2023-06-26 11:34:03 +03:00
|
|
|
:param model_name: Language model name to be used.
|
|
|
|
:param private_model_args: Dictionary containing model_path, n_ctx and n_batch.
|
|
|
|
:param private: Boolean value to determine if private model is to be used. Defaulted to True.
|
|
|
|
:return: Language model instance
|
|
|
|
"""
|
2023-06-30 11:10:59 +03:00
|
|
|
model_path = self.llm_settings.model_path
|
|
|
|
model_n_ctx = self.llm_settings.model_n_ctx
|
|
|
|
model_n_batch = self.llm_settings.model_n_batch
|
2023-06-26 11:34:03 +03:00
|
|
|
|
|
|
|
logger.info("Using private model: %s", model_path)
|
|
|
|
|
|
|
|
return GPT4All(
|
|
|
|
model=model_path,
|
|
|
|
n_ctx=model_n_ctx,
|
|
|
|
n_batch=model_n_batch,
|
|
|
|
backend="gptj",
|
|
|
|
verbose=True,
|
|
|
|
)
|