got rid of the excess whitespaces and added the maxOutputTokens into the GenerateConfig in the types.ts

This commit is contained in:
Ivan Rudik
2025-11-09 05:42:16 +00:00
parent 3686ae72da
commit 73668377bf
16 changed files with 366 additions and 371 deletions
+11 -11
View File
@@ -5,8 +5,8 @@ import type { NextApiRequest, NextApiResponse } from 'next';
// IMPORTANT: The GEMINI_API_KEY should be an environment variable.
const apiKey = process.env.GEMINI_API_KEY;
if(!apiKey){
throw new Error("api key not found");
if (!apiKey) {
throw new Error("api key not found");
}
const ai = new GoogleGenerativeAI(apiKey);
@@ -24,7 +24,7 @@ export default async function handler(
// Destructure the data sent from the frontend
let { prompt, settings } = req.body;
// Basic validation
if (!prompt || typeof prompt !== 'string') {
return res.status(400).json({ error: 'Prompt is required' });
@@ -37,32 +37,32 @@ export default async function handler(
try {
// 1. Call the Gemini API using the correct method signature
const model = settings?.model || 'gemini-2.5-flash-lite';
const generativeModel = ai.getGenerativeModel({ model });
// 1. Define the generation configuration object
// 1. Define the generation configuration object
const generationConfig = {
temperature: settings?.temperature || 0.9,
// The SDK property name for system instructions is 'systemInstruction'
systemInstruction: settings?.sysTemInstructions,
systemInstruction: settings?.sysTemInstructions,
};
// 2. Call generateContent with the single GenerateContentRequest object.
const result = await generativeModel.generateContent({
// contents must be an array of Content objects
contents: [{
role: "user",
role: "user",
parts: [{ text: prompt }]
}],
}],
// The fixed property name that resolves the TypeScript error
generationConfig: generationConfig,
generationConfig: generationConfig,
});
// 2. Extract the response text
// The result object now contains a property 'text' at the top level for simple text generation
const geminiResponseText = result.response.text(); res.status(200).json({ response: geminiResponseText });
const geminiResponseText = result.response.text(); res.status(200).json({ response: geminiResponseText });
} catch (error) {
console.error("Gemini API Error:", error);