Skip to content

Commit

Permalink
♻️ refactor: refactor the server config to migrate model provider env
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 10, 2024
1 parent 199ded2 commit c398063
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 414 deletions.
214 changes: 214 additions & 0 deletions src/app/api/config/__snapshots__/route.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`GET /api/config > Model Provider env > CUSTOM_MODELS > custom deletion, addition, and renaming of models 1`] = `
[
{
"displayName": "llama",
"enabled": true,
"functionCall": true,
"id": "llama",
"vision": true,
},
{
"displayName": "claude-2",
"enabled": true,
"functionCall": true,
"id": "claude-2",
"vision": true,
},
{
"displayName": "gpt-4-32k",
"enabled": true,
"functionCall": true,
"id": "gpt-4-0125-preview",
"tokens": 128000,
},
]
`;

exports[`GET /api/config > Model Provider env > OPENAI_MODEL_LIST > custom deletion, addition, and renaming of models 1`] = `
[
{
"displayName": "llama",
"enabled": true,
"functionCall": true,
"id": "llama",
"vision": true,
},
{
"displayName": "claude-2",
"enabled": true,
"functionCall": true,
"id": "claude-2",
"vision": true,
},
{
"displayName": "gpt-4-32k",
"enabled": true,
"functionCall": true,
"id": "gpt-4-0125-preview",
"tokens": 128000,
},
]
`;

exports[`GET /api/config > Model Provider env > OPENAI_MODEL_LIST > should work correct with gpt-4 1`] = `
[
{
"displayName": "GPT-3.5 Turbo (1106)",
"enabled": true,
"functionCall": true,
"id": "gpt-3.5-turbo-1106",
"tokens": 16385,
},
{
"description": "GPT 3.5 Turbo,适用于各种文本生成和理解任务",
"displayName": "GPT-3.5 Turbo",
"enabled": true,
"functionCall": true,
"id": "gpt-3.5-turbo",
"tokens": 16385,
},
{
"displayName": "GPT-3.5 Turbo 16K",
"enabled": true,
"id": "gpt-3.5-turbo-16k",
"tokens": 16385,
},
{
"displayName": "GPT-4",
"enabled": true,
"functionCall": true,
"id": "gpt-4",
"tokens": 8192,
},
{
"displayName": "GPT-4 32K",
"enabled": true,
"functionCall": true,
"id": "gpt-4-32k",
"tokens": 32768,
},
{
"displayName": "GPT-4 Turbo Preview (1106)",
"enabled": true,
"functionCall": true,
"id": "gpt-4-1106-preview",
"tokens": 128000,
},
{
"description": "GPT-4 视觉预览版,支持视觉任务",
"displayName": "GPT-4 Turbo Vision Preview",
"enabled": true,
"id": "gpt-4-vision-preview",
"tokens": 128000,
"vision": true,
},
]
`;

exports[`GET /api/config > Model Provider env > OPENROUTER_MODEL_LIST > custom deletion, addition, and renaming of models 1`] = `
[
{
"description": "GPT 3.5 Turbo,适用于各种文本生成和理解任务",
"displayName": "GPT-3.5 Turbo",
"enabled": true,
"functionCall": true,
"id": "gpt-3.5-turbo",
"tokens": 16385,
},
{
"displayName": "GPT-3.5 Turbo (0125)",
"functionCall": true,
"id": "gpt-3.5-turbo-0125",
"tokens": 16385,
},
{
"displayName": "GPT-3.5 Turbo (1106)",
"functionCall": true,
"id": "gpt-3.5-turbo-1106",
"tokens": 16385,
},
{
"displayName": "GPT-3.5 Turbo Instruct",
"id": "gpt-3.5-turbo-instruct",
"tokens": 4096,
},
{
"displayName": "GPT-3.5 Turbo 16K",
"id": "gpt-3.5-turbo-16k",
"tokens": 16385,
},
{
"displayName": "GPT-3.5 Turbo (0613)",
"id": "gpt-3.5-turbo-0613",
"legacy": true,
"tokens": 4096,
},
{
"displayName": "GPT-3.5 Turbo 16K (0613)",
"id": "gpt-3.5-turbo-16k-0613",
"legacy": true,
"tokens": 4096,
},
{
"displayName": "GPT-4 Turbo Preview",
"enabled": true,
"functionCall": true,
"id": "gpt-4-turbo-preview",
"tokens": 128000,
},
{
"displayName": "GPT-4 Turbo Preview (0125)",
"functionCall": true,
"id": "gpt-4-0125-preview",
"tokens": 128000,
},
{
"description": "GPT-4 视觉预览版,支持视觉任务",
"displayName": "GPT-4 Turbo Vision Preview",
"enabled": true,
"id": "gpt-4-vision-preview",
"tokens": 128000,
"vision": true,
},
{
"displayName": "GPT-4 Turbo Preview (1106)",
"functionCall": true,
"id": "gpt-4-1106-preview",
"tokens": 128000,
},
{
"displayName": "GPT-4",
"functionCall": true,
"id": "gpt-4",
"tokens": 8192,
},
{
"displayName": "GPT-4 (0613)",
"functionCall": true,
"id": "gpt-4-0613",
"tokens": 8192,
},
{
"displayName": "GPT-4 32K",
"functionCall": true,
"id": "gpt-4-32k",
"tokens": 32768,
},
{
"displayName": "GPT-4 32K (0613)",
"functionCall": true,
"id": "gpt-4-32k-0613",
"tokens": 32768,
},
{
"displayName": "GPT-4 ALL",
"files": true,
"functionCall": true,
"id": "gpt-4-all",
"tokens": 32768,
"vision": true,
},
]
`;
172 changes: 172 additions & 0 deletions src/app/api/config/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { OllamaProvider, OpenRouterProvider, TogetherAIProvider } from '@/config/modelProviders';
import { getServerConfig } from '@/config/server';
import { GlobalServerConfig } from '@/types/settings';

