(self, tokenizer, query: str, history: List[Tuple[str, str]] = None, max_length: int = 2048, num_beams=1,
do_sample=True, top_p=0.7, temperature=0.95, logits_processor=None, **kwargs)
| 164 | |
| 165 | @torch.no_grad() |
| 166 | def chat(self, tokenizer, query: str, history: List[Tuple[str, str]] = None, max_length: int = 2048, num_beams=1, |
| 167 | do_sample=True, top_p=0.7, temperature=0.95, logits_processor=None, **kwargs): |
| 168 | if history is None: |
| 169 | history = [] |
| 170 | if logits_processor is None: |
| 171 | logits_processor = LogitsProcessorList() |
| 172 | logits_processor.append(InvalidScoreLogitsProcessor()) |
| 173 | gen_kwargs = {"max_length": max_length, "num_beams": num_beams, "do_sample": do_sample, "top_p": top_p, |
| 174 | "temperature": temperature, "logits_processor": logits_processor, **kwargs} |
| 175 | if not history: |
| 176 | prompt = query |
| 177 | else: |
| 178 | prompt = "" |
| 179 | for i, (old_query, response) in enumerate(history): |
| 180 | prompt += "[Round {}]\n问:{}\n答:{}\n".format(i, old_query, response) |
| 181 | prompt += "[Round {}]\n问:{}\n答:".format(len(history), query) |
| 182 | inputs = tokenizer([prompt], return_tensors="pt") |
| 183 | inputs = inputs.to(self.device) |
| 184 | outputs = self.generate(**inputs, **gen_kwargs) |
| 185 | outputs = outputs.tolist()[0][len(inputs["input_ids"][0]):] |
| 186 | response = tokenizer.decode(outputs) |
| 187 | response = self.process_response(response) |
| 188 | history = history + [(query, response)] |
| 189 | return response, history |
| 190 | |
| 191 | @torch.no_grad() |
| 192 | def batch_generate(self, tokenizer, queries, max_length: int = 2048, num_beams=1, |
no test coverage detected