(
self,
prompt_cot,
sys_prompt="请根据用户的输入,生成思考过程的思维链并改写提示词:",
temperature=0.0,
top_p=1.0,
max_new_tokens=2048,
device="cuda:0",
)
| 49 | |
| 50 | @torch.inference_mode() |
| 51 | def predict( |
| 52 | self, |
| 53 | prompt_cot, |
| 54 | sys_prompt="请根据用户的输入,生成思考过程的思维链并改写提示词:", |
| 55 | temperature=0.0, |
| 56 | top_p=1.0, |
| 57 | max_new_tokens=2048, |
| 58 | device="cuda:0", |
| 59 | ): |
| 60 | org_prompt_cot = prompt_cot |
| 61 | try: |
| 62 | user_prompt_format = sys_prompt + "\n" + org_prompt_cot |
| 63 | messages = [ |
| 64 | { |
| 65 | "role": "user", |
| 66 | "content": [ |
| 67 | {"type": "text", "text": user_prompt_format}, |
| 68 | ], |
| 69 | } |
| 70 | ] |
| 71 | |
| 72 | text = self.processor.apply_chat_template( |
| 73 | messages, tokenize=False, add_generation_prompt=True |
| 74 | ) |
| 75 | image_inputs, video_inputs = process_vision_info(messages) |
| 76 | inputs = self.processor( |
| 77 | text=[text], |
| 78 | images=image_inputs, |
| 79 | videos=video_inputs, |
| 80 | padding=True, |
| 81 | return_tensors="pt", |
| 82 | ) |
| 83 | inputs = inputs.to(device) |
| 84 | |
| 85 | # 注意:原始代码固定 do_sample=False,top_k=5, top_p=0.9,这里保持一致 |
| 86 | generated_ids = self.model.generate( |
| 87 | **inputs, |
| 88 | max_new_tokens=2048, # 与原始代码保持一致(未使用 max_new_tokens 参数) |
| 89 | temperature=float(temperature), |
| 90 | do_sample=False, |
| 91 | top_k=5, |
| 92 | top_p=0.9 |
| 93 | ) |
| 94 | generated_ids_trimmed = [ |
| 95 | out_ids[len(in_ids):] |
| 96 | for in_ids, out_ids in zip(inputs.input_ids, generated_ids) |
| 97 | ] |
| 98 | output_text = self.processor.batch_decode( |
| 99 | generated_ids_trimmed, |
| 100 | skip_special_tokens=True, |
| 101 | clean_up_tokenization_spaces=False, |
| 102 | ) |
| 103 | output_res = output_text[0] |
| 104 | assert output_res.count("think>") == 2 |
| 105 | prompt_cot = output_res.split("think>")[-1] |
| 106 | if prompt_cot.startswith("\n"): |
| 107 | prompt_cot = prompt_cot[1:] |
| 108 | prompt_cot = replace_single_quotes(prompt_cot) |
nothing calls this directly
no test coverage detected