def chat(
            self,
            query: str,
            history: Union[List, List[Tuple[str, str]]] = None,
            stream: bool = False,
            skip_prompt: bool = True,
            prompt_template_name: str = "",
            max_new_tokens: int = None,
            do_sample: bool = None,
            temperature: float = None,
            repetition_penalty: float = None,
            context_len: int = 2048,
            **kwargs
    ):
        """Chat model with multi turn conversation."""
        prompt_template = get_conv_template(prompt_template_name or self.args.prompt_template_name)

        if history is None:
            history = []
        history.append([query, ''])
        prompt = prompt_template.get_prompt(messages=history)

        input_ids = self.tokenizer(prompt).input_ids
        max_new_tokens = max_new_tokens if max_new_tokens is not None else self.args.max_length
        max_src_len = context_len - max_new_tokens - 8
        input_ids = input_ids[-max_src_len:]
        if stream:
            streamer = TextIteratorStreamer(
                self.tokenizer, timeout=60.0, skip_prompt=skip_prompt, skip_special_tokens=True
            )
            generation_kwargs = dict(
                input_ids=torch.as_tensor([input_ids]).to(self.device),
                max_new_tokens=max_new_tokens,
                do_sample=do_sample if do_sample is not None else self.args.do_sample,
                temperature=temperature if temperature is not None else self.args.temperature,
                repetition_penalty=repetition_penalty if repetition_penalty is not None else self.args.repetition_penalty,
                streamer=streamer,
                **kwargs,
            )
            thread = Thread(target=self.model.generate, kwargs=generation_kwargs)
            thread.start()
            yield from streamer
        else:
            generation_kwargs = dict(
                max_new_tokens=max_new_tokens if max_new_tokens is not None else self.args.max_length,
                do_sample=do_sample if do_sample is not None else self.args.do_sample,
                temperature=temperature if temperature is not None else self.args.temperature,
                repetition_penalty=repetition_penalty if repetition_penalty is not None else self.args.repetition_penalty,
            )
            outputs = self.model.generate(
                input_ids=torch.as_tensor([input_ids]).to(self.device),
                **generation_kwargs,
                **kwargs,
            )
            output_tensor = outputs[0][len(input_ids[0]):] if skip_prompt else outputs[0]
            response = self.tokenizer.decode(output_tensor, skip_special_tokens=True)
            response = response.strip()
            history[-1][1] = response
            return response, history
Chat model with multi turn conversation.
pycorrector/gpt/gpt_model.py