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
159 lines
4.6 KiB
TypeScript
159 lines
4.6 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { disableCache, enableCache, fetchWithCache } from '../../../src/cache';
|
|
import { OpenAiEmbeddingProvider } from '../../../src/providers/openai/embedding';
|
|
import { mockProcessEnv } from '../../util/utils';
|
|
import { getOpenAiMissingApiKeyMessage } from './shared';
|
|
|
|
vi.mock('../../../src/cache');
|
|
|
|
describe('OpenAI Provider', () => {
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
disableCache();
|
|
});
|
|
|
|
afterEach(() => {
|
|
enableCache();
|
|
});
|
|
|
|
describe('OpenAiEmbeddingProvider', () => {
|
|
const configuredEmbeddingCostPerToken = 0.42 / 1e6;
|
|
const provider = new OpenAiEmbeddingProvider('text-embedding-3-large', {
|
|
config: {
|
|
apiKey: 'test-key',
|
|
cost: configuredEmbeddingCostPerToken,
|
|
},
|
|
});
|
|
|
|
it('should call embedding API successfully', async () => {
|
|
const mockResponse = {
|
|
data: [
|
|
{
|
|
embedding: [0.1, 0.2, 0.3],
|
|
},
|
|
],
|
|
usage: {
|
|
total_tokens: 10,
|
|
prompt_tokens: 0,
|
|
completion_tokens: 0,
|
|
},
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue({
|
|
data: mockResponse,
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
});
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
const expectedCost = 10 * provider.config.cost!;
|
|
expect(result.embedding).toEqual([0.1, 0.2, 0.3]);
|
|
expect(result.tokenUsage).toEqual({
|
|
total: 10,
|
|
prompt: 0,
|
|
completion: 0,
|
|
numRequests: 1,
|
|
});
|
|
expect(result.cost).toBeCloseTo(expectedCost, 12);
|
|
});
|
|
|
|
it('should pass through embedding request fields', async () => {
|
|
const passthroughProvider = new OpenAiEmbeddingProvider('text-embedding-3-small', {
|
|
config: {
|
|
apiKey: 'test-key',
|
|
passthrough: {
|
|
dimensions: 8,
|
|
encoding_format: 'float',
|
|
},
|
|
},
|
|
});
|
|
|
|
const mockEmbeddingResponse = {
|
|
data: [{ embedding: [0.1, 0.2, 0.3] }],
|
|
usage: {
|
|
total_tokens: 10,
|
|
prompt_tokens: 10,
|
|
completion_tokens: 0,
|
|
},
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValue({
|
|
data: mockEmbeddingResponse,
|
|
cached: false,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
});
|
|
|
|
await passthroughProvider.callEmbeddingApi('test text');
|
|
|
|
expect(fetchWithCache).toHaveBeenCalledWith(
|
|
expect.stringContaining('/embeddings'),
|
|
expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
'X-OpenAI-Originator': 'promptfoo',
|
|
}),
|
|
body: JSON.stringify({
|
|
input: 'test text',
|
|
model: 'text-embedding-3-small',
|
|
dimensions: 8,
|
|
encoding_format: 'float',
|
|
}),
|
|
}),
|
|
expect.any(Number),
|
|
'json',
|
|
false,
|
|
undefined,
|
|
);
|
|
expect(mockEmbeddingResponse.usage.completion_tokens).toBe(0);
|
|
});
|
|
|
|
it('should handle API errors', async () => {
|
|
vi.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
expect(result.error).toBe('API call error: Error: API error');
|
|
expect(result.embedding).toBeUndefined();
|
|
});
|
|
|
|
it('should validate input type', async () => {
|
|
const result = await provider.callEmbeddingApi({ message: 'test' } as unknown as string);
|
|
expect(result.error).toBe(
|
|
'Invalid input type for embedding API. Expected string, got object. Input: {"message":"test"}',
|
|
);
|
|
expect(result.embedding).toBeUndefined();
|
|
});
|
|
|
|
it('should handle HTTP error status', async () => {
|
|
vi.mocked(fetchWithCache).mockResolvedValue({
|
|
data: { error: { message: 'Unauthorized' } },
|
|
cached: false,
|
|
status: 401,
|
|
statusText: 'Unauthorized',
|
|
});
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
expect(result.error).toBe(
|
|
'API error: 401 Unauthorized\n{"error":{"message":"Unauthorized"}}',
|
|
);
|
|
expect(result.embedding).toBeUndefined();
|
|
});
|
|
|
|
it('should validate API key', async () => {
|
|
const restoreEnv = mockProcessEnv({ OPENAI_API_KEY: undefined });
|
|
|
|
try {
|
|
const providerNoKey = new OpenAiEmbeddingProvider('text-embedding-3-large', {
|
|
config: {},
|
|
});
|
|
|
|
const result = await providerNoKey.callEmbeddingApi('test text');
|
|
expect(result.error).toBe(getOpenAiMissingApiKeyMessage());
|
|
expect(result.embedding).toBeUndefined();
|
|
} finally {
|
|
restoreEnv();
|
|
}
|
|
});
|
|
});
|
|
});
|