clean up openai adapters tests

This commit is contained in:
Nate 2025-02-15 19:26:31 -08:00
parent 62978d19ee
commit 92b07f2c58
5 changed files with 449 additions and 385 deletions

View file

@ -5,6 +5,7 @@ export default {
transform: {
"\\.[jt]sx?$": ["ts-jest", { useESM: true }],
},
testMatch: ["<rootDir>/src/**/*.test.{js,jsx,ts,tsx}"],
moduleNameMapper: {
"(.+)\\.js": "$1",

View file

@ -1,16 +1,17 @@
{
"name": "@continuedev/openai-adapters",
"version": "1.0.9",
"version": "1.0.12",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@continuedev/openai-adapters",
"version": "1.0.9",
"version": "1.0.12",
"license": "Apache-2.0",
"dependencies": {
"@azure/openai": "^1.0.0-beta.12",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.31",
"@continuedev/fetch": "^1.0.3",
"node-fetch": "^3.3.2",
"openai": "^4.76.0",
@ -701,13 +702,23 @@
"peer": true
},
"node_modules/@continuedev/config-types": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@continuedev/config-types/-/config-types-1.0.5.tgz",
"integrity": "sha512-Uv3JWKmpgSo9cMhZb2hWoJgR2ezuAn3CenEziWT/5d7mddPLzvuuHgFX6aDjLVVMojS1ul0rpfAd2LGxEb6nDQ==",
"version": "1.0.14",
"resolved": "https://registry.npmjs.org/@continuedev/config-types/-/config-types-1.0.14.tgz",
"integrity": "sha512-PVHyHPyRXd2QsaNgnCpiKYU3uHFTlyuQSkqE8OwrBmQqO6/TXUVIr/2EGtyIZGrml4Y+rGMSH40WU4/0t4SGpQ==",
"dependencies": {
"zod": "^3.23.8"
}
},
"node_modules/@continuedev/config-yaml": {
"version": "1.0.31",
"resolved": "https://registry.npmjs.org/@continuedev/config-yaml/-/config-yaml-1.0.31.tgz",
"integrity": "sha512-HNj+z7UTTARgsJTI0A1buM09fS4HoWVnn6Hbl6kJ2tZmOOm1OgogbcKP68AzHb4fy5H18MWnKS7XuseZpHFzHQ==",
"dependencies": {
"@continuedev/config-types": "^1.0.14",
"yaml": "^2.6.1",
"zod": "^3.23.8"
}
},
"node_modules/@continuedev/fetch": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.3.tgz",
@ -4455,6 +4466,17 @@
"dev": true,
"peer": true
},
"node_modules/yaml": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.7.0.tgz",
"integrity": "sha512-+hSoy/QHluxmC9kCIJyL/uyFmLmc+e5CFR5Wa+bpIhIj85LVb9ZH2nVnqrHoSvKogwODv0ClqZkmiSSaIH5LTA==",
"bin": {
"yaml": "bin.mjs"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/yargs": {
"version": "17.7.2",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",

View file

@ -14,6 +14,7 @@
"dependencies": {
"@azure/openai": "^1.0.0-beta.12",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.31",
"@continuedev/fetch": "^1.0.3",
"node-fetch": "^3.3.2",
"openai": "^4.76.0",

View file

@ -0,0 +1,420 @@
import { ModelConfig } from "@continuedev/config-yaml";
import * as dotenv from "dotenv";
import { BaseLlmApi, constructLlmApi } from "../index.js";
import { LLMConfig } from "../types.js";
dotenv.config();
function getLlmApi(config: LLMConfig) {
const api = constructLlmApi(config);
if (!api) {
throw new Error("Failed to construct LLM");
}
return api;
}
function testEmbed(config: LLMConfig, model: string) {
test("should successfully embed", async () => {
const api = getLlmApi(config);
const response = await api.embed({
model,
input: ["This is a test", "Hello world!"],
});
expect(response.model).toBe(model);
expect(response.object).toBe("list");
expect(response.data.length).toEqual(2);
response.data.forEach((val, index) => {
expect(val.index).toBe(index);
expect(val.object).toBe("embedding");
expect(val.embedding.some((v) => typeof v !== "number")).toBe(false);
});
});
}
function testRerank(config: LLMConfig, model: string) {
test("should successfully rerank", async () => {
const api = getLlmApi(config);
const response = await api.rerank({
model: model,
query: "What is the capital of spain?",
documents: [
"The capital of spain is Madrid",
"The largest breed of dog is the Great Dane",
],
});
expect(response.model).toBe(model);
expect(response.object).toBe("list");
expect(response.data.length).toEqual(2);
response.data.forEach((val, index) => {
expect(val.index).toBe(index);
expect(typeof val.relevance_score).toBe("number");
});
expect(response.data[0].relevance_score).toBeGreaterThan(
response.data[1].relevance_score,
);
});
}
function testFim(config: LLMConfig, model: string) {
test("should successfully fim", async () => {
const api = getLlmApi(config);
const response = api.fimStream(
{
model: model,
prompt: "This is a ",
suffix: " .",
stream: true,
},
new AbortController().signal,
);
let completion = "";
for await (const result of response) {
expect(result.choices.length).toBeGreaterThan(0);
expect(typeof result.choices[0].delta.content).toBe("string");
completion += result.choices[0].delta.content;
}
expect(completion.length).toBeGreaterThan(0);
});
}
function testChat(api: BaseLlmApi, model: string) {
test("should successfully stream chat", async () => {
const stream = api.chatCompletionStream(
{
model,
messages: [{ role: "user", content: "Hello! Who are you?" }],
stream: true,
},
new AbortController().signal,
);
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully stream multi-part chat with empty text", async () => {
const stream = api.chatCompletionStream(
{
model,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Hello! Who are you?",
},
{
// @ts-ignore
type: "text",
text: "",
},
],
},
],
stream: true,
},
new AbortController().signal,
);
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test.skip("should successfully stream multi-part chat with image", async () => {
const stream = api.chatCompletionStream(
{
model,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Hello! Who are you?",
},
{
// @ts-ignore
type: "image_url",
image_url: {
url: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png",
detail: "low",
},
},
],
},
],
stream: true,
},
new AbortController().signal,
);
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully non-stream chat", async () => {
const response = await api.chatCompletionNonStream(
{
model,
messages: [{ role: "user", content: "Hello! Who are you?" }],
stream: false,
},
new AbortController().signal,
);
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].message.content;
expect(typeof completion).toBe("string");
expect(completion?.length).toBeGreaterThan(0);
});
test("should acknowledge system message in chat", async () => {
const response = await api.chatCompletionNonStream(
{
model,
messages: [
{
role: "system",
content:
"Regardless of what is asked of you, your answer should start with 'RESPONSE: '.",
},
{ role: "user", content: "Who are you?" },
],
stream: false,
},
new AbortController().signal,
);
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].message.content;
expect(typeof completion).toBe("string");
expect(completion?.length).toBeGreaterThan(0);
expect(completion?.startsWith("RESPONSE: ")).toBe(true);
});
}
function testCompletion(api: BaseLlmApi, model: string) {
test("should successfully stream complete", async () => {
const stream = api.completionStream(
{
model: model,
prompt: "Hello! Who are you?",
stream: true,
},
new AbortController().signal,
);
let completion = "";
for await (const result of stream) {
completion += result.choices[0].text ?? "";
expect(typeof result.choices[0].text).toBe("string");
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully non-stream complete", async () => {
const response = await api.completionNonStream(
{
model,
prompt: "Hello! Who are you?",
stream: false,
},
new AbortController().signal,
);
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].text;
expect(typeof completion).toBe("string");
expect(completion.length).toBeGreaterThan(0);
});
}
function testConfig(config: ModelConfig) {
const model = config.model;
const api = getLlmApi({
provider: config.provider as any,
apiKey: config.apiKey,
apiBase: config.apiBase,
});
if (false) {
testCompletion(api, model);
}
if (
["chat", "summarize", "edit", "apply"].some((role) =>
config.roles?.includes(role as any),
)
) {
testChat(api, model);
}
}
const TESTS: Omit<ModelConfig, "name">[] = [
{
provider: "openai",
model: "gpt-4o",
apiKey: process.env.OPENAI_API_KEY!,
roles: ["chat"],
},
{
provider: "anthropic",
model: "claude-3-haiku-20240307",
apiKey: process.env.ANTHROPIC_API_KEY!,
roles: ["chat"],
},
{
provider: "gemini",
model: "gemini-1.5-flash-latest",
apiKey: process.env.GEMINI_API_KEY!,
roles: ["chat"],
},
// {
// provider: "cohere",
// model: "command-r",
// apiKey: process.env.COHERE_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "fireworks",
// model: "accounts/fireworks/models/llama-v3p1-8b-instruct",
// apiKey: process.env.FIREWORKS_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "groq",
// model: "llama3-8b-8192",
// apiKey: process.env.GROQ_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "deepinfra",
// model: "meta-llama/Meta-Llama-3.1-8B-Instruct",
// apiKey: process.env.DEEP_INFRA_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "together",
// model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
// apiKey: process.env.TOGETHER_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "novita",
// model: "meta-llama/llama-3.1-8b-instruct",
// apiKey: process.env.NOVITA_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "sambanova",
// model: "Meta-Llama-3.1-8B-Instruct",
// apiKey: process.env.SAMBANOVA_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "nebius",
// model: "llama3.1-8b",
// apiKey: process.env.NEBIUS_API_KEY!,
// roles: ["chat"],
// },
// {
// provider: "scaleway",
// model: "llama3.1-8b",
// apiKey: process.env.SCALEWAY_API_KEY!,
// roles: ["chat"],
// },
{
provider: "mistral",
model: "codestral-latest",
apiKey: process.env.MISTRAL_API_KEY!,
apiBase: "https://api.mistral.ai/v1",
roles: ["autocomplete"],
},
{
provider: "deepseek",
model: "deepseek-coder",
apiKey: process.env.DEEPSEEK_API_KEY!,
roles: ["autocomplete"],
},
{
provider: "openai",
model: "text-embedding-3-small",
apiKey: process.env.OPENAI_API_KEY!,
roles: ["embed"],
},
{
provider: "voyage",
model: "voyage-code-3",
apiKey: process.env.VOYAGE_API_KEY!,
roles: ["embed"],
},
// {
// provider: "cohere",
// model: "embed-english-v3.0",
// apiKey: process.env.COHERE_API_KEY!,
// roles: ["embed"],
// },
// {
// provider: "gemini",
// model: "models/text-embedding-004",
// apiKey: process.env.GEMINI_API_KEY!,
// roles: ["embed"],
// },
// {
// provider: "nebius",
// model: "BAAI/bge-en-icl",
// apiKey: process.env.NEBIUS_API_KEY!,
// roles: ["embed"],
// },
// {
// provider: "scaleway",
// model: "bge-multilingual-gemma2",
// apiKey: process.env.SCALEWAY_API_KEY!,
// roles: ["embed"],
// },
{
provider: "voyage",
model: "rerank-lite-1",
apiKey: process.env.VOYAGE_API_KEY!,
roles: ["rerank"],
},
// {
// provider: "cohere",
// model: "rerank-english-v3.0",
// apiKey: process.env.COHERE_API_KEY!,
// roles: ["rerank"],
// },
// {
// provider: "jina",
// model: "jina-reranker-v2-base-multilingual",
// apiKey: process.env.JINA_API_KEY!,
// roles: ["rerank"],
// },
];
describe("should successfully call all adapters", () => {
TESTS.forEach((config) => {
testConfig({ name: config.model, ...config });
});
});

