UNPKG

gaunt-sloth-assistant

Version:

[![Tests and Lint](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml) [![Integration Tests](https://github.co

46 lines 1.78 kB
import { displayWarning } from '#src/consoleUtils.js'; import { env } from '#src/systemUtils.js'; import { writeFileIfNotExistsWithMessages } from '#src/utils.js'; // Function to process JSON config and create OpenAI LLM instance // noinspection JSUnusedGlobalSymbols export async function processJsonConfig(llmConfig) { const { ChatOpenAI } = await import('@langchain/openai'); // Use environment variable if available, otherwise use the config value const openaiApiKey = getApiKey(llmConfig); const configFields = { ...llmConfig, apiKey: openaiApiKey, model: llmConfig.model || 'gpt-4o', }; // eslint-disable-next-line @typescript-eslint/no-explicit-any delete configFields.type; // eslint-disable-next-line @typescript-eslint/no-explicit-any delete configFields.apiKeyEnvironmentVariable; return new ChatOpenAI(configFields); } function getApiKey(llmConfig) { // eslint-disable-next-line @typescript-eslint/no-explicit-any const conf = llmConfig; if (conf.apiKeyEnvironmentVariable && env[conf.apiKeyEnvironmentVariable]) { return env[conf.apiKeyEnvironmentVariable]; } else { return llmConfig.apiKey || env.OPENAI_API_KEY; } } const jsonContent = `{ "llm": { "type": "openai", "model": "gpt-4o" } }`; export function init(configFileName) { // Determine which content to use based on file extension if (!configFileName.endsWith('.json')) { throw new Error('Only JSON config is supported.'); } writeFileIfNotExistsWithMessages(configFileName, jsonContent); displayWarning(`You need to edit your ${configFileName} to configure model, ` + 'or define OPENAI_API_KEY environment variable.'); } //# sourceMappingURL=openai.js.map