0d3cb498a3
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
170 lines
4.9 KiB
TypeScript
170 lines
4.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { matchesGEval, matchesLlmRubric } from '../../src/matchers/llmGrading';
|
|
import { matchesAnswerRelevance } from '../../src/matchers/rag';
|
|
import { loadApiProvider } from '../../src/providers/index';
|
|
import { DefaultGradingProvider } from '../../src/providers/openai/defaults';
|
|
|
|
describe('Matcher Token Tracking', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('matchesLlmRubric', () => {
|
|
it('should track numRequests in token usage', async () => {
|
|
const mockProvider = await loadApiProvider('echo');
|
|
const mockCallApi = vi.fn().mockResolvedValue({
|
|
output: JSON.stringify({
|
|
pass: true,
|
|
score: 1,
|
|
reason: 'Test passed',
|
|
}),
|
|
tokenUsage: {
|
|
total: 100,
|
|
prompt: 60,
|
|
completion: 40,
|
|
cached: 0,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
|
|
mockProvider.callApi = mockCallApi;
|
|
|
|
const result = await matchesLlmRubric(
|
|
'Test rubric',
|
|
'Test output',
|
|
{ provider: mockProvider },
|
|
{},
|
|
undefined,
|
|
);
|
|
|
|
expect(result.tokensUsed).toBeDefined();
|
|
expect(result.tokensUsed?.numRequests).toBe(1);
|
|
expect(result.tokensUsed?.total).toBe(100);
|
|
expect(result.tokensUsed?.prompt).toBe(60);
|
|
expect(result.tokensUsed?.completion).toBe(40);
|
|
expect(result.tokensUsed?.cached).toBe(0);
|
|
});
|
|
|
|
it('should default numRequests to 0 when not provided', async () => {
|
|
const mockProvider = await loadApiProvider('echo');
|
|
const mockCallApi = vi.fn().mockResolvedValue({
|
|
output: JSON.stringify({
|
|
pass: true,
|
|
score: 1,
|
|
reason: 'Test passed',
|
|
}),
|
|
tokenUsage: {
|
|
total: 100,
|
|
prompt: 60,
|
|
completion: 40,
|
|
cached: 0,
|
|
// numRequests not provided
|
|
},
|
|
});
|
|
|
|
mockProvider.callApi = mockCallApi;
|
|
|
|
const result = await matchesLlmRubric(
|
|
'Test rubric',
|
|
'Test output',
|
|
{ provider: mockProvider },
|
|
{},
|
|
undefined,
|
|
);
|
|
|
|
expect(result.tokensUsed).toBeDefined();
|
|
expect(result.tokensUsed?.numRequests).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('matchesGEval', () => {
|
|
it('should accumulate numRequests across multiple API calls', async () => {
|
|
const mockCallApi = vi.spyOn(DefaultGradingProvider, 'callApi');
|
|
|
|
// First call for steps
|
|
mockCallApi.mockResolvedValueOnce({
|
|
output: JSON.stringify({
|
|
steps: ['Step 1', 'Step 2'],
|
|
}),
|
|
tokenUsage: {
|
|
total: 50,
|
|
prompt: 30,
|
|
completion: 20,
|
|
cached: 0,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
|
|
// Second call for evaluation
|
|
mockCallApi.mockResolvedValueOnce({
|
|
output: JSON.stringify({
|
|
score: 8,
|
|
reason: 'Good response',
|
|
}),
|
|
tokenUsage: {
|
|
total: 100,
|
|
prompt: 60,
|
|
completion: 40,
|
|
cached: 10,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
|
|
const result = await matchesGEval('Test criteria', 'Test input', 'Test output', 0.7, {});
|
|
|
|
expect(result.tokensUsed).toBeDefined();
|
|
expect(result.tokensUsed?.numRequests).toBe(2); // 1 from steps + 1 from evaluation
|
|
expect(result.tokensUsed?.total).toBe(150); // 50 + 100
|
|
expect(result.tokensUsed?.prompt).toBe(90); // 30 + 60
|
|
expect(result.tokensUsed?.completion).toBe(60); // 20 + 40
|
|
expect(result.tokensUsed?.cached).toBe(10); // 0 + 10
|
|
});
|
|
});
|
|
|
|
describe('matchesAnswerRelevance', () => {
|
|
it('should track numRequests from embedding calls', async () => {
|
|
const { DefaultGradingProvider, DefaultEmbeddingProvider } = await import(
|
|
'../../src/providers/openai/defaults'
|
|
);
|
|
|
|
const mockCallApi = vi.spyOn(DefaultGradingProvider, 'callApi');
|
|
const mockCallEmbeddingApi = vi.spyOn(DefaultEmbeddingProvider, 'callEmbeddingApi');
|
|
|
|
// Mock text generation calls (3 times for candidate questions)
|
|
mockCallApi.mockResolvedValue({
|
|
output: 'Generated question',
|
|
tokenUsage: {
|
|
total: 20,
|
|
prompt: 10,
|
|
completion: 10,
|
|
cached: 0,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
|
|
// Mock embedding calls
|
|
mockCallEmbeddingApi.mockResolvedValue({
|
|
embedding: [1, 0, 0],
|
|
tokenUsage: {
|
|
total: 5,
|
|
prompt: 5,
|
|
completion: 0,
|
|
cached: 0,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
|
|
const result = await matchesAnswerRelevance('Test input', 'Test output', 0.7, {});
|
|
|
|
expect(result.tokensUsed).toBeDefined();
|
|
// 3 text generation + 1 input embedding + 3 question embeddings = 7 requests
|
|
expect(result.tokensUsed?.numRequests).toBe(7);
|
|
});
|
|
});
|
|
});
|