From 8bb0a29a1a69064293c3050012882045c7305b46 Mon Sep 17 00:00:00 2001 From: Suryansh Kushwaha <112935210+suryanshkushwaha@users.noreply.github.com> Date: Sat, 9 Nov 2024 17:37:44 +0800 Subject: [PATCH 1/5] Update server.js --- peerprep/backend/collab-service/src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peerprep/backend/collab-service/src/server.js b/peerprep/backend/collab-service/src/server.js index d37a4ed280..d4e65d3ac5 100644 --- a/peerprep/backend/collab-service/src/server.js +++ b/peerprep/backend/collab-service/src/server.js @@ -12,7 +12,7 @@ const cors = require("cors"); const app = express(); const server = http.createServer(app); -const wss = new WebSocket.Server({ server }); +const wss = new WebSocket.Server({ noServer: true }); app.use(cors()); app.use(express.json()); From 47d6085631674b49ed7bfba973336afcf3374716 Mon Sep 17 00:00:00 2001 From: Suryansh Kushwaha <112935210+suryanshkushwaha@users.noreply.github.com> Date: Sat, 9 Nov 2024 22:45:04 +0800 Subject: [PATCH 2/5] code refactor: gpt NTH --- peerprep/backend/collab-service/.gitignore | 3 + peerprep/backend/collab-service/package.json | 1 + .../src/controller/gptController.js | 57 +++++++++++++++++++ .../collab-service/src/routes/gptRoutes.js | 9 +++ peerprep/backend/collab-service/src/server.js | 4 ++ .../src/controllers/gptController.ts | 54 ------------------ .../question-service/src/routes/gptRoutes.ts | 9 --- .../backend/question-service/src/server.ts | 3 - .../api/{assescodeApi.ts => assesscodeApi.ts} | 8 +-- .../CollabServiceIntegratedView.tsx | 9 +-- 10 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 peerprep/backend/collab-service/.gitignore create mode 100644 peerprep/backend/collab-service/src/controller/gptController.js create mode 100644 peerprep/backend/collab-service/src/routes/gptRoutes.js delete mode 100644 peerprep/backend/question-service/src/controllers/gptController.ts delete mode 100644 peerprep/backend/question-service/src/routes/gptRoutes.ts rename peerprep/frontend/src/api/{assescodeApi.ts => assesscodeApi.ts} (82%) diff --git a/peerprep/backend/collab-service/.gitignore b/peerprep/backend/collab-service/.gitignore new file mode 100644 index 0000000000..d09104f042 --- /dev/null +++ b/peerprep/backend/collab-service/.gitignore @@ -0,0 +1,3 @@ +.env + +node_modules \ No newline at end of file diff --git a/peerprep/backend/collab-service/package.json b/peerprep/backend/collab-service/package.json index ea60abe041..f43330d2bd 100644 --- a/peerprep/backend/collab-service/package.json +++ b/peerprep/backend/collab-service/package.json @@ -16,6 +16,7 @@ ] }, "dependencies": { + "axios": "^1.7.7", "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.1", diff --git a/peerprep/backend/collab-service/src/controller/gptController.js b/peerprep/backend/collab-service/src/controller/gptController.js new file mode 100644 index 0000000000..8291b6ef52 --- /dev/null +++ b/peerprep/backend/collab-service/src/controller/gptController.js @@ -0,0 +1,57 @@ +const axios = require('axios'); + +const assessCode = async (req, res) => { + console.log('assessCode controller activated'); + try { + const { codeDetails } = req.body; + if (!codeDetails) { + res.status(400).json({ error: 'Code content is required' }); + return; + } + + const instructionalPrompt = + "Analyze the following: 1: Question, and its 2: Description, and 3: The code attempt. " + "\n" + + "Assess the code, focusing on its efficiency and style. Determine the correctness of the code, given the language and question specified. " + + "Your response should include:" + "\n" + + "\n" + + "\t1. Time Complexity – Provide the Big-O notation." + "\n" + + "\t2. Space Complexity – Provide the Big-O notation." + "\n" + + "\t3. Code Style – Briefly assess readability, naming conventions, and formatting." + "\n" + + "\t4. Optimization Hints – Suggest improvements if the time or space complexity could be reduced." + "\n" + + "\t5. General Comments – Summarize any other relevant observations and assess correctness to question requirements " + + "(e.g., potential edge cases, overall structure)." + "\n" + + "\n" + + "Keep each response concise but comprehensive."; + + console.log('Submitting code to OpenAI API:', instructionalPrompt, codeDetails); + + // API request to OpenAI for code assessment + const response = await axios.post( + 'https://api.openai.com/v1/chat/completions', + { + model: 'gpt-4', + messages: [ + { role: 'system', content: "You are a coding assistant." }, + { role: 'user', content: `${instructionalPrompt}\n\n${codeDetails}` } + ], + temperature: 0 + }, + { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` + } + } + ); + + const feedback = response.data.choices[0].message.content; + res.json({ feedback }); + } catch (error) { + console.error('Error in assessCode controller:', error); + res.status(500).json({ error: 'Failed to process code assessment' }); + } +}; + +module.exports = { + assessCode +}; \ No newline at end of file diff --git a/peerprep/backend/collab-service/src/routes/gptRoutes.js b/peerprep/backend/collab-service/src/routes/gptRoutes.js new file mode 100644 index 0000000000..fec36ac692 --- /dev/null +++ b/peerprep/backend/collab-service/src/routes/gptRoutes.js @@ -0,0 +1,9 @@ +const { Router } = require('express'); +const { assessCode } = require('../controller/gptController'); + +const router = Router(); + +// Route for handling code assessment with GPT +router.post('/gpt/assess', assessCode); + +module.exports = router; diff --git a/peerprep/backend/collab-service/src/server.js b/peerprep/backend/collab-service/src/server.js index d4e65d3ac5..8582a1082b 100644 --- a/peerprep/backend/collab-service/src/server.js +++ b/peerprep/backend/collab-service/src/server.js @@ -9,6 +9,8 @@ const connectDB = require('../config/db'); const { storeDocument, getDocument } = require('./controller/collab-controller'); const { Server } = require("socket.io"); const cors = require("cors"); +const gptRoutes = require('./routes/gptRoutes'); +const dotenv = require('dotenv'); const app = express(); const server = http.createServer(app); @@ -20,6 +22,8 @@ app.use(express.json()); // Connect to MongoDB connectDB(); +app.use('/api', gptRoutes); + // Endpoint to save a document to MongoDB app.post('/api/saveDocument', async (req, res) => { try { diff --git a/peerprep/backend/question-service/src/controllers/gptController.ts b/peerprep/backend/question-service/src/controllers/gptController.ts deleted file mode 100644 index 2ec75268d7..0000000000 --- a/peerprep/backend/question-service/src/controllers/gptController.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { Request, Response } from 'express'; -import axios from 'axios'; - -export const assesCode = async (req: Request, res: Response): Promise => { - console.log('assesCode controller activated'); - try { - const { codeDetails } = req.body; - - if (!codeDetails) { - res.status(400).json({ error: 'Code content is required' }); - return; - } - - const instructionalPrompt = - "Analyze the following: 1: Question, and its 2: Description, and 3: The code attempt. " + "\n" + - "Asses the code, focusing on its efficiency and style. Determine the correctness of the code, given the language and question specified. " + - "Your response should include:" + "\n" + - "\n" + - "\t1. Time Complexity – Provide the Big-O notation." + "\n" + - "\t2. Space Complexity – Provide the Big-O notation." + "\n" + - "\t3. Code Style – Briefly assess readability, naming conventions, and formatting." + "\n" + - "\t4. Optimization Hints – Suggest improvements if the time or space complexity could be reduced." + "\n" + - "\t5. General Comments – Summarize any other relevant observations and asses correctness to question requirements " + - "(e.g., potential edge cases, overall structure)." + "\n" + - "\n" + - "Keep each response concise but comprehensive."; - console.log('Submitting code to OpenAI API:', instructionalPrompt, codeDetails); - - // API request to OpenAI for code assessment - const response = await axios.post( - 'https://api.openai.com/v1/chat/completions', - { - model: 'gpt-4', - messages: [ - { role: 'system', content: "You are a coding assistant." }, - { role: 'user', content: `${instructionalPrompt}\n\n${codeDetails}` } - ], - temperature: 0 - }, - { - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` - } - } - ); - - const feedback = response.data.choices[0].message.content; - res.json({ feedback }); - } catch (error) { - console.error('Error in assessCode controller:', error); - res.status(500).json({ error: 'Failed to process code assessment' }); - } -}; \ No newline at end of file diff --git a/peerprep/backend/question-service/src/routes/gptRoutes.ts b/peerprep/backend/question-service/src/routes/gptRoutes.ts deleted file mode 100644 index ab681a46c9..0000000000 --- a/peerprep/backend/question-service/src/routes/gptRoutes.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Router } from 'express'; -import { assesCode } from '../controllers/gptController'; - -const router: Router = Router(); - -// Route for handling code assessment with GPT -router.post('/gpt/asses', assesCode); - -export default router; \ No newline at end of file diff --git a/peerprep/backend/question-service/src/server.ts b/peerprep/backend/question-service/src/server.ts index 3ed070789b..4831dfa5c8 100644 --- a/peerprep/backend/question-service/src/server.ts +++ b/peerprep/backend/question-service/src/server.ts @@ -6,7 +6,6 @@ dotenv.config(); import connectDB from '../config/db'; import questionRoutes from './routes/questionRoutes'; import databaseRoutes from './routes/databaseRoutes'; -import gptRoutes from './routes/gptRoutes'; import loadSampleData from './sampleData'; connectDB() // Initialize MongoDB connection @@ -49,8 +48,6 @@ app.use('/api', questionRoutes); // Database routes app.use('/api', databaseRoutes); -app.use('/api', gptRoutes); - // Health check route app.get('/hello', (req, res) => { res.json({ message: 'Hello World' }); diff --git a/peerprep/frontend/src/api/assescodeApi.ts b/peerprep/frontend/src/api/assesscodeApi.ts similarity index 82% rename from peerprep/frontend/src/api/assescodeApi.ts rename to peerprep/frontend/src/api/assesscodeApi.ts index b44918cfbb..a7333726cf 100644 --- a/peerprep/frontend/src/api/assescodeApi.ts +++ b/peerprep/frontend/src/api/assesscodeApi.ts @@ -1,6 +1,6 @@ import axios, { AxiosError } from 'axios'; -const API_URL = 'http://localhost:8080/api/gpt/asses'; +const API_URL = 'http://localhost:1234/api/gpt/assess'; // Define a custom error class for API errors export class ApiError extends Error { @@ -25,16 +25,16 @@ const handleApiError = (error: unknown): never => { } }; -export const assesCode = async (currentCode: string): Promise => { +export const assessCode = async (currentCode: string): Promise => { try { console.log('Submitting code to backend API:'); // Call the backend API instead of OpenAI directly - const response = await axios.post(API_URL, { currentCode }); + const response = await axios.post(API_URL, { codeDetails: currentCode }); // Extract and display the response content from the backend const feedback = response.data.feedback; - //console.log('Backend API response:', feedback); + console.log('Backend API response:', feedback); return feedback; } catch (error) { console.error('Error executing backend API call:', error); diff --git a/peerprep/frontend/src/views/CollabServiceViews/CollabServiceIntegratedView.tsx b/peerprep/frontend/src/views/CollabServiceViews/CollabServiceIntegratedView.tsx index 20914d3597..0addd75b44 100644 --- a/peerprep/frontend/src/views/CollabServiceViews/CollabServiceIntegratedView.tsx +++ b/peerprep/frontend/src/views/CollabServiceViews/CollabServiceIntegratedView.tsx @@ -16,7 +16,7 @@ import 'codemirror/mode/clike/clike'; // For C, C++, Java (these use the 'clike' import 'codemirror/mode/python/python'; // For Python import 'codemirror/mode/swift/swift'; // For Swift -import { assesCode } from '../../api/assescodeApi.ts'; +import { assessCode } from '../../api/assesscodeApi.ts'; // @ts-check import { CodemirrorBinding } from 'y-codemirror'; @@ -187,7 +187,7 @@ const CollaborationServiceIntegratedView: React.FC = () => { } }; - const handleAssesCode = async () => { + const handleAssessCode = async () => { try { if (!yText) { console.error('Error: Yjs text instance is not available'); @@ -199,8 +199,9 @@ const CollaborationServiceIntegratedView: React.FC = () => { const questionInput = "1: Question - " + questionTitle + "\n" + "2: Description" + questionDescription + "\n"; const codeAttempt = "3: Code attempt in - " + syntaxFullLang + "\n" + currentCode; const inputString = questionInput + codeAttempt; - const responseContent = await assesCode(inputString); + const responseContent = await assessCode(inputString); //setCommentOutput(responseContent); + console.log(responseContent) setOutput(responseContent) } catch (error) { console.error('Error executing OpenAI API call:', error); @@ -253,7 +254,7 @@ const CollaborationServiceIntegratedView: React.FC = () => { > Run Code