UNPKG

@rolme/ytscript

Version:

A CLI tool to download YouTube transcripts and generate summaries

65 lines (63 loc) 2.23 kB
import { AIError } from '../../types/ai.js'; export class ChatGPTProvider { name = 'chatgpt'; apiKey; constructor(apiKey) { if (!apiKey) { throw new Error('OpenAI API key is required'); } this.apiKey = apiKey; } async summarize(transcript, options = {}) { try { const prompt = this.buildPrompt(transcript, options); const result = await this.callOpenAI(prompt); if (!result?.choices?.[0]?.message?.content) { throw new Error('Invalid response from OpenAI API'); } return result.choices[0].message.content; } catch (error) { throw new AIError(error instanceof Error ? error.message : 'Failed to generate summary'); } } buildPrompt(transcript, options) { const style = options.style || 'concise'; const maxLength = options.maxLength ? `Keep the summary under ${options.maxLength} characters.` : ''; return ` Summarize the following transcript in a ${style} manner. ${maxLength} Focus on the key points and main ideas. Transcript: ${transcript} `.trim(); } async callOpenAI(prompt) { const response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ model: 'gpt-4-turbo-preview', messages: [ { role: 'system', content: 'You are a helpful assistant that summarizes video transcripts accurately and concisely.' }, { role: 'user', content: prompt } ], temperature: 0.3, max_tokens: 500 }), }); if (!response.ok) { throw new Error(`OpenAI API request failed: ${response.statusText}`); } const data = await response.json(); return data; } }