feat: add support for defining custom models in config, switched toml library for proper serialization

This commit is contained in:
Justin Luoma 2024-05-24 06:48:15 -04:00
parent d04ba91c85
commit 62910b5879
7 changed files with 9767 additions and 5596 deletions

3
.gitignore vendored
View file

@ -2,10 +2,13 @@
node_modules/ node_modules/
npm-debug.log npm-debug.log
yarn-error.log yarn-error.log
.yarnrc.yml
# Build output # Build output
/.next/ /.next/
/out/ /out/
dist/
.yarn/
# IDE/Editor specific # IDE/Editor specific
.vscode/ .vscode/

View file

@ -20,7 +20,6 @@
"typescript": "^5.4.3" "typescript": "^5.4.3"
}, },
"dependencies": { "dependencies": {
"@iarna/toml": "^2.2.5",
"@langchain/openai": "^0.0.25", "@langchain/openai": "^0.0.25",
"@xenova/transformers": "^2.17.1", "@xenova/transformers": "^2.17.1",
"axios": "^1.6.8", "axios": "^1.6.8",
@ -30,8 +29,10 @@
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"express": "^4.19.2", "express": "^4.19.2",
"langchain": "^0.1.30", "langchain": "^0.1.30",
"smol-toml": "^1.0.0",
"winston": "^3.13.0", "winston": "^3.13.0",
"ws": "^8.16.0", "ws": "^8.16.0",
"zod": "^3.22.4" "zod": "^3.22.4"
} },
"packageManager": "yarn@3.6.1+sha512.de524adec81a6c3d7a26d936d439d2832e351cdfc5728f9d91f3fc85dd20b04391c038e9b4ecab11cae2b0dd9f0d55fd355af766bc5c1a7f8d25d96bb2a0b2ca"
} }

View file

@ -9,3 +9,23 @@ GROQ = "" # Groq API key - gsk_1234567890abcdef1234567890abcdef
[API_ENDPOINTS] [API_ENDPOINTS]
SEARXNG = "http://localhost:32768" # SearxNG API URL SEARXNG = "http://localhost:32768" # SearxNG API URL
OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434 OLLAMA = "" # Ollama API URL - http://host.docker.internal:11434
[[MODELS]]
name = "text-generation-webui"
api_key = "blah"
base_url = "http://localhost:5000/v1"
provider = "openai"
[[EMBEDDINGS]]
name = "text-generation-webui-small"
model = "text-embedding-3-small"
api_key = "blah"
base_url = "http://localhost:5000/v1"
provider = "openai"
[[EMBEDDINGS]]
name = "text-generation-webui-large"
model = "text-embedding-3-large"
api_key = "blah"
base_url = "http://localhost:5000/v1"
provider = "openai"

View file

@ -1,6 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import toml from '@iarna/toml'; import {parse, stringify} from "smol-toml";
const configFileName = 'config.toml'; const configFileName = 'config.toml';
@ -17,6 +17,23 @@ interface Config {
SEARXNG: string; SEARXNG: string;
OLLAMA: string; OLLAMA: string;
}; };
MODELS: [
{
"name": string;
"api_key": string;
"base_url": string;
"provider": string;
}
];
EMBEDDINGS: [
{
"name": string;
"model": string;
"api_key": string;
"base_url": string;
"provider": string;
}
];
} }
type RecursivePartial<T> = { type RecursivePartial<T> = {
@ -24,7 +41,7 @@ type RecursivePartial<T> = {
}; };
const loadConfig = () => const loadConfig = () =>
toml.parse( parse(
fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'), fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'),
) as any as Config; ) as any as Config;
@ -41,29 +58,22 @@ export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG;
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA; export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;
export const getCustomModels = () => loadConfig().MODELS;
export const getCustomEmbeddingModels = () => loadConfig().EMBEDDINGS;
export const updateConfig = (config: RecursivePartial<Config>) => { export const updateConfig = (config: RecursivePartial<Config>) => {
const currentConfig = loadConfig(); const currentConfig = loadConfig();
for (const key in currentConfig) { const updatedConfig = {
if (!config[key]) config[key] = {}; ...currentConfig,
...config
};
if (typeof currentConfig[key] === 'object' && currentConfig[key] !== null) { const toml = stringify(updatedConfig);
for (const nestedKey in currentConfig[key]) {
if (
!config[key][nestedKey] &&
currentConfig[key][nestedKey] &&
config[key][nestedKey] !== ''
) {
config[key][nestedKey] = currentConfig[key][nestedKey];
}
}
} else if (currentConfig[key] && config[key] !== '') {
config[key] = currentConfig[key];
}
}
fs.writeFileSync( fs.writeFileSync(
path.join(__dirname, `../${configFileName}`), path.join(__dirname, `../${configFileName}`),
toml.stringify(config), toml,
); );
}; };

