UNPKG

nyegosh

Version:

AI generated commit messages, like Njegoš wrote them.

71 lines (61 loc) 2.29 kB
const path = require("path"); const dotenv = require("dotenv"); const fs = require("fs"); const { AzureOpenAI, default: OpenAI } = require("openai"); const args = process.argv.slice(2); const isDevelopment = args.includes("--development"); const envPath = path.join(require("os").homedir(), ".nyegosh.env"); if (fs.existsSync(envPath)) { dotenv.config({ path: envPath }); console.log(`Environment variables loaded from ${envPath}`); } else if (isDevelopment) { dotenv.config({ path: ".env" }); console.log(`Environment variables loaded local .env`); } else { console.warn(`Environment file not found at ${envPath}. Please run the connection script.`); process.exit(1); } function initializeOpenAIModelConnection() { const { NYEGOSH_OPENAI_API_KEY } = process.env; if (!NYEGOSH_OPENAI_API_KEY) { console.error("Missing required environment variables. Please check your .env file."); process.exit(1); } return new OpenAI({ apiKey: NYEGOSH_OPENAI_API_KEY }); } function initializeAzureModelConnection() { const { NYEGOSH_GENERATOR_DEPLOYMENT, NYEGOSH_AZURE_OPENAI_ENDPOINT, NYEGOSH_AZURE_OPENAI_API_KEY, NYEGOSH_AZURE_OPENAI_API_VERSION, } = process.env; if ( !NYEGOSH_GENERATOR_DEPLOYMENT || !NYEGOSH_AZURE_OPENAI_ENDPOINT || !NYEGOSH_AZURE_OPENAI_API_KEY || !NYEGOSH_AZURE_OPENAI_API_VERSION ) { console.error("Missing required environment variables. Please check your .env file."); process.exit(1); } return new AzureOpenAI({ apiKey: NYEGOSH_AZURE_OPENAI_API_KEY, deployment: NYEGOSH_GENERATOR_DEPLOYMENT, apiVersion: NYEGOSH_AZURE_OPENAI_API_VERSION, endpoint: NYEGOSH_AZURE_OPENAI_ENDPOINT, }); } function initializeModelConnection() { const connectionType = process.env.NYEGOSH_CONNECTION_TYPE; switch (connectionType) { case "azure": return initializeAzureModelConnection(); case "openai": return initializeOpenAIModelConnection(); default: console.error("Invalid connection type. Please specify either 'azure' or 'openai'."); process.exit(1); } } module.exports = { initializeModelConnection };