Repository: nomic-ai/gpt4all
File: gpt4all-bindings/python/gpt4all/gpt4all.py (Python)

Code before:

        Returns:
            Either the entire completion or a generator that yields the completion token by token.
        """
        generate_kwargs = dict(
            temp=temp,
            top_k=top_k,
            top_p=top_p,
            repeat_penalty=repeat_penalty,
            repeat_last_n=repeat_last_n,
            n_batch=n_batch,
            n_predict=n_predict if n_predict is not None else max_tokens,
            verbose=verbose,
        )

        if self._is_chat_session_activated:
            self.current_chat_session.append({"role": "user", "content": prompt})
            prompt = self._format_chat_prompt_template(
                         messages = self.current_chat_session,
                         default_prompt_header = self.chat_session_header
                     )
            generate_kwargs['reset_context'] = len(self.current_chat_session) == 1
        else:
            generate_kwargs['reset_context'] = True
        
        output_collector = [{"content": ""}]

        if self._is_chat_session_activated:
            self.current_chat_session.append({"role": "assistant", "content": ""})
            output_collector = self.current_chat_session

        def _callback_wrapper(callback, output_collector):
            def _callback(token_id, response):
                nonlocal callback, output_collector

                output_collector[-1]["content"] += response
                
                return callback(token_id, response)

            return _callback
        
        if streaming:
            return self.model.prompt_model_streaming(prompt = prompt, callback = _callback_wrapper(callback, output_collector), **generate_kwargs)

        self.model.prompt_model(prompt = prompt, callback = _callback_wrapper(callback, output_collector), **generate_kwargs)

        return output_collector[-1]["content"]

    @contextmanager
    def chat_session(self, header: str = ""):
        '''

Review comment (suggestion):
Could add:
```py
output_collector: List[Dict[str, str]] = ...
```

Code after:

        response = requests.get(download_url, stream=True)
        total_size_in_bytes = int(response.headers.get("content-length", 0))
        block_size = 2**20  # 1 MB

        with tqdm(total=total_size_in_bytes, unit="iB", unit_scale=True) as progress_bar:
            try:
                with open(download_path, "wb") as file:
                    for data in response.iter_content(block_size):
                        progress_bar.update(len(data))
                        file.write(data)
            except Exception:
                if os.path.exists(download_path):
                    if verbose:
                        print("Cleaning up the interrupted download...")
                    os.remove(download_path)
                raise

        # Validate download was successful
        if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
            raise RuntimeError("An error occurred during download. Downloaded file may not work.")

        # Sleep for a little bit so OS can remove file lock
        time.sleep(2)

        if verbose:
            print("Model downloaded at: ", download_path)
        return download_path

    def generate(
        self,
        prompt: str,
        max_tokens: int = 200,
        temp: float = 0.7,
        top_k: int = 40,
        top_p: float = 0.4,
        repeat_penalty: float = 1.18,
        repeat_last_n: int = 64,
        n_batch: int = 8,
        n_predict: Optional[int] = None,
        streaming: bool = False,
        callback: pyllmodel.ResponseCallbackType = pyllmodel.empty_response_callback,
    ) -> Union[str, Iterable[str]]:
        """
        Generate outputs from any GPT4All model.

        Args:
            prompt: The prompt for the model the complete.
            max_tokens: The maximum number of tokens to generate.
            temp: The model temperature. Larger values increase creativity but decrease factuality.
            top_k: Randomly sample from the top_k most likely tokens at each generation step. Set this to 1 for greedy decoding.