|
| 1 | +import fetchRmp from '@/modules/fetchRmp'; |
| 2 | +import type { SearchQuery } from '@/types/SearchQuery'; |
| 3 | +import { GoogleGenAI } from '@google/genai'; |
| 4 | +import { NextResponse } from 'next/server'; |
| 5 | + |
| 6 | +export async function GET(request: Request) { |
| 7 | + const API_URL = process.env.NEBULA_API_URL; |
| 8 | + if (typeof API_URL !== 'string') { |
| 9 | + return NextResponse.json( |
| 10 | + { message: 'error', data: 'API URL is undefined' }, |
| 11 | + { status: 500 }, |
| 12 | + ); |
| 13 | + } |
| 14 | + const API_KEY = process.env.NEBULA_API_KEY; |
| 15 | + if (typeof API_KEY !== 'string') { |
| 16 | + return NextResponse.json( |
| 17 | + { message: 'error', data: 'API key is undefined' }, |
| 18 | + { status: 500 }, |
| 19 | + ); |
| 20 | + } |
| 21 | + const API_STORAGE_BUCKET = process.env.NEBULA_API_STORAGE_BUCKET; |
| 22 | + if (typeof API_STORAGE_BUCKET !== 'string') { |
| 23 | + return NextResponse.json( |
| 24 | + { message: 'error', data: 'API storage bucket is undefined' }, |
| 25 | + { status: 500 }, |
| 26 | + ); |
| 27 | + } |
| 28 | + const API_STORAGE_KEY = process.env.NEBULA_API_STORAGE_KEY; |
| 29 | + if (typeof API_STORAGE_KEY !== 'string') { |
| 30 | + return NextResponse.json( |
| 31 | + { message: 'error', data: 'API storage key is undefined' }, |
| 32 | + { status: 500 }, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const { searchParams } = new URL(request.url); |
| 37 | + const profFirst = searchParams.get('profFirst'); |
| 38 | + const profLast = searchParams.get('profLast'); |
| 39 | + if (typeof profFirst !== 'string' || typeof profLast !== 'string') { |
| 40 | + return NextResponse.json( |
| 41 | + { message: 'error', data: 'Incorrect query parameters' }, |
| 42 | + { status: 400 }, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + // Check cache |
| 47 | + const filename = profFirst + profLast + '.txt'; |
| 48 | + const url = API_URL + 'storage/' + API_STORAGE_BUCKET + '/' + filename; |
| 49 | + const headers = { |
| 50 | + 'x-api-key': API_KEY, |
| 51 | + 'x-storage-key': API_STORAGE_KEY, |
| 52 | + }; |
| 53 | + const cache = await fetch(url, { headers }); |
| 54 | + if (cache.ok) { |
| 55 | + const cacheData = await cache.json(); |
| 56 | + // Cache is valid for 30 days |
| 57 | + if ( |
| 58 | + new Date(cacheData.data.updated) > |
| 59 | + new Date(Date.now() - 1000 * 60 * 60 * 24 * 30) |
| 60 | + ) { |
| 61 | + const mediaData = await fetch(cacheData.data.media_link); |
| 62 | + if (mediaData.ok) { |
| 63 | + return NextResponse.json( |
| 64 | + { message: 'success', data: await mediaData.text() }, |
| 65 | + { status: 200 }, |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Fetch RMP |
| 72 | + const searchQuery: SearchQuery = { |
| 73 | + profFirst: profFirst, |
| 74 | + profLast: profLast, |
| 75 | + }; |
| 76 | + const rmp = await fetchRmp(searchQuery, true); |
| 77 | + |
| 78 | + if (!rmp?.ratings) { |
| 79 | + return NextResponse.json( |
| 80 | + { message: 'error', data: 'No ratings found' }, |
| 81 | + { status: 500 }, |
| 82 | + ); |
| 83 | + } |
| 84 | + if (rmp.ratings.edges.length < 5) { |
| 85 | + return NextResponse.json( |
| 86 | + { message: 'error', data: 'Not enough ratings for a summary' }, |
| 87 | + { status: 500 }, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + // AI |
| 92 | + const prompt = `Summarize the Rate My Professors reviews of professor ${profFirst} ${profLast}: |
| 93 | +
|
| 94 | +${rmp.ratings.edges.map((rating) => rating.node.comment.replaceAll('\n', ' ').slice(0, 500)).join('\n')} |
| 95 | +
|
| 96 | +Summary requirements: |
| 97 | +- Summarize the reviews in a concise and informative manner. |
| 98 | +- Focus on the structure of the class, exams, projects, homeworks, and assignments. |
| 99 | +- Be respectful but honest, like a student writing to a peer. |
| 100 | +- Respond in plain-text (no markdown), in 30 words. |
| 101 | +`; |
| 102 | + const GEMINI_SERVICE_ACCOUNT = process.env.GEMINI_SERVICE_ACCOUNT; |
| 103 | + if (typeof GEMINI_SERVICE_ACCOUNT !== 'string') { |
| 104 | + return NextResponse.json( |
| 105 | + { message: 'error', data: 'GEMINI_SERVICE_ACCOUNT is undefined' }, |
| 106 | + { status: 500 }, |
| 107 | + ); |
| 108 | + } |
| 109 | + const serviceAccount = JSON.parse(GEMINI_SERVICE_ACCOUNT); |
| 110 | + const geminiClient = new GoogleGenAI({ |
| 111 | + vertexai: true, |
| 112 | + project: serviceAccount.project_id, |
| 113 | + googleAuthOptions: { |
| 114 | + credentials: { |
| 115 | + client_email: serviceAccount.client_email, |
| 116 | + private_key: serviceAccount.private_key, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }); |
| 120 | + const response = await geminiClient.models.generateContent({ |
| 121 | + model: 'gemini-2.5-flash-lite', |
| 122 | + contents: prompt, |
| 123 | + }); |
| 124 | + |
| 125 | + // Cache response |
| 126 | + const cacheResponse = await fetch(url, { |
| 127 | + method: 'POST', |
| 128 | + headers: headers, |
| 129 | + body: response.text, |
| 130 | + }); |
| 131 | + |
| 132 | + if (!cacheResponse.ok) { |
| 133 | + return NextResponse.json( |
| 134 | + { message: 'error', data: 'Failed to cache response' }, |
| 135 | + { status: 500 }, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + // Return |
| 140 | + return NextResponse.json( |
| 141 | + { message: 'success', data: response.text }, |
| 142 | + { status: 200 }, |
| 143 | + ); |
| 144 | +} |
0 commit comments