FastGPT/packages/service/core/evaluation/scoring.ts
chanzhi82020 31c17999b8
This PR introduces evaluation support designed specifically to track and benchmark applications built on the FastGPT platform. (#5476)
- Adds a lightweight evaluation framework for app-level tracking and benchmarking.
- Changes: 28 files, +1455 additions, -66 deletions.
- Branch: add-evaluations -> main.
- PR: https://github.com/chanzhi82020/FastGPT/pull/1

Applications built on FastGPT need repeatable, comparable benchmarks to measure regressions, track improvements, and validate releases. This initial implementation provides the primitives to define evaluation scenarios, run them against app endpoints or model components, and persist results for later analysis.

I updated the PR description to emphasize that the evaluation system is targeted at FastGPT-built apps and expanded the explanation of the core pieces so reviewers understand the scope and intended use. The new description outlines the feature intent, core components, and how results are captured and aggregated for benchmarking.

- Evaluation definitions
  - Define evaluation tasks that reference an app (app id, version, endpoint), test datasets or input cases, expected outputs (when applicable), and run configuration (parallelism, timeouts).
  - Support for custom metric plugins so teams can add domain-specific measures.

- Runner / Executor
  - Executes evaluation cases against app endpoints or internal model interfaces.
  - Captures raw responses, response times, status codes, and any runtime errors.
  - Computes per-case metrics (e.g., correctness, latency) immediately after each case run.

- Metrics & Aggregation
  - Built-in metrics: accuracy/success rate, latency (p50/p90/p99), throughput, error rate.
  - Aggregation produces per-run summaries and per-app historical summaries for trend analysis.
  - Allows combining metrics into composite scores for high-level benchmarking.

- Persistence & Logging
  - Stores run results, input/output pairs (when needed), timestamps, environment info, and app/version metadata so runs are reproducible and auditable.
  - Logs are retained to facilitate debugging and root-cause analysis of regressions.

- Reporting & Comparison
  - Produces aggregated reports suitable for CI gating, release notes, or dashboards.
  - Supports comparing multiple app versions or deployments side-by-side.

- Extensibility & Integration
  - Designed to plug into CI (automated runs on PRs or releases), dashboards, and downstream analysis tools.
  - Easy to add new metrics, evaluators, or dataset connectors.

By centering the evaluation system on FastGPT apps, teams can benchmark full application behavior (not only raw model outputs), correlate metrics with deployment configurations, and make informed release decisions.

- Expand built-in metric suite (e.g., F1, BLEU/ROUGE where applicable), add dataset connectors, and provide example evaluation scenarios for sample apps.
- Integrate with CI pipelines and add basic dashboarding for trend visualization.

Related Issue: N/A

Co-authored-by: Archer <545436317@qq.com>
2025-09-16 15:20:59 +08:00

130 lines
4.1 KiB
TypeScript

import type { ChatCompletionMessageParam } from '@fastgpt/global/core/ai/type';
import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants';
import { getLLMModel } from '../../core/ai/model';
import { createChatCompletion } from '../../core/ai/config';
import { formatLLMResponse, llmCompletionsBodyFormat } from '../../core/ai/utils';
import { loadRequestMessages } from '../../core/chat/utils';
import { countGptMessagesTokens, countPromptTokens } from '../../common/string/tiktoken';
const template_accuracy1 = `
Instruction: You are a world class state of the art assistant for rating a User Answer given a Question. The Question is completely answered by the Reference Answer.
Say 4, if User Answer is full contained and equivalent to Reference Answer in all terms, topics, numbers, metrics, dates and units.
Say 2, if User Answer is partially contained and almost equivalent to Reference Answer in all terms, topics, numbers, metrics, dates and units.
Say 0, if User Answer is not contained in Reference Answer or not accurate in all terms, topics, numbers, metrics, dates and units or the User Answer do not answer the question.
Do not explain or justify your rating. Your rating must be only 4, 2 or 0 according to the instructions above.
## Question
{query}
## Answer0
{sentence_inference}
## Answer1
{sentence_true}
## Rating`;
const template_accuracy2 = `
I will rate the User Answer in comparison to the Reference Answer for a given Question.
A rating of 4 indicates that the User Answer is entirely consistent with the Reference Answer, covering all aspects, topics, numbers, metrics, dates, and units.
A rating of 2 signifies that the User Answer is mostly aligned with the Reference Answer, with minor discrepancies in some areas.
A rating of 0 means that the User Answer is either inaccurate, incomplete, or unrelated to the Reference Answer, or it fails to address the Question.
I will provide the rating without any explanation or justification, adhering to the following scale: 0 (no match), 2 (partial match), 4 (exact match).
Do not explain or justify my rating. My rating must be only 4, 2 or 0 only.
## Question
{query}
## Answer0
{sentence_inference}
## Answer1
{sentence_true}
## Rating`;
export const getAppEvaluationScore = async ({
question,
appAnswer,
standardAnswer,
model
}: {
question: string;
appAnswer: string;
standardAnswer: string;
model: string;
}) => {
const modelData = getLLMModel(model);
if (!modelData) {
return Promise.reject('Evaluation model not found');
}
const getEvalResult = async (template: string) => {
const messages: ChatCompletionMessageParam[] = [
{
role: ChatCompletionRequestMessageRoleEnum.System,
content: template
},
{
role: ChatCompletionRequestMessageRoleEnum.User,
content: [
{
type: 'text',
text: `## Question
${question}
## Answer0
${appAnswer}
## Answer1
${standardAnswer}
## Rating`
}
]
}
];
const { response } = await createChatCompletion({
body: llmCompletionsBodyFormat(
{
model: modelData.model,
temperature: 0.3,
messages: await loadRequestMessages({ messages, useVision: true }),
stream: true,
max_tokens: 5
},
modelData
)
});
const { text, usage } = await formatLLMResponse(response);
const numberText = Number(text);
const rate = isNaN(numberText) ? 0 : numberText / 4;
return {
rate,
inputTokens: usage?.prompt_tokens || (await countGptMessagesTokens(messages)),
outputTokens: usage?.completion_tokens || (await countPromptTokens(text))
};
};
const results = await Promise.all([
getEvalResult(template_accuracy1),
getEvalResult(template_accuracy2)
]);
const accuracyScore =
Math.round((results.reduce((acc, item) => acc + item.rate, 0) / results.length) * 100) / 100;
const inputTokens = results.reduce((acc, item) => acc + item.inputTokens, 0);
const outputTokens = results.reduce((acc, item) => acc + item.outputTokens, 0);
return {
accuracyScore,
usage: {
inputTokens,
outputTokens
}
};
};