Perplexica/src/config.ts

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-04-20 09:32:19 +05:30
import fs from 'fs';
import path from 'path';
2024-04-20 11:18:52 +05:30
import toml from '@iarna/toml';
2024-04-20 09:32:19 +05:30
const configFileName = 'config.toml';
interface Config {
GENERAL: {
PORT: number;
SIMILARITY_MEASURE: string;
2024-11-20 19:11:47 +05:30
KEEP_ALIVE: string;
2024-04-20 09:32:19 +05:30
};
API_KEYS: {
OPENAI: string;
2024-05-01 19:43:06 +05:30
GROQ: string;
2024-07-15 21:20:16 +05:30
ANTHROPIC: string;
2024-11-28 20:47:18 +05:30
GEMINI: string;
2024-04-20 09:32:19 +05:30
};
API_ENDPOINTS: {
SEARXNG: string;
2024-04-20 11:18:52 +05:30
OLLAMA: string;
2024-04-20 09:32:19 +05:30
};
}
type RecursivePartial<T> = {
[P in keyof T]?: RecursivePartial<T[P]>;
};
2024-04-20 09:32:19 +05:30
const loadConfig = () =>
toml.parse(
2024-04-20 11:18:52 +05:30
fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'),
2024-04-20 09:32:19 +05:30
) as any as Config;
export const getPort = () => loadConfig().GENERAL.PORT;
export const getSimilarityMeasure = () =>
loadConfig().GENERAL.SIMILARITY_MEASURE;
2024-11-20 19:11:47 +05:30
export const getKeepAlive = () => loadConfig().GENERAL.KEEP_ALIVE;
2024-04-20 09:32:19 +05:30
export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI;
2024-05-01 19:43:06 +05:30
export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
2024-07-15 21:20:16 +05:30
export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC;
2024-11-28 20:47:18 +05:30
export const getGeminiApiKey = () => loadConfig().API_KEYS.GEMINI;
export const getSearxngApiEndpoint = () =>
process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG;
2024-04-20 11:18:52 +05:30
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;
export const updateConfig = (config: RecursivePartial<Config>) => {
const currentConfig = loadConfig();
for (const key in currentConfig) {
if (!config[key]) config[key] = {};
if (typeof currentConfig[key] === 'object' && currentConfig[key] !== null) {
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(
path.join(__dirname, `../${configFileName}`),
toml.stringify(config),
);
};
export const config = {
ollama: {
url: process.env.OLLAMA_URL || 'http://localhost:11434',
model: process.env.OLLAMA_MODEL || 'mistral',
options: {
temperature: 0.1,
top_p: 0.9,
timeout: 30000 // 30 seconds timeout
}
},
// ... other config
};