dt- eval- lib (英语)
用于运行LLM-as-a-judge评价的最小类型Script库.
安装
npm install @dynatrace-oss/dt-eval-lib
然后安装您的提供者的对等依赖 :
| 提供者 | 同伴依赖关系 |
|---|---|
openai, (中文(简体) ), azure-openai | npm install openai |
anthropic | npm install @anthropic-ai/sdk |
vertex, (中文(简体) ), gemini | npm install @google/genai |
bedrock | npm install @aws-sdk/client-bedrock-runtime |
构建
npm run build
测试
npm test
快速使用
import { evaluate, BuiltInMetric } from "@dynatrace-oss/dt-eval-lib";
const result = await evaluate(
BuiltInMetric.Toxicity,
{
input: "Tell me a joke",
output: "Why did the chicken cross the road? To get to the other side!",
},
{
provider: {
provider: "openai",
apiKey: "sk-...",
},
},
);
console.log(result.score); // { value: 1, label: "pass" }
console.log(result.explanation); // { summary: "...", reasoning: "..." }
可用计量
所有内置参数返回 a 连续 计分 [0.0, 1.0]标签 "pass" 何时 value >= threshold 财务报告和审定财务报表 "fail" 否则。 每个内置的公尺默认值 threshold: 0.5 -通过呼叫控制它 EvalConfig.scoring.thresholdOverride真相来源: src/prompts/catalog-data.ts。 。 。 .
| 度量衡 | 数字 | 分数范围 | 使用的字段来自 EvalInput |
|---|---|---|---|
answer-completeness | BuiltInMetric.AnswerCompleteness | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
bias | BuiltInMetric.Bias | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
conciseness | BuiltInMetric.Conciseness | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
context-relevance | BuiltInMetric.ContextRelevance | 0.0–1.0 (中文(简体) ) | input |
factual-accuracy | BuiltInMetric.FactualAccuracy | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
faithfulness | BuiltInMetric.Faithfulness | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
fluency | BuiltInMetric.Fluency | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
hallucination | BuiltInMetric.Hallucination | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
output-prompt-injection | BuiltInMetric.OutputPromptInjection | 0.0–1.0 (中文(简体) ) | output |
pii-leakage | BuiltInMetric.PiiLeakage | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
prompt-injection | BuiltInMetric.PromptInjection | 0.0–1.0 (中文(简体) ) | input |
relevance | BuiltInMetric.Relevance | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
summarization-quality | BuiltInMetric.SummarizationQuality | 0.0–1.0 (中文(简体) ) | input, (中文(简体) ), output |
toxicity | BuiltInMetric.Toxicity | 0.0–1.0 (中文(简体) ) | output |
user-frustration | BuiltInMetric.UserFrustration | 0.0–1.0 (中文(简体) ) | input |
说明: “ 所用的字段” 一栏列出哪些字段是鼠标提示实际读取的 .
EvalInput.output在所有调用时需要类型-通过output: ""仅用于计量的input。 。 。 .
供应商
支助 开放AI, (中文(简体) ), 安东尼, (中文(简体) ), Azure OpenAI 系统, (中文(简体) ), 谷歌双子企业平台(Vertex AI),以及 亚马逊贝德洛克通过代码或环境变量中的明确选项配置.
环境变量
# OpenAI
OPENAI_API_KEY=sk-...
OPENAI_BASE_URL=https://your-proxy.example.com/v1 # optional
# Anthropic
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_BASE_URL=https://your-proxy.example.com # optional
# Azure OpenAI (all three required)
AZURE_OPENAI_API_KEY=...
AZURE_OPENAI_ENDPOINT=https://<resource>.openai.azure.com
AZURE_OPENAI_API_VERSION=2024-02-01
# Google Gemini Enterprise Platform (Vertex AI) — ADC path (no API key needed)
GOOGLE_CLOUD_PROJECT=my-gcp-project
GOOGLE_CLOUD_LOCATION=us-central1 # optional, defaults to "global"
# Google Gemini Enterprise Platform (Vertex AI) — API key path
GOOGLE_API_KEY=AIza...
# Amazon Bedrock — static credentials (optional if using IAM roles / SSO)
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...
AWS_DEFAULT_REGION=us-east-1 # optional, defaults to "us-east-1"
每个选项的配置按此顺序解决:
- 明确价值
provider选项 - 环境变量
谷歌双子企业平台(Vertex AI) 设置
使用带有提供者ID的应用程序默认证书( ADC) vertex 当运行在 GKE 或带有工作负荷身份的 Cloud 运行时, 或者通过 apiKey 带有提供者 ID gemini 用于基于密钥的认证.
Azure OpenAI 设置
所有四个领域都需要—— model 是您的 Azure 部署名称, 它由用户定义, 没有默认 .
await evaluate(BuiltInMetric.Toxicity, input, {
provider: {
provider: "azure-openai",
apiKey: "...", // or AZURE_OPENAI_API_KEY
baseUrl: "https://<resource>.openai.azure.com", // or AZURE_OPENAI_ENDPOINT
apiVersion: "2024-02-01", // or AZURE_OPENAI_API_VERSION
model: "my-gpt4-deployment", // your deployment name
},
});
亚马逊 Bedrock 设置
使用AWS SDK证书链——IAM角色,SSO,以及 AWS_PROFILE 全部自动解决。 只有不能使用基于角色的认证时,才需要静态证书.
说明: 谷歌提供者标识
vertex财务报告和审定财务报表gemini两者都使用@google/genaiSDK; (中文(简体) );azure-openai财务报告和审定财务报表openai两者都使用openaiSDK(英语:SDK).
计量识别
计量标准由 BuiltInMetric um. 也可以通过习惯 PromptDefinition 直接对象 :
import { evaluate, BuiltInMetric } from "@dynatrace-oss/dt-eval-lib";
await evaluate(BuiltInMetric.Toxicity, input, config); // built-in metric via enum
await evaluate(myCustomPrompt, input, config); // custom PromptDefinition object
使用 listPrompts() 财务报告和审定财务报表 getPrompt() 用于发现现有衡量标准:
import { listPrompts, getPrompt, BuiltInMetric } from "@dynatrace-oss/dt-eval-lib";
const all = listPrompts(); // all 15 built-in metrics
const tox = getPrompt(BuiltInMetric.Toxicity); // single metric by ID
配置
import type { EvalConfig, EvalInput } from "@dynatrace-oss/dt-eval-lib";
// What you pass to evaluate()
const input: EvalInput = {
input: "What is the capital of France?", // required — the user query
output: "The capital of France is Paris.", // required — the LLM response to evaluate
context: "...", // optional — retrieved documents (RAG)
expectedOutput: "...", // optional — reference answer
};
// How to run the evaluation
const config: EvalConfig = {
provider: {
provider: "openai", // "openai" | "anthropic" | "azure-openai" | "vertex" | "gemini" | "bedrock"
apiKey: "sk-...", // optional if env var is set
baseUrl: "https://", // optional — openai and anthropic only
model: "...", // optional — see src/engine/providers/index.ts for per-provider defaults
timeout: 30000, // optional — request timeout in ms (default: 30000)
maxRetries: 2, // optional — retries on transient errors (default: 2)
},
scoring: {
thresholdOverride: 0.8, // optional — override the metric's pass threshold (default: 0.5)
},
};