bilingual-summarizer
Version:
A powerful text summarization package for Arabic and English content with sentiment analysis and topic extraction
83 lines (80 loc) • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeWithGeminiAI = summarizeWithGeminiAI;
exports.isGeminiConfigValid = isGeminiConfigValid;
const generative_ai_1 = require("@google/generative-ai");
/**
* Generates a summary using Google's Gemini AI
* @param text The text to summarize
* @param sentenceCount The maximum number of sentences in the summary
* @param geminiConfig Configuration for the Gemini API
* @returns The AI-generated summary
*/
async function summarizeWithGeminiAI(text, sentenceCount = 5, geminiConfig) {
if (!geminiConfig.apiKey) {
throw new Error('Gemini API key is required. Get one from Google AI Studio (https://ai.google.dev/)');
}
try {
// Initialize the Google Generative AI with the provided API key
const genAI = new generative_ai_1.GoogleGenerativeAI(geminiConfig.apiKey);
// Get the model - default to gemini-1.5-flash if not specified
// Using newer model names based on the API error message
const model = genAI.getGenerativeModel({
model: geminiConfig.model || 'gemini-1.5-flash',
safetySettings: [
{
category: generative_ai_1.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: generative_ai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: generative_ai_1.HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: generative_ai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: generative_ai_1.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: generative_ai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: generative_ai_1.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: generative_ai_1.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
],
generationConfig: {
temperature: geminiConfig.temperature || 0.2,
maxOutputTokens: geminiConfig.maxOutputTokens || 1000,
},
});
// Determine language to create appropriate prompt
const isArabic = /[\u0600-\u06FF]/.test(text);
const lang = isArabic ? 'Arabic' : 'English';
// Use a consistent English prompt format for all languages
const prompt = `
Context:
You are a professional linguist in the ${lang} language. Your task is to create a brief summary of articles and posts in a paragraph containing no more than ${sentenceCount} complete sentences. The last sentence does not need to be fully completed.
Instructions:
Analyze the text carefully. Do not use bullet points or numbered lists. Provide a unique, complete summary as your answer, and ensure it is written in the ${lang} language.
Input:
The text to summarize is:
${text}`;
// Generate content using the model
const result = await model.generateContent(prompt);
const response = result.response;
// Get the summary text and ensure it ends with proper punctuation
const summaryText = response.text().trim();
return summaryText;
}
catch (error) {
console.error('Error using Gemini API for summarization:', error);
throw new Error(`Failed to summarize using Gemini AI: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Validates that a Gemini configuration is properly set up
* @param config The Gemini configuration to validate
* @returns True if the configuration is valid, false otherwise
*/
function isGeminiConfigValid(config) {
if (!config)
return false;
return Boolean(config.apiKey && config.apiKey.trim().length > 0);
}