def create_prompt(instruction, answer1, answer2, mode, model_name=None):
"""
根据模型名称生成不同的 prompt 模板。
"""
if not instruction or not answer1 or not answer2:
raise ValueError("Instruction, Answer 1, and Answer 2 cannot be empty.")
if model_name and "[Redacted Person]" in model_name.lower():
return f"""You are a helpful and precise assistant for checking the quality of the answer.
[Question]
{instruction}
[The Start of Assistant 1's Answer]
{answer1}
[The End of Assistant 1's Answer]
[The Start of Assistant 2's Answer]
{answer2}
[The End of Assistant 2's Answer]
[System]
We would like to request your feedback on the performance of two AI assistants in response to the user question displayed above.
Please rate the helpfulness, relevance, accuracy, level of details of their responses. Each assistant receives an overall score on a scale of 1 to 10, where a higher score indicates better overall performance.
Please first output a single line containing only two values indicating the scores for Assistant 1 and 2, respectively. The two scores are separated by a space. In the subsequent line, please provide a comprehensive explanation of your evaluation, avoiding any potential bias and ensuring that the order in which the responses were presented does not affect your judgment.
### Response:"""
else:
# 对话式模板
if mode == "直接评估":
return [
{"role": "system", "content": "You are a helpful and precise assistant for checking the quality of the answer."},
{"role": "user", "content": f"""[Question]\n{instruction}\n[The Start of Assistant 1's Answer]\n{answer1}\n[The End of Assistant 1's Answer]\n\n[The Start of Assistant 2's Answer]\n{answer2}\n[The End of Assistant 2's Answer]\n\nWe would like to request your feedback on the performance of two AI assistants in response to the user question displayed above.\nPlease rate the helpfulness, relevance, accuracy, level of details of their responses. Each assistant receives an overall score on a scale of 1 to 10, where a higher score indicates better overall performance.\nPlease first output a single line containing only two values indicating the scores for Assistant 1 and 2, respectively. The two scores are separated by a space. In the subsequent line, please provide a comprehensive explanation of your evaluation, avoiding any potential bias and ensuring that the order in which the responses were presented does not affect your judgment."""},
]
elif mode == "思维链":
return [
{"role": "system", "content": "You are a helpful and precise assistant for checking the quality of the answer using a chain of thought reasoning approach."},
{"role": "user", "content": f"""[Question]\n{instruction}\n[The Start of Assistant 1's Answer]\n{answer1}\n[The End of Assistant 1's Answer]\n\n[The Start of Assistant 2's Answer]\n{answer2}\n[The End of Assistant 2's Answer]\n\nWe would like to request your feedback on the performance of two AI assistants in response to the user question displayed above.\nPlease rate the helpfulness, relevance, accuracy, level of details of their responses. Each assistant receives an overall score on a scale of 1 to 10, where a higher score indicates better overall performance.\nIn the first line, please provide a comprehensive explanation of your evaluation, avoiding any potential bias and ensuring that the order in which the responses were presented does not affect your judgment.\nIn the subsequent line, please output a single line containing only two values indicating the scores for Assistant 1 and 2, respectively. The two scores are separated by a space. There should be nothing on this line except two scores and a space."""},
]
else:
raise ValueError(f"Unsupported mode: {mode}")
3. 在级联评估中支持推理策略
在 evaluate 函数中为级联评估逻辑添加推理策略支持:
py
def evaluate(instruction, answer1, answer2, mode, state=None, model_name=None, proprietary_model=None, eval_mode="单模型评估"):
# 创建 prompt
if eval_mode == "级联评估":
# 获取模型和 tokenizer
model = state.get("model")
tokenizer = state.get("tokenizer")
if model is None or tokenizer is None:
return "请先加载模型", "", []
# 根据推理策略生成 prompt
conversation = create_prompt(instruction, answer1, answer2, mode, model_name)
# 调用微调裁判模型
with torch.no_grad():
inputs = tokenizer(conversation, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=2048
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
# 提取评分
scores = extract_scores(result, mode)
# 如果置信度低或评分失败,调用专有模型
if len(scores) != 2 or proprietary_model:
conversation = create_prompt(instruction, answer1, answer2, mode, proprietary_model)
result = call_model(conversation, proprietary_model)
scores = extract_scores(result, mode)
return scores