使用make_genai_metric_from_prompt()自定义LLM指标遇变量缺失错误求助
解决
make_genai_metric_from_prompt()变量缺失报错问题 你遇到的核心问题是没有在自定义指标中显式声明prompt里用到的变量,make_genai_metric_from_prompt()需要通过variables参数明确指定prompt中使用的占位符变量,MLflow才能从DataFrame中匹配对应列的数据。
修复步骤
- 创建指标时添加
variables参数:在调用make_genai_metric_from_prompt()时,新增variables参数,列出你prompt中用到的input和output变量。 - 调整评估配置(可选):如果DataFrame列名和变量名不一致,再通过
evaluator_config的col_mapping做映射;如果一致,无需额外配置。
修改后的完整代码
modified_x_metric = make_genai_metric_from_prompt( name="x_bool", judge_prompt="""...examples could be included below for reference. Make sure to use them as references and to understand them before completing the task. Input: {input} Output: {output} Metric and Grading Directions: ...""", model="openai:/gpt-4o", variables=["input", "output"], # 关键:声明prompt中使用的变量 aggregations=[ "mean", "median", ], greater_is_better=True, ) with mlflow.start_run() as run: results = mlflow.evaluate( data=eval_data, predictions="output", # 对应DataFrame中的output列 extra_metrics=[modified_x_metric], # 如果列名和变量名不同,才需要下面的配置,否则可以注释掉 # evaluator_config={"col_mapping": {"input": "你的输入列名", "output": "你的输出列名"}}, )
原理说明
make_genai_metric()默认会处理常见的input/output变量,但make_genai_metric_from_prompt()是更灵活的自定义方式,需要你明确告诉它prompt里用到了哪些变量,这样MLflow才能在评估时自动从DataFrame中提取对应列的值,填充到prompt的占位符中,避免变量缺失的报错。
内容的提问来源于stack exchange,提问作者user30999949
相关产品推荐
相关产品推荐

