From 18ca7c3eb7cbe12123706cc5ebaf59b677784ff5 Mon Sep 17 00:00:00 2001 From: alicia yu Date: Wed, 21 Aug 2024 19:17:33 -0700 Subject: [PATCH] update seeding/caching function to allow for multiple users on the fly.io server --- frontend/src/interface.js | 3 +-- server/controllers/deleteCodebase.js | 6 ++---- server/controllers/downloadCodebase.js | 5 +++-- server/controllers/pineconeQuery.js | 5 +++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/frontend/src/interface.js b/frontend/src/interface.js index c5ace3f..3a8e7c2 100644 --- a/frontend/src/interface.js +++ b/frontend/src/interface.js @@ -133,14 +133,13 @@ export async function fetchChatGPTResponse(userInput) { const ec2 = "https://18.226.98.245:443"; const local = "http://localhost:3000"; const apiURL = "https://syntaxsorcerer-backend.fly.dev"; - const codebasePath = "../../codebases"; const response = await fetch(`${apiURL}/api/chatgpt`, { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ prompt: userInput, codebasePath }), + body: JSON.stringify({ prompt: userInput }), }); const botMessage = await response.json(); diff --git a/server/controllers/deleteCodebase.js b/server/controllers/deleteCodebase.js index 7ae71db..b367085 100644 --- a/server/controllers/deleteCodebase.js +++ b/server/controllers/deleteCodebase.js @@ -2,11 +2,12 @@ const fs = require("fs"); const fsp = require("fs/promises"); const path = require("path"); const pinecone = require('../config/pineconeConfig/pineconeInit'); +const seed = require('../config/seedConfig'); const deleteCodebase = async (req, res) => { const codebasePath = path.join( path.resolve(__dirname, "../../"), - "codebases", + `codebases${seed}` ); if (!fs.existsSync(codebasePath)) { @@ -14,12 +15,9 @@ const deleteCodebase = async (req, res) => { } try { - const files = await fsp.readdir(codebasePath); await fsp.rm(codebasePath, { recursive: true, force: true }); // Delete everything from the Pinecone namespace - const index = "syntaxsorcerer"; - const namespace = "codebase"; // Default for our project await pinecone.deleteVectorsFromNamespace(); diff --git a/server/controllers/downloadCodebase.js b/server/controllers/downloadCodebase.js index 3fa3de1..df09589 100644 --- a/server/controllers/downloadCodebase.js +++ b/server/controllers/downloadCodebase.js @@ -3,6 +3,7 @@ const AdmZip = require("adm-zip"); const fs = require("fs"); const path = require("path"); const processFile = require('../database/processFile'); +const seed = require('../config/seedConfig'); // Endpoint to download and cache a codebase const downloadCodebase = async (req, res) => { @@ -11,7 +12,7 @@ const downloadCodebase = async (req, res) => { // Print an error message if the codebase already exists const codebasePath = path.join( path.resolve(__dirname, "../../"), - "codebases" + `codebases${seed}` ); if (fs.existsSync(codebasePath)) { return res.status(400).json({ error: "Codebase already cached; please delete it if you want to upload a new one" }); @@ -31,7 +32,7 @@ const downloadCodebase = async (req, res) => { const zip = new AdmZip(response.data); const extractPath = path.join( path.resolve(__dirname, "../../"), // Navigate to the root directory and add the codebases directory - "codebases", + `codebases${seed}`, path.basename(url, ".zip"), ); zip.extractAllTo(extractPath, true); diff --git a/server/controllers/pineconeQuery.js b/server/controllers/pineconeQuery.js index 3db14e4..d0c1e94 100644 --- a/server/controllers/pineconeQuery.js +++ b/server/controllers/pineconeQuery.js @@ -3,6 +3,7 @@ const {generateEmbeddings, processAndUpdateDictionary} = require("../database/e const fs = require("fs"); const path = require("path"); const readCodeFromFile = require('../database/readCodeFromFile'); +const seed = require('../config/seedConfig'); const getPineconeResponse = async (req, res) => { const userInput = req.body.prompt; @@ -10,13 +11,13 @@ const getPineconeResponse = async (req, res) => { // Print an error message if the codebase doesn't exist const codebasePath = path.join( path.resolve(__dirname, "../../"), - "codebases" + `codebases${seed}` ); if (!fs.existsSync(codebasePath)) { return res.json({ text: "You haven't uploaded a codebase yet! Please try again." }); } - shortPath = "../codebases/"; + shortPath = `../codebases${seed}/`; if (!userInput) { return res.status(400).json({ error: "Input is required" });