用代码评估器为AI应用补充确定性信号
介绍 Phoenix Evals 中基于 Python 或 TypeScript 编写的程序化代码评估器,说明如何用 create_evaluator 将普通函数转为评估器,并返回布尔值或 Score 对象以提供确定性信号。
评估并不都要求LLMS,通常应该创建评价器,对数据集进行基本检查或计算,与LLM评价配合,可以帮助提供有用的信号来改进应用程序。 这些不使用LLM的评价用一个工具来表示。 `kind="code"` 记分牌上的旗帜。 此页面覆盖 **方案代码评价员** ——您在 Python 或 TypeScript 中写入并运行的函数 `arize-phoenix-evals` SDK. 如果你想在凤凰城自动运行的 UI 中进行确定性检查,不需要任何代码,请参见 [服务器 Evals](<https://arize.com/docs/phoenix/evaluation/server-evals/overview>). 对于凤凰城UI代码评价员所接受的返回形状的完整目录(分类、连续、多输出的路由和 `explanation` 字段),参见 [代码评价器输出形状](<https://arize.com/docs/phoenix/evaluation/how-to-evals/code-evaluator-output-shapes>). ### 使用( E) `create_evaluator` 为方便起见,一个简单的(同步或同步)函数可以使用下列函数转换成评价器: `create_evaluator` 装饰器。此函数可以直接返回 a `Score` 对象或可以转换成分数的值。在下面的例子中,我们装饰的评价函数和皮层返回布尔值,当它被用作评价器时,它被转换成一个 `Score` 分数为: `1` 或 时 间 `0` 和相应的标签: `True` 或 时 间 `False`. - Python 键 - TypeScript ``` from phoenix.evals import create_evaluator @create_evaluator( name="exact-match", kind="code", direction="maximize" ) def exact_match(input: str, output: str) -> bool: return input == output exact_match.evaluate({"input": "hello world", "output": "hello world"}) # [ # Score( # name='exact-match', # score=1, # label=None, # explanation=None, # metadata={}, # kind='code', # direction='maximize' # ) # ] @create_evaluator( name="contains-link", kind="code", direction="maximize" ) async def contains_link(output: str) -> Score: link = "https://arize-phoenix.readthedocs.io/projects/evals/" return link in output ``` ``` import { createEvaluator } from "@arizeai/phoenix-evals"; const exactMatch = createEvaluator<{ input: string; output: string }>( ({ input, output }) => { return input === output ? 1 : 0; }, { name: "exact-match", kind: "CODE", optimizationDirection: "MAXIMIZE", } ); const result = await exactMatch.evaluate({ input: "hello world", output: "hello world", }); // result: { score: 1 } const containsLink = createEvaluator<{ output: string }>( async ({ output }) => { const link = "https://arize-phoenix.readthedocs.io/projects/evals/"; return output.includes(link) ? 1 : 0; }, { name: "contains-link", kind: "CODE", optimizationDirection: "MAXIMIZE", } ); ``` 通知原功能仍可用于测试目的: - Python 键 - TypeScript ``` exact_match("hello", "world") # False await contains_link( "read the documentation here: " "https://arize-phoenix.readthedocs.io/projects/evals/" ) # True ``` ``` // The underlying function logic can be extracted for testing const exactMatchFn = (input: string, output: string) => input === output; exactMatchFn("hello", "world"); // false const containsLinkFn = async (output: string) => { const link = "https://arize-phoenix.readthedocs.io/projects/evals/"; return output.includes(link); }; await containsLinkFn( "read the documentation here: " + "https://arize-phoenix.readthedocs.io/projects/evals/" ); // true ``` #### 返回时 `Score` 直接对象 - Python 键 - TypeScript ``` from phoenix.evals import create_evaluator, Score from textdistance import levenshtein @create_evaluator( name="levenshtein-distance", kind="code", direction="minimize" ) def levenshtein(output: str, expected: str) -> Score: distance = levenshtein(output, expected) return Score( name="levenshtein-distance", score=distance, explanation="Levenshtein distance between {output} and {expected}", kind="code", direction="minimize", ) ``` ``` import { createEvaluator } from "@arizeai/phoenix-evals"; // Simple Levenshtein distance implementation function levenshteinDistance(a: string, b: string): number { const matrix: number[][] = []; for (let i = 0; i <= b.length; i++) { matrix[i] = [i]; } for (let j = 0; j <= a.length; j++) { matrix[0][j] = j; } for (let i = 1; i <= b.length; i++) { for (let j = 1; j <= a.length; j++) { if (b.charAt(i - 1) === a.charAt(j - 1)) { matrix[i][j] = matrix[i - 1][j - 1]; } else { matrix[i][j] = Math.min( matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1 ); } } } return matrix[b.length][a.length]; } const levenshtein = createEvaluator<{ output: string; expected: string }>( ({ output, expected }) => { const distance = levenshteinDistance(output, expected); return { score: distance, explanation: `Levenshtein distance between ${output} and ${expected}`, }; }, { name: "levenshtein-distance", kind: "CODE", optimizationDirection: "MINIMIZE", } ); ``` #### 其他人员 `Score` 改划 那个... `create_evaluator` / `createEvaluator` 函数将自动将许多不同的函数输出转换成分数: - Python 键 - TypeScript - 计分对象( 不需要转换) - 数字( 转换为分数. score) - 一个布尔( 转换为整数分数. scount and string counter. label) - 短字符串( QQ 3 单词, 转换为 Score. label) - 长字符串( QQ 4 单词, 转换成 Score. explaination) - 带有“分数”、“标签”、“解释”或“元数据”的词典 - 值的翻转( 仅允许布尔、 数字、 字符串类型) - EvaluationResult 对象( 不需要转换) - 数字(转换为 `{ score: number }`) - 字符串( 转换为 `{ label: string }`) - 对象为 `score`, `label`和(或) `explanation` 属性 ### 已建分类计量器 对于分类和标签工作,凤凰为两种语言的内置代码评价员提供精度,召回和F分数,支持二进制和多类分类. `macro`/`micro`/`weighted` 平均,见 [精度 / 召回 / F- 分数](<https://arize.com/docs/phoenix/evaluation/pre-built-metrics/precision-recall-fscore>) 用于使用,基础公式,以及何时选择每种平均策略. 这页有用吗? [建议编辑](<https://github.com/arize-ai/phoenix/edit/main/docs/phoenix/evaluation/how-to-evals/code-evaluators.mdx>)提出问题
查看原始来源