import { GET } from './route';

beforeEach(() => {
vi.resetAllMocks();
});

describe('GET /api/config', () => {
describe('Model Provider env', () => {
describe('OPENAI_MODEL_LIST', () => {
it('custom deletion, addition, and renaming of models', async () => {
process.env.OPENAI_MODEL_LIST =
'-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k';

const response = await GET();

// Assert
expect(response).toBeInstanceOf(Response);
expect(response.status).toBe(200);

const jsonResponse: GlobalServerConfig = await response.json();

const result = jsonResponse.languageModel?.openai?.serverModelCards;

expect(result).toMatchSnapshot();
process.env.OPENAI_MODEL_LIST = '';
});

it('should work correct with gpt-4', async () => {
process.env.OPENAI_MODEL_LIST =
'-all,+gpt-3.5-turbo-1106,+gpt-3.5-turbo,+gpt-3.5-turbo-16k,+gpt-4,+gpt-4-32k,+gpt-4-1106-preview,+gpt-4-vision-preview';

const response = await GET();
const jsonResponse: GlobalServerConfig = await response.json();

const result = jsonResponse.languageModel?.openai?.serverModelCards;

expect(result).toMatchSnapshot();

process.env.OPENAI_MODEL_LIST = '';
});

it('duplicate naming model', async () => {
process.env.OPENAI_MODEL_LIST =
'gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k';

const response = await GET();
const jsonResponse: GlobalServerConfig = await response.json();

const result = jsonResponse.languageModel?.openai?.serverModelCards;

expect(result?.find((s) => s.id === 'gpt-4-0125-preview')?.displayName).toEqual(
'gpt-4-32k',
);

process.env.OPENAI_MODEL_LIST = '';
});

it('should delete model', async () => {
process.env.OPENAI_MODEL_LIST = '-gpt-4';

const res = await GET();
const data: GlobalServerConfig = await res.json();

const result = data.languageModel?.openai?.serverModelCards;

expect(result?.find((r) => r.id === 'gpt-4')).toBeUndefined();

process.env.OPENAI_MODEL_LIST = '';
});

it('show the hidden model', async () => {
process.env.OPENAI_MODEL_LIST = '+gpt-4-1106-preview';

const res = await GET();
const data: GlobalServerConfig = await res.json();

const result = data.languageModel?.openai?.serverModelCards;

expect(result?.find((o) => o.id === 'gpt-4-1106-preview')).toEqual({
displayName: 'GPT-4 Turbo Preview (1106)',
functionCall: true,
enabled: true,
id: 'gpt-4-1106-preview',
tokens: 128000,
});

process.env.OPENAI_MODEL_LIST = '';
});

it('only add the model', async () => {
process.env.OPENAI_MODEL_LIST = 'model1,model2,model3,model4';

const res = await GET();
const data: GlobalServerConfig = await res.json();

const result = data.languageModel?.openai?.serverModelCards;

expect(result).toContainEqual({
displayName: 'model1',
functionCall: true,
id: 'model1',
enabled: true,
vision: true,
});
expect(result).toContainEqual({
displayName: 'model2',
functionCall: true,
enabled: true,
id: 'model2',
vision: true,
});
expect(result).toContainEqual({
displayName: 'model3',
enabled: true,
functionCall: true,
id: 'model3',
vision: true,
});
expect(result).toContainEqual({
displayName: 'model4',
functionCall: true,
enabled: true,
id: 'model4',
vision: true,
});

process.env.OPENAI_MODEL_LIST = '';
});
});

describe('CUSTOM_MODELS', () => {
it('custom deletion, addition, and renaming of models', async () => {
process.env.CUSTOM_MODELS =
'-all,+llama,+claude-2,-gpt-3.5-turbo,gpt-4-0125-preview=gpt-4-turbo,gpt-4-0125-preview=gpt-4-32k';

const response = await GET();

// Assert
expect(response).toBeInstanceOf(Response);
expect(response.status).toBe(200);

const jsonResponse: GlobalServerConfig = await response.json();

const result = jsonResponse.languageModel?.openai?.serverModelCards;

expect(result).toMatchSnapshot();
});
});

describe('OPENROUTER_MODEL_LIST', () => {
it('custom deletion, addition, and renaming of models', async () => {
process.env.OPENROUTER_MODEL_LIST =
'-all,+google/gemma-7b-it,+mistralai/mistral-7b-instruct=Mistral-7B-Instruct';

const res = await GET();
const data: GlobalServerConfig = await res.json();

const result = data.languageModel?.openai?.serverModelCards;

expect(result).toMatchSnapshot();

process.env.OPENROUTER_MODEL_LIST = '';
});
});
});
});
Loading

0 comments on commit c398063

Please sign in to comment.