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,304 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { handleContextRelevance } from '../../src/assertions/contextRelevance';
|
||||
import * as contextUtils from '../../src/assertions/contextUtils';
|
||||
import { matchesContextRelevance } from '../../src/matchers/rag';
|
||||
import { createMockProvider } from '../factories/provider';
|
||||
|
||||
vi.mock('../../src/matchers/rag');
|
||||
vi.mock('../../src/assertions/contextUtils');
|
||||
|
||||
describe('handleContextRelevance', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should pass when context relevance is above threshold', async () => {
|
||||
const mockResult = {
|
||||
pass: true,
|
||||
score: 0.9,
|
||||
reason: 'Context is highly relevant',
|
||||
metadata: {
|
||||
extractedSentences: ['Paris is the capital.'],
|
||||
totalContextSentences: 2,
|
||||
relevantSentenceCount: 1,
|
||||
insufficientInformation: false,
|
||||
score: 0.5,
|
||||
},
|
||||
};
|
||||
vi.mocked(matchesContextRelevance).mockResolvedValue(mockResult);
|
||||
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
|
||||
|
||||
const result = await handleContextRelevance({
|
||||
assertion: {
|
||||
type: 'context-relevance',
|
||||
threshold: 0.8,
|
||||
},
|
||||
test: {
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'France is a country in Europe. Paris is the capital.',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
output: 'test output',
|
||||
prompt: 'test prompt',
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'test prompt',
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'France is a country in Europe. Paris is the capital.',
|
||||
},
|
||||
test: {
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'France is a country in Europe. Paris is the capital.',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
outputString: 'test output',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any);
|
||||
|
||||
expect(result.pass).toBe(true);
|
||||
expect(result.score).toBe(0.9);
|
||||
expect(result.reason).toBe('Context is highly relevant');
|
||||
expect(result.metadata).toEqual({
|
||||
context: 'test context',
|
||||
extractedSentences: ['Paris is the capital.'],
|
||||
totalContextSentences: 2,
|
||||
relevantSentenceCount: 1,
|
||||
insufficientInformation: false,
|
||||
score: 0.5,
|
||||
});
|
||||
expect(matchesContextRelevance).toHaveBeenCalledWith(
|
||||
'What is the capital of France?',
|
||||
'test context',
|
||||
0.8,
|
||||
{},
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it('should fail when context relevance is below threshold', async () => {
|
||||
const mockResult = {
|
||||
pass: false,
|
||||
score: 0.3,
|
||||
reason: 'Context not relevant to query',
|
||||
metadata: {
|
||||
extractedSentences: [],
|
||||
totalContextSentences: 1,
|
||||
relevantSentenceCount: 0,
|
||||
insufficientInformation: true,
|
||||
score: 0,
|
||||
},
|
||||
};
|
||||
vi.mocked(matchesContextRelevance).mockResolvedValue(mockResult);
|
||||
vi.mocked(contextUtils.resolveContext).mockResolvedValue('irrelevant context');
|
||||
|
||||
const result = await handleContextRelevance({
|
||||
assertion: {
|
||||
type: 'context-relevance',
|
||||
threshold: 0.7,
|
||||
},
|
||||
test: {
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'Information about weather patterns in Australia.',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
output: 'test output',
|
||||
prompt: 'test prompt',
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'test prompt',
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'Information about weather patterns in Australia.',
|
||||
},
|
||||
test: {
|
||||
vars: {
|
||||
query: 'What is the capital of France?',
|
||||
context: 'Information about weather patterns in Australia.',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
outputString: 'test output',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any);
|
||||
|
||||
expect(result.pass).toBe(false);
|
||||
expect(result.score).toBe(0.3);
|
||||
expect(result.reason).toBe('Context not relevant to query');
|
||||
expect(result.metadata).toEqual({
|
||||
context: 'irrelevant context',
|
||||
extractedSentences: [],
|
||||
totalContextSentences: 1,
|
||||
relevantSentenceCount: 0,
|
||||
insufficientInformation: true,
|
||||
score: 0,
|
||||
});
|
||||
expect(matchesContextRelevance).toHaveBeenCalledWith(
|
||||
'What is the capital of France?',
|
||||
'irrelevant context',
|
||||
0.7,
|
||||
{},
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it('should use default threshold of 0 when not provided', async () => {
|
||||
const mockResult = { pass: true, score: 1, reason: 'Perfect relevance' };
|
||||
vi.mocked(matchesContextRelevance).mockResolvedValue(mockResult);
|
||||
vi.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
|
||||
|
||||
const result = await handleContextRelevance({
|
||||
assertion: {
|
||||
type: 'context-relevance',
|
||||
},
|
||||
test: {
|
||||
vars: {
|
||||
query: 'test query',
|
||||
context: 'test context',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
output: 'test output',
|
||||
prompt: 'test prompt',
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'test prompt',
|
||||
vars: { query: 'test query', context: 'test context' },
|
||||
test: { vars: { query: 'test query', context: 'test context' }, options: {} },
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
outputString: 'test output',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any);
|
||||
|
||||
expect(matchesContextRelevance).toHaveBeenCalledWith(
|
||||
'test query',
|
||||
'test context',
|
||||
0,
|
||||
{},
|
||||
undefined,
|
||||
);
|
||||
expect(result.metadata).toEqual({
|
||||
context: 'test context',
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw error when test.vars is missing', async () => {
|
||||
await expect(
|
||||
handleContextRelevance({
|
||||
assertion: { type: 'context-relevance' },
|
||||
test: {
|
||||
vars: undefined,
|
||||
options: {},
|
||||
},
|
||||
output: 'test output',
|
||||
prompt: 'test prompt',
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'test prompt',
|
||||
vars: {},
|
||||
test: { vars: undefined, options: {} },
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
outputString: 'test output',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any),
|
||||
).rejects.toThrow('context-relevance assertion requires a test with variables');
|
||||
});
|
||||
|
||||
it('should throw error when query is missing', async () => {
|
||||
await expect(
|
||||
handleContextRelevance({
|
||||
assertion: { type: 'context-relevance' },
|
||||
test: {
|
||||
vars: {
|
||||
context: 'test context',
|
||||
},
|
||||
options: {},
|
||||
},
|
||||
output: 'test output',
|
||||
prompt: 'test prompt',
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'test prompt',
|
||||
vars: { context: 'test context' },
|
||||
test: { vars: { context: 'test context' }, options: {} },
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
outputString: 'test output',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any),
|
||||
).rejects.toThrow(
|
||||
'context-relevance assertion requires a "query" variable with the user question',
|
||||
);
|
||||
});
|
||||
|
||||
it('should use contextTransform when provided', async () => {
|
||||
const mockResult = { pass: true, score: 1, reason: 'ok' };
|
||||
vi.mocked(matchesContextRelevance).mockResolvedValue(mockResult);
|
||||
vi.mocked(contextUtils.resolveContext).mockResolvedValue('cx');
|
||||
|
||||
const result = await handleContextRelevance({
|
||||
assertion: {
|
||||
type: 'context-relevance',
|
||||
contextTransform: 'expr',
|
||||
},
|
||||
test: {
|
||||
vars: { query: 'q' },
|
||||
options: {},
|
||||
},
|
||||
baseType: 'context-relevance',
|
||||
assertionValueContext: {
|
||||
prompt: 'p',
|
||||
vars: {},
|
||||
test: { vars: { query: 'q' }, options: {} },
|
||||
logProbs: undefined,
|
||||
provider: createMockProvider({ id: 'id', config: {} }),
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
},
|
||||
inverse: false,
|
||||
prompt: 'p',
|
||||
output: 'out',
|
||||
outputString: 'out',
|
||||
providerResponse: { output: 'out', tokenUsage: {} },
|
||||
} as any);
|
||||
|
||||
expect(contextUtils.resolveContext).toHaveBeenCalledWith(
|
||||
{ type: 'context-relevance', contextTransform: 'expr' },
|
||||
{ vars: { query: 'q' }, options: {} },
|
||||
'out',
|
||||
'p',
|
||||
undefined,
|
||||
{ output: 'out', tokenUsage: {} },
|
||||
);
|
||||
expect(matchesContextRelevance).toHaveBeenCalledWith('q', 'cx', 0, {}, undefined);
|
||||
expect(result.metadata).toEqual({
|
||||
context: 'cx',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user