feat: added basic gemini functionality

This commit is contained in:
devaine 2025-10-18 20:06:10 -05:00
commit d044eb5b06
No known key found for this signature in database
GPG key ID: 4BA6E50E8768348F
7 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,4 @@
import { createPartFromUri, GoogleGenAI } from "@google/genai"
import 'dotenv/config'
const ai = new GoogleGenAI({ apiKey: ${} })

View file

@ -1,5 +1,6 @@
{
"name": "codered-astra",
"type": "module",
"private": true,
"scripts": {
"build": "vite build",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,65 @@
import { createPartFromUri, GoogleGenAI } from "@google/genai"
import 'dotenv/config'
import fs from "fs"
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY })
async function uploadLocalPDFs() {
var pdfList = fs.readdirSync("public/pdfs")
// Upload each file in /public
pdfList.forEach(async (path) => {
console.log("file names: " + path)
console.log("file names: " + path.slice(0, path.length - 4))
console.log("UPLOADING")
const file = await ai.files.upload({
file: "public/pdfs/" + path,
config: {
displayName: path.slice(0, path.length - 4)
}
})
console.log("FETCHING: public/pdfs/" + path)
// Wait for the file to be processed
let getFile = await ai.files.get({
name: file.name
})
while (getFile.state === "PROCESSING") {
let getFile = await ai.files.get({
name: file.name
})
console.log(`Current file status: ${getFile.state}`)
console.log("File is currently processing, retrying in 5 seconds")
await new Promise((resolve) => {
setTimeout(resolve, 5000) // Checks every 5 seconds
})
// Error handling
if (getFile.state === "FAILED") {
throw new Error("File has failed to process!")
}
return file
}
})
}
async function main() {
const prompts = [
"If possible, using Gemini's Javascript API, how would you grab an image from a PDF sent to the API?"
]
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: prompts
})
console.log(response.text)
}
uploadLocalPDFs()
main()