@aituber-onair/core
Version:
Core library for AITuber OnAir providing voice synthesis and chat processing
75 lines • 3.17 kB
JavaScript
import { ENDPOINT_GEMINI_API, MODEL_GEMINI_2_0_FLASH_LITE, DEFAULT_SUMMARY_PROMPT_TEMPLATE, } from '@aituber-onair/chat';
import { createSummaryContext, summarizeWithFallback, } from '../summarizerUtils';
/**
* Implementation of summarization functionality using Gemini
*/
export class GeminiSummarizer {
/**
* Constructor
* @param apiKey Google API key
* @param model Name of the model to use
* @param defaultPromptTemplate Default prompt template for summarization
*/
constructor(apiKey, model = MODEL_GEMINI_2_0_FLASH_LITE, defaultPromptTemplate = DEFAULT_SUMMARY_PROMPT_TEMPLATE) {
this.apiKey = apiKey;
this.model = model;
this.defaultPromptTemplate = defaultPromptTemplate;
}
/**
* Summarize chat messages
* @param messages Array of messages to summarize
* @param maxLength Maximum number of characters (default 256)
* @param customPrompt Custom prompt template for summarization (optional)
* @returns Summarized text
*/
async summarize(messages, maxLength = 256, customPrompt) {
const { systemPrompt, conversationText } = createSummaryContext(messages, maxLength, this.defaultPromptTemplate, customPrompt);
return summarizeWithFallback(messages, async () => {
// Create the endpoint URL with API key
const apiUrl = `${ENDPOINT_GEMINI_API}/models/${this.model}:generateContent?key=${this.apiKey}`;
// API request
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: [
{
role: 'user',
parts: [
{
text: systemPrompt,
},
{
text: conversationText,
},
],
},
],
generationConfig: {
temperature: 0.2,
topK: 40,
topP: 0.95,
maxOutputTokens: maxLength,
},
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`Gemini API error: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
// Extract response text from Gemini's response format
if (data.candidates &&
data.candidates.length > 0 &&
data.candidates[0].content &&
data.candidates[0].content.parts &&
data.candidates[0].content.parts.length > 0) {
return data.candidates[0].content.parts[0].text || '';
}
return '';
});
}
}
//# sourceMappingURL=GeminiSummarizer.js.map