|
| 1 | +from pytgpt.utils import Optimizers |
| 2 | +from pytgpt.utils import Conversation |
| 3 | +from pytgpt.utils import AwesomePrompts |
| 4 | +from pytgpt.base import Provider |
| 5 | +from gpt4all import GPT4All |
| 6 | +from gpt4all.gpt4all import empty_chat_session |
| 7 | +from gpt4all.gpt4all import append_extension_if_missing |
| 8 | + |
| 9 | + |
| 10 | +import logging |
| 11 | + |
| 12 | +my_logger = logging.getLogger("gpt4all") |
| 13 | +my_logger.setLevel(logging.CRITICAL) |
| 14 | + |
| 15 | + |
| 16 | +class GPT4ALL(Provider): |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + model: str, |
| 20 | + is_conversation: bool = True, |
| 21 | + max_tokens: int = 800, |
| 22 | + temperature: float = 0.7, |
| 23 | + presence_penalty: int = 0, |
| 24 | + frequency_penalty: int = 1.18, |
| 25 | + top_p: float = 0.4, |
| 26 | + intro: str = None, |
| 27 | + filepath: str = None, |
| 28 | + update_file: bool = True, |
| 29 | + history_offset: int = 10250, |
| 30 | + act: str = None, |
| 31 | + ): |
| 32 | + """Instantiates GPT4ALL |
| 33 | +
|
| 34 | + Args: |
| 35 | + model (str, optional): Path to LLM model (.gguf or .bin). |
| 36 | + is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True. |
| 37 | + max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 800. |
| 38 | + temperature (float, optional): Charge of the generated text's randomness. Defaults to 0.7. |
| 39 | + presence_penalty (int, optional): Chances of topic being repeated. Defaults to 0. |
| 40 | + frequency_penalty (int, optional): Chances of word being repeated. Defaults to 1.18. |
| 41 | + top_p (float, optional): Sampling threshold during inference time. Defaults to 0.4. |
| 42 | + intro (str, optional): Conversation introductory prompt. Defaults to None. |
| 43 | + filepath (str, optional): Path to file containing conversation history. Defaults to None. |
| 44 | + update_file (bool, optional): Add new prompts and responses to the file. Defaults to True. |
| 45 | + history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250. |
| 46 | + act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None. |
| 47 | + """ |
| 48 | + self.is_conversation = is_conversation |
| 49 | + self.max_tokens_to_sample = max_tokens |
| 50 | + self.model = model |
| 51 | + self.temperature = temperature |
| 52 | + self.presence_penalty = presence_penalty |
| 53 | + self.frequency_penalty = frequency_penalty |
| 54 | + self.top_p = top_p |
| 55 | + self.last_response = {} |
| 56 | + |
| 57 | + self.__available_optimizers = ( |
| 58 | + method |
| 59 | + for method in dir(Optimizers) |
| 60 | + if callable(getattr(Optimizers, method)) and not method.startswith("__") |
| 61 | + ) |
| 62 | + Conversation.intro = ( |
| 63 | + AwesomePrompts().get_act( |
| 64 | + act, raise_not_found=True, default=None, case_insensitive=True |
| 65 | + ) |
| 66 | + if act |
| 67 | + else intro or Conversation.intro |
| 68 | + ) |
| 69 | + self.conversation = Conversation( |
| 70 | + is_conversation, self.max_tokens_to_sample, filepath, update_file |
| 71 | + ) |
| 72 | + self.conversation.history_offset = history_offset |
| 73 | + |
| 74 | + def get_model_name_path(): |
| 75 | + import os |
| 76 | + from pathlib import Path |
| 77 | + |
| 78 | + initial_model_path = Path(append_extension_if_missing(model)) |
| 79 | + if initial_model_path.exists: |
| 80 | + if not initial_model_path.is_absolute(): |
| 81 | + initial_model_path = Path(os.getcwd()) / initial_model_path |
| 82 | + return os.path.split(initial_model_path.as_posix()) |
| 83 | + else: |
| 84 | + raise FileNotFoundError( |
| 85 | + "File does not exist " + initial_model_path.as_posix() |
| 86 | + ) |
| 87 | + |
| 88 | + model_dir, model_name = get_model_name_path() |
| 89 | + |
| 90 | + self.gpt4all = GPT4All( |
| 91 | + model_name=model_name, |
| 92 | + model_path=model_dir, |
| 93 | + allow_download=False, |
| 94 | + verbose=False, |
| 95 | + ) |
| 96 | + |
| 97 | + def ask( |
| 98 | + self, |
| 99 | + prompt: str, |
| 100 | + stream: bool = False, |
| 101 | + raw: bool = False, |
| 102 | + optimizer: str = None, |
| 103 | + conversationally: bool = False, |
| 104 | + ) -> dict: |
| 105 | + """Chat with AI |
| 106 | +
|
| 107 | + Args: |
| 108 | + prompt (str): Prompt to be send. |
| 109 | + stream (bool, optional): Flag for streaming response. Defaults to False. |
| 110 | + raw (bool, optional): Stream back raw response as received. Defaults to False. |
| 111 | + optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None. |
| 112 | + conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False. |
| 113 | + Returns: |
| 114 | + dict : {} |
| 115 | + ```json |
| 116 | + { |
| 117 | + "text" : "How may I help you today?" |
| 118 | + } |
| 119 | + ``` |
| 120 | + """ |
| 121 | + conversation_prompt = self.conversation.gen_complete_prompt(prompt) |
| 122 | + if optimizer: |
| 123 | + if optimizer in self.__available_optimizers: |
| 124 | + conversation_prompt = getattr(Optimizers, optimizer)( |
| 125 | + conversation_prompt if conversationally else prompt |
| 126 | + ) |
| 127 | + else: |
| 128 | + raise Exception( |
| 129 | + f"Optimizer is not one of {self.__available_optimizers}" |
| 130 | + ) |
| 131 | + |
| 132 | + def for_stream(): |
| 133 | + response = self.gpt4all.generate( |
| 134 | + prompt=conversation_prompt, |
| 135 | + max_tokens=self.max_tokens_to_sample, |
| 136 | + temp=self.temperature, |
| 137 | + top_p=self.top_p, |
| 138 | + repeat_penalty=self.frequency_penalty, |
| 139 | + streaming=True, |
| 140 | + ) |
| 141 | + |
| 142 | + message_load: str = "" |
| 143 | + for token in response: |
| 144 | + message_load += token |
| 145 | + resp: dict = dict(text=message_load) |
| 146 | + yield token if raw else resp |
| 147 | + self.last_response.update(resp) |
| 148 | + |
| 149 | + self.conversation.update_chat_history( |
| 150 | + prompt, self.get_message(self.last_response) |
| 151 | + ) |
| 152 | + self.gpt4all.current_chat_session = empty_chat_session() |
| 153 | + |
| 154 | + def for_non_stream(): |
| 155 | + for _ in for_stream(): |
| 156 | + pass |
| 157 | + return self.last_response |
| 158 | + |
| 159 | + return for_stream() if stream else for_non_stream() |
| 160 | + |
| 161 | + def chat( |
| 162 | + self, |
| 163 | + prompt: str, |
| 164 | + stream: bool = False, |
| 165 | + optimizer: str = None, |
| 166 | + conversationally: bool = False, |
| 167 | + ) -> str: |
| 168 | + """Generate response `str` |
| 169 | + Args: |
| 170 | + prompt (str): Prompt to be send. |
| 171 | + stream (bool, optional): Flag for streaming response. Defaults to False. |
| 172 | + optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None. |
| 173 | + conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False. |
| 174 | + Returns: |
| 175 | + str: Response generated |
| 176 | + """ |
| 177 | + |
| 178 | + def for_stream(): |
| 179 | + for response in self.ask( |
| 180 | + prompt, True, optimizer=optimizer, conversationally=conversationally |
| 181 | + ): |
| 182 | + yield self.get_message(response) |
| 183 | + |
| 184 | + def for_non_stream(): |
| 185 | + return self.get_message( |
| 186 | + self.ask( |
| 187 | + prompt, |
| 188 | + False, |
| 189 | + optimizer=optimizer, |
| 190 | + conversationally=conversationally, |
| 191 | + ) |
| 192 | + ) |
| 193 | + |
| 194 | + return for_stream() if stream else for_non_stream() |
| 195 | + |
| 196 | + def get_message(self, response: dict) -> str: |
| 197 | + """Retrieves message only from response |
| 198 | +
|
| 199 | + Args: |
| 200 | + response (str): Response generated by `self.ask` |
| 201 | +
|
| 202 | + Returns: |
| 203 | + str: Message extracted |
| 204 | + """ |
| 205 | + assert isinstance(response, dict), "Response should be of dict data-type only" |
| 206 | + return response["text"] |
0 commit comments