View file

@ -3,6 +3,8 @@ import { ChatOllama } from '@langchain/community/chat_models/ollama';
import {OllamaEmbeddings} from '@langchain/community/embeddings/ollama'; import {OllamaEmbeddings} from '@langchain/community/embeddings/ollama';
import {HuggingFaceTransformersEmbeddings} from './huggingfaceTransformer'; import {HuggingFaceTransformersEmbeddings} from './huggingfaceTransformer';
import { import {
getCustomEmbeddingModels,
getCustomModels,
getGroqApiKey, getGroqApiKey,
getOllamaApiEndpoint, getOllamaApiEndpoint,
getOpenaiApiKey, getOpenaiApiKey,
@ -13,6 +15,7 @@ export const getAvailableChatModelProviders = async () => {
const openAIApiKey = getOpenaiApiKey(); const openAIApiKey = getOpenaiApiKey();
const groqApiKey = getGroqApiKey(); const groqApiKey = getGroqApiKey();
const ollamaEndpoint = getOllamaApiEndpoint(); const ollamaEndpoint = getOllamaApiEndpoint();
const customModels = getCustomModels();
const models = {}; const models = {};
@ -119,12 +122,36 @@ export const getAvailableChatModelProviders = async () => {
models['custom_openai'] = {}; models['custom_openai'] = {};
if (customModels && customModels.length > 0) {
models['custom'] = {};
try {
customModels.forEach((model) => {
if (model.provider === "openai") {
models['custom'] = {
...models['custom'],
[model.name]: new ChatOpenAI({
openAIApiKey: model.api_key,
modelName: model.name,
temperature: 0.7,
configuration: {
baseURL: model.base_url,
}
})
}
}
});
} catch (err) {
logger.error(`Error loading custom models: ${err}`);
}
}
return models; return models;
}; };
export const getAvailableEmbeddingModelProviders = async () => { export const getAvailableEmbeddingModelProviders = async () => {
const openAIApiKey = getOpenaiApiKey(); const openAIApiKey = getOpenaiApiKey();
const ollamaEndpoint = getOllamaApiEndpoint(); const ollamaEndpoint = getOllamaApiEndpoint();
const customEmbeddingModels = getCustomEmbeddingModels();
const models = {}; const models = {};
@ -134,7 +161,7 @@ export const getAvailableEmbeddingModelProviders = async () => {
'Text embedding 3 small': new OpenAIEmbeddings({ 'Text embedding 3 small': new OpenAIEmbeddings({
openAIApiKey, openAIApiKey,
modelName: 'text-embedding-3-small', modelName: 'text-embedding-3-small',
}), }, {baseURL: "http://10.0.1.2:5000/v1"}),
'Text embedding 3 large': new OpenAIEmbeddings({ 'Text embedding 3 large': new OpenAIEmbeddings({
openAIApiKey, openAIApiKey,
modelName: 'text-embedding-3-large', modelName: 'text-embedding-3-large',
@ -167,6 +194,28 @@ export const getAvailableEmbeddingModelProviders = async () => {
} }
} }
if (customEmbeddingModels && customEmbeddingModels.length > 0) {
models['custom'] = {};
try {
customEmbeddingModels.forEach((model) => {
if (model.provider === "openai") {
models['custom'] = {
...models['custom'],
[model.name]: new OpenAIEmbeddings({
openAIApiKey: model.api_key,
modelName: model.model,
},
{
baseURL: model.base_url,
}),
}
}
});
} catch (err) {
logger.error(`Error loading custom models: ${err}`);
}
}
try { try {
models['local'] = { models['local'] = {
'BGE Small': new HuggingFaceTransformersEmbeddings({ 'BGE Small': new HuggingFaceTransformersEmbeddings({

File diff suppressed because it is too large Load diff

6103
yarn.lock

File diff suppressed because it is too large Load diff