-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularized code further - working on implementing different options
- Loading branch information
1 parent
cb42b27
commit 4ea9d20
Showing
4 changed files
with
118 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// src/ai.js | ||
|
||
import dotenv from 'dotenv'; | ||
|
||
/**************************** API imports ***********************************/ | ||
// Reference: https://ai.google.dev/gemini-api/docs/text-generation?lang=node | ||
import { GoogleGenerativeAI } from '@google/generative-ai'; | ||
|
||
// Make values from .env available | ||
dotenv.config(); | ||
|
||
// Initialize Google Generative AI client | ||
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 | ||
|
||
switch (modelFlag) { | ||
case 'gemini': | ||
await promptGemini(prompt, outputFlag); | ||
break; | ||
case 'openai': | ||
await promptOpenAi(prompt, outputFlag); | ||
break; | ||
case 'claude': | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
// Helpers | ||
async function promptGemini(prompt, outputFlag) { | ||
// Depending on the outputFlag, output to stdout or paste the content into a file. | ||
|
||
try { | ||
// Generate content using the AI model | ||
const result = await model.generateContent(prompt); | ||
const responseText = result.response.text(); | ||
|
||
|
||
console.log(result.response.text()); | ||
} catch (error) { | ||
// Error will propagate to _gr.js | ||
throw Error(`Error prompting Gemini ${error}`); | ||
} | ||
} | ||
|
||
async function promptOpenAi(prompt, outputFlag) { | ||
Check failure on line 50 in src/ai.js GitHub Actions / ESLint
|
||
try { | ||
|
||
} catch (error) { | ||
Check failure on line 53 in src/ai.js GitHub Actions / ESLint
|
||
|
||
} | ||
} | ||
|
||
// handleOutput | ||
function handleOutput(outputFlag) { | ||
if (outputFlag == 'cmd') { | ||
} else { | ||
// output content into a README.md file | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// src/getFileContent.js | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export default function getFileContent(filePath) { | ||
// Gets the absolute path of the file (this isn't sent to OpenAI) | ||
const resolvedPath = path.resolve(filePath); | ||
|
||
// Check if the file exists | ||
if (!fs.existsSync(resolvedPath)) { | ||
console.error(`Error: The file "${resolvedPath}" does not exist.`); | ||
return null; | ||
} | ||
|
||
// Read file content | ||
try { | ||
let fileContent = resolvedPath + '\n'; | ||
|
||
fileContent += fs.readFileSync(resolvedPath, 'utf-8'); | ||
|
||
console.log(`Resolved path is: ${resolvedPath}`); | ||
console.log(fileContent); | ||
console.log('\n'); | ||
|
||
return fileContent; | ||
} catch (error) { | ||
console.error(`Error reading file "${resolvedPath}": ${error.message}`); | ||
return null; | ||
} | ||
} |