Skip to content

Commit

Permalink
Updated file to separate contacting the api vs. handling the api's ou…
Browse files Browse the repository at this point in the history
…tput
  • Loading branch information
peterdanwan committed Sep 11, 2024
1 parent 9eae47d commit 75684d5
Showing 1 changed file with 49 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/ai.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`);
}

0 comments on commit 75684d5

Please sign in to comment.