chore: import upstream snapshot with attribution
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# eval-assertion-scoring-override (Assertion Scoring Function Override Example)
|
||||
|
||||
You can run this example with:
|
||||
|
||||
```bash
|
||||
npx promptfoo@latest init --example eval-assertion-scoring-override
|
||||
cd eval-assertion-scoring-override
|
||||
```
|
||||
|
||||
This example demonstrates different ways to define and override the default scoring function in promptfoo. It shows three patterns for implementing and referencing scoring functions:
|
||||
|
||||
1. A global override in the `defaultTest` section of the config
|
||||
2. A named export in a JavaScript file
|
||||
3. A Python function export in a Python file
|
||||
|
||||
## Getting Started
|
||||
|
||||
Initialize the example:
|
||||
|
||||
```bash
|
||||
npx promptfoo@latest init --example eval-assertion-scoring-override
|
||||
```
|
||||
|
||||
Run the evaluation:
|
||||
|
||||
```bash
|
||||
cd eval-assertion-scoring-override
|
||||
promptfoo eval
|
||||
```
|
||||
|
||||
View the results:
|
||||
|
||||
```bash
|
||||
promptfoo view
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
// Default scoring function that weights metrics by geometric mean
|
||||
module.exports = (namedScores, context) => {
|
||||
const scores = {};
|
||||
for (const [key, value] of Object.entries(namedScores)) {
|
||||
scores[key] = value || 0;
|
||||
}
|
||||
|
||||
const totalScore = Math.pow(
|
||||
Object.values(scores).reduce((acc, score) => acc * score, 1),
|
||||
1 / Object.keys(scores).length,
|
||||
);
|
||||
|
||||
console.log('Default scoring function (JavaScript):', namedScores, 'Total score:', totalScore);
|
||||
const threshold = context?.threshold ?? 0.7;
|
||||
return {
|
||||
pass: totalScore >= threshold,
|
||||
score: totalScore,
|
||||
reason: `Weighted score calculation: ${Object.entries(scores)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join(', ')}`,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
const overrideScoring = (namedScores, context) => {
|
||||
console.log('Override scoring function (JavaScript):', namedScores);
|
||||
const accuracyScore = namedScores.accuracy || 0;
|
||||
const fluencyScore = namedScores.fluency || 0;
|
||||
const grammarScore = namedScores.grammar || 0;
|
||||
const bananaScore = namedScores.banana || 0;
|
||||
|
||||
const minScore = Math.min(accuracyScore, fluencyScore, grammarScore, bananaScore);
|
||||
const threshold = context?.threshold ?? 0.7;
|
||||
return {
|
||||
pass: minScore >= threshold,
|
||||
score: minScore,
|
||||
reason:
|
||||
`minimum score: ${minScore.toFixed(2)} (threshold: ${threshold})\n` +
|
||||
`Individual scores:\n` +
|
||||
`- contains banana: ${bananaScore.toFixed(2)}\n` +
|
||||
`- accuracy: ${accuracyScore.toFixed(2)}\n` +
|
||||
`- fluency: ${fluencyScore.toFixed(2)}\n` +
|
||||
`- grammar: ${grammarScore.toFixed(2)}`,
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
overrideScoring,
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
from typing import Any, Dict, Optional, Union
|
||||
|
||||
|
||||
def calculate_score(
|
||||
named_scores: Dict[str, float], context: Optional[Dict[str, Any]] = None
|
||||
) -> Dict[str, Union[bool, float, str]]:
|
||||
print("Override scoring function (Python):", named_scores)
|
||||
accuracy_score = named_scores.get("accuracy", 0)
|
||||
return {
|
||||
"pass": accuracy_score >= context.get("threshold", 0.7),
|
||||
"score": accuracy_score,
|
||||
"reason": f"Accuracy-only score: {accuracy_score}",
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
# yaml-language-server: $schema=https://promptfoo.dev/config-schema.json
|
||||
description: Example showing how to override scoring functions
|
||||
providers:
|
||||
- openai:gpt-4.1-mini
|
||||
|
||||
prompts:
|
||||
- 'Translate the following text to {{language}}: {{text}}'
|
||||
|
||||
defaultTest:
|
||||
assertScoringFunction: file://./default.js
|
||||
assert:
|
||||
- type: llm-rubric
|
||||
value: Rate the accuracy of the translation of {{text}} from English to {{language}}
|
||||
metric: accuracy
|
||||
- type: llm-rubric
|
||||
value: Rate the fluency of the translation of {{text}} from English to {{language}}
|
||||
metric: fluency
|
||||
- type: llm-rubric
|
||||
value: Rate the grammar of the translation of {{text}} from English to {{language}}
|
||||
metric: grammar
|
||||
options:
|
||||
provider:
|
||||
text: openai:gpt-4.1-mini
|
||||
embedding: openai:embedding:text-embedding-3-small
|
||||
|
||||
tests:
|
||||
- description: Default scoring function (geometric mean)
|
||||
vars:
|
||||
language: Spanish
|
||||
text: Hello, how are you today?
|
||||
assert:
|
||||
- type: similar
|
||||
value: Como estas?
|
||||
metric: similar
|
||||
|
||||
- description: Python scoring function (accuracy only)
|
||||
vars:
|
||||
language: French
|
||||
text: The weather is beautiful today.
|
||||
assertScoringFunction: file://./override.py:calculate_score
|
||||
|
||||
- description: JavaScript scoring function (minimum score)
|
||||
vars:
|
||||
language: German
|
||||
text: I love learning new languages.
|
||||
assert:
|
||||
- type: contains
|
||||
value: Bananas
|
||||
metric: banana
|
||||
assertScoringFunction: file://./override.js:overrideScoring
|
||||
Reference in New Issue
Block a user