(model, tokenizer,
max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95)
| 29 | return content, history |
| 30 | |
| 31 | def chat(model, tokenizer, |
| 32 | max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95): |
| 33 | query = "你好" |
| 34 | history = [] |
| 35 | inputs = tokenizer.build_chat_input(query, history=history, role="user")['input_ids'].to(model.parameters().__next__().device)[0] |
| 36 | seq = torch.cat( |
| 37 | [inputs, torch.tensor([-1]*(max_length-len(inputs)), device=inputs.device)], dim=0 |
| 38 | ) |
| 39 | strategy = BaseStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id, tokenizer.get_command("<|user|>"), tokenizer.get_command("<|observation|>")]) |
| 40 | |
| 41 | output = filling_sequence( |
| 42 | model, seq, |
| 43 | batch_size=1, |
| 44 | strategy=strategy |
| 45 | )[0] # drop memory |
| 46 | output_list = output.tolist()[0][len(inputs):-1] |
| 47 | |
| 48 | response = tokenizer.decode(output_list) |
| 49 | history.append({"role": "user", "content": query}) |
| 50 | response, history = process_response(response, history) |
| 51 | print(response) |
| 52 | print(history) |
| 53 | |
| 54 | if __name__ == "__main__": |
| 55 | import argparse |
no test coverage detected