View file

@ -1,380 +0,0 @@
import * as dotenv from "dotenv";
import { constructLlmApi, LlmApiConfig } from "../src/index.js";
dotenv.config();
function testEmbed(config: LlmApiConfig) {
test("should successfully embed", async () => {
const api = constructLlmApi(config);
const response = await api.embed({
model: config.model,
input: ["This is a test", "Hello world!"],
});
expect(response.model).toBe(config.model);
expect(response.object).toBe("list");
expect(response.data.length).toEqual(2);
response.data.forEach((val, index) => {
expect(val.index).toBe(index);
expect(val.object).toBe("embedding");
expect(val.embedding.some((v) => typeof v !== "number")).toBe(false);
});
});
}
function testRerank(config: LlmApiConfig) {
test("should successfully rerank", async () => {
const api = constructLlmApi(config);
const response = await api.rerank({
model: config.model,
query: "What is the capital of spain?",
documents: [
"The capital of spain is Madrid",
"The largest breed of dog is the Great Dane",
],
});
expect(response.model).toBe(config.model);
expect(response.object).toBe("list");
expect(response.data.length).toEqual(2);
response.data.forEach((val, index) => {
expect(val.index).toBe(index);
expect(typeof val.relevance_score).toBe("number");
});
expect(response.data[0].relevance_score).toBeGreaterThan(
response.data[1].relevance_score,
);
});
}
function testFim(config: LlmApiConfig) {
test("should successfully fim", async () => {
const api = constructLlmApi(config);
const response = api.fimStream({
model: config.model,
prompt: "This is a ",
suffix: " .",
stream: true,
});
let completion = "";
for await (const result of response) {
expect(result.choices.length).toBeGreaterThan(0);
expect(typeof result.choices[0].delta.content).toBe("string");
completion += result.choices[0].delta.content;
}
expect(completion.length).toBeGreaterThan(0);
});
}
function testConfig(config: LlmApiConfig, chatOnly: boolean = false) {
const api = constructLlmApi(config);
if (!chatOnly) {
test("should successfully stream complete", async () => {
const stream = api.completionStream({
model: config.model,
prompt: "Hello! Who are you?",
stream: true,
});
let completion = "";
for await (const result of stream) {
completion += result.choices[0].text ?? "";
expect(typeof result.choices[0].text).toBe("string");
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully non-stream complete", async () => {
const response = await api.completionNonStream({
model: config.model,
prompt: "Hello! Who are you?",
stream: false,
});
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].text;
expect(typeof completion).toBe("string");
expect(completion.length).toBeGreaterThan(0);
});
}
test("should successfully stream chat", async () => {
const stream = api.chatCompletionStream({
model: config.model,
messages: [{ role: "user", content: "Hello! Who are you?" }],
stream: true,
});
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully stream multi-part chat with empty text", async () => {
const stream = api.chatCompletionStream({
model: config.model,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Hello! Who are you?",
},
{
// @ts-ignore
type: "text",
text: "",
},
],
},
],
stream: true,
});
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test.skip("should successfully stream multi-part chat with image", async () => {
const stream = api.chatCompletionStream({
model: config.model,
messages: [
{
role: "user",
content: [
{
type: "text",
text: "Hello! Who are you?",
},
{
// @ts-ignore
type: "image_url",
imageUrl: {
url: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png",
detail: "low",
},
},
],
},
],
stream: true,
});
let completion = "";
for await (const result of stream) {
completion += result.choices[0].delta.content ?? "";
expect(result.choices.length).toBeGreaterThan(0);
}
expect(completion.length).toBeGreaterThan(0);
});
test("should successfully non-stream chat", async () => {
const response = await api.chatCompletionNonStream({
model: config.model,
messages: [{ role: "user", content: "Hello! Who are you?" }],
stream: false,
});
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].message.content;
expect(typeof completion).toBe("string");
expect(completion?.length).toBeGreaterThan(0);
});
test("should acknowledge system message in chat", async () => {
const response = await api.chatCompletionNonStream({
model: config.model,
messages: [
{
role: "system",
content:
"Regardless of what is asked of you, your answer should start with 'RESPONSE: '.",
},
{ role: "user", content: "Who are you?" },
],
stream: false,
});
expect(response.choices.length).toBeGreaterThan(0);
const completion = response.choices[0].message.content;
expect(typeof completion).toBe("string");
expect(completion?.length).toBeGreaterThan(0);
expect(completion?.startsWith("RESPONSE: ")).toBe(true);
});
}
const COMPLETION_TESTS: ({ chatOnly?: boolean } & LlmApiConfig)[] = [
{
provider: "openai",
model: "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY!,
chatOnly: true,
},
{
provider: "anthropic",
model: "claude-3-haiku-20240307",
apiKey: process.env.ANTHROPIC_API_KEY!,
chatOnly: true,
},
{
provider: "gemini",
model: "gemini-1.5-flash-latest",
apiKey: process.env.GEMINI_API_KEY!,
chatOnly: true,
},
{
provider: "cohere",
model: "command-r",
apiKey: process.env.COHERE_API_KEY!,
chatOnly: true,
},
{
provider: "fireworks",
model: "accounts/fireworks/models/llama-v3p1-8b-instruct",
apiKey: process.env.FIREWORKS_API_KEY!,
chatOnly: true,
},
{
provider: "groq",
model: "llama3-8b-8192",
apiKey: process.env.GROQ_API_KEY!,
chatOnly: true,
},
{
provider: "deepinfra",
model: "meta-llama/Meta-Llama-3.1-8B-Instruct",
apiKey: process.env.DEEP_INFRA_API_KEY!,
chatOnly: true,
},
{
provider: "together",
model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
apiKey: process.env.TOGETHER_API_KEY!,
chatOnly: true,
},
{
provider: "novita",
model: "meta-llama/llama-3.1-8b-instruct",
apiKey: process.env.NOVITA_API_KEY!,
chatOnly: true,
},
{
provider: "sambanova",
model: "Meta-Llama-3.1-8B-Instruct",
apiKey: process.env.SAMBANOVA_API_KEY!,
chatOnly: true,
},
{
provider: "nebius",
model: "llama3.1-8b",
apiKey: process.env.NEBIUS_API_KEY!,
chatOnly: true,
},
{
provider: "scaleway",
model: "llama3.1-8b",
apiKey: process.env.SCALEWAY_API_KEY!,
chatOnly: true,
},
];
const FIM_TESTS: LlmApiConfig[] = [
{
provider: "openai",
model: "codestral-latest",
apiKey: process.env.MISTRAL_API_KEY!,
apiBase: "https://api.mistral.ai/v1",
},
{
provider: "deepseek",
model: "deepseek-coder",
apiKey: process.env.DEEPSEEK_API_KEY!,
},
];
const EMBEDDINGS_TESTS: LlmApiConfig[] = [
{
provider: "openai",
model: "text-embedding-3-small",
apiKey: process.env.OPENAI_API_KEY!,
},
{
provider: "openai",
model: "voyage-code-3",
apiKey: process.env.VOYAGE_API_KEY!,
apiBase: "https://api.voyageai.com/v1/",
},
{
provider: "cohere",
model: "embed-english-v3.0",
apiKey: process.env.COHERE_API_KEY!,
},
{
provider: "gemini",
model: "models/text-embedding-004",
apiKey: process.env.GEMINI_API_KEY!,
},
{
provider: "nebius",
model: "BAAI/bge-en-icl",
apiKey: process.env.NEBIUS_API_KEY!,
},
{
provider: "scaleway",
model: "bge-multilingual-gemma2",
apiKey: process.env.SCALEWAY_API_KEY!,
},
];
const RERANK_TESTS: LlmApiConfig[] = [
{
provider: "openai",
model: "rerank-lite-1",
apiKey: process.env.VOYAGE_API_KEY!,
apiBase: "https://api.voyageai.com/v1/",
},
{
provider: "cohere",
model: "rerank-english-v3.0",
apiKey: process.env.COHERE_API_KEY!,
},
{
provider: "jina",
model: "jina-reranker-v2-base-multilingual",
apiKey: process.env.JINA_API_KEY!,
},
];
describe("should successfully call all adapters", () => {
COMPLETION_TESTS.forEach((config) => {
const { chatOnly, ...rest } = config;
testConfig(rest, chatOnly);
});
EMBEDDINGS_TESTS.forEach((config) => {
testEmbed(config);
});
RERANK_TESTS.forEach((config) => {
testRerank(config);
});
FIM_TESTS.forEach((config) => {
testFim(config);
});
});