From 75684d566a93f19b061cc81db348c730015993cd Mon Sep 17 00:00:00 2001 From: peterdanwan Date: Tue, 10 Sep 2024 21:04:02 -0400 Subject: [PATCH] Updated file to separate contacting the api vs. handling the api's output --- src/ai.js | 63 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/ai.js b/src/ai.js index 755cc56..1435f9b 100644 --- a/src/ai.js +++ b/src/ai.js @@ -1,6 +1,8 @@ // src/ai.js import dotenv from 'dotenv'; +import fs from 'fs'; +import path from 'path'; /**************************** API imports ***********************************/ // Reference: https://ai.google.dev/gemini-api/docs/text-generation?lang=node @@ -14,51 +16,84 @@ const genAI = new GoogleGenerativeAI(process.env.GEMINI_KEY); const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' }); // can provide system instruction // Publicly available function -export default async function promptAI(prompt, modelFlag = 'gemini', outputFlag = 'cmd') { - // Depending on which model is specified in modelFlag, call the right API's logic +export default async function promptAI(prompt, model, outputFile) { + if (model == null) { + // Set the model to whatever is configured in their .env (default to gemini) and if that isn't set, set it to 'gemini' + model = 'gemini'; + } - switch (modelFlag) { + switch (model) { case 'gemini': - await promptGemini(prompt, outputFlag); + await promptGemini(prompt, outputFile); break; case 'openai': - await promptOpenAi(prompt, outputFlag); + await promptOpenAi(prompt, outputFile); break; case 'claude': + break; default: break; } } // Helpers -async function promptGemini(prompt, outputFlag) { +async function promptGemini(prompt, outputFile) { // Depending on the outputFlag, output to stdout or paste the content into a file. + console.log('In promptGemini'); + try { // Generate content using the AI model const result = await model.generateContent(prompt); const responseText = result.response.text(); - - console.log(result.response.text()); + handleOutput(responseText, outputFile); } catch (error) { // Error will propagate to _gr.js throw Error(`Error prompting Gemini ${error}`); } } -async function promptOpenAi(prompt, outputFlag) { +// eslint-disable-next-line no-unused-vars +async function promptOpenAi(prompt, outputFile) { try { - + // prompt OpenAI } catch (error) { - + throw Error(`Error prompting OpenAI ${error}`); } } // handleOutput -function handleOutput(outputFlag) { - if (outputFlag == 'cmd') { +function handleOutput(responseText, outputFile) { + if (outputFile == null) { + console.log(responseText); + return; + } + + let filePath = path.resolve(outputFile); + let fileExists = fs.existsSync(filePath); + + // If the file exists, modify the filename slightly + if (fileExists) { + const ext = path.extname(outputFile); + const baseName = path.basename(outputFile, ext); + const dirName = path.dirname(outputFile); + + // Append a number to the filename to create a unique file + let counter = 1; + + while (fileExists) { + filePath = path.join(dirName, `${baseName}_${counter}${ext}`); + fileExists = fs.existsSync(filePath); + counter++; + } + + console.warn(`File already exists. Writing to new file: ${filePath}`); } else { - // output content into a README.md file + console.log(`Writing output to file: ${filePath}`); } + + // Write the content to the file + fs.writeFileSync(filePath, responseText, 'utf-8'); + console.log(`Output successfully written to ${filePath}`); }