@@ -8,7 +8,7 @@ import { joinYoutubeAndOpenPlannerData } from './utils/joinYoutubeAndOpenPlanner
88import 'dotenv/config'
99
1010const POLLING_INTERVAL = 5000 // 5 seconds
11- const CONCURRENT_JOBS = 5
11+ const CONCURRENT_JOBS = 1
1212
1313// This whole is here to generate subtitles for a youtube video
1414// using Gladia & ChatGPT. It won't upload subtitles to youtube
@@ -18,8 +18,8 @@ const CONCURRENT_JOBS = 5
1818// - Fill the .env file with the following variables:
1919// - GLADIA_API_KEY
2020// - GLADIA_MODEL (optional, defaults to solaria-3; use solaria-1 for 100+ languages)
21- // - OPENAI_API_KEY
22- // - OPENAI_MODEL (optional, defaults to gpt-4o-mini)
21+ // - OPENROUTER_API_KEY
22+ // - OPENROUTER_MODEL (optional, defaults to openai/ gpt-4o-mini)
2323// - YOUTUBE_PLAYLIST_ID
2424// - OPENPLANNER_EVENT_ID
2525// - Ensure you have youtube credentials for API in ~/.credentials/youtube.credentials.json
@@ -31,18 +31,21 @@ const CONCURRENT_JOBS = 5
3131// - If any SRT or keywords are already generated, they won't be recreated.
3232
3333const GLADIA_API_KEY = process . env . GLADIA_API_KEY
34- const OPENAI_API_KEY = process . env . OPENAI_API_KEY
34+ const OPENROUTER_API_KEY = process . env . OPENROUTER_API_KEY
3535
36- if ( ! GLADIA_API_KEY || ! OPENAI_API_KEY ) {
37- throw new Error ( 'GLADIA_API_KEY and OPENAI_API_KEY must be set' )
36+ if ( ! GLADIA_API_KEY || ! OPENROUTER_API_KEY ) {
37+ throw new Error ( 'GLADIA_API_KEY and OPENROUTER_API_KEY must be set' )
3838}
3939
40+ const OPENROUTER_ENDPOINT = 'https://openrouter.ai/api/v1/chat/completions'
41+
4042const GLADIA_TRANSCRIPTION_ENDPOINT = 'https://api.gladia.io/v2/pre-recorded'
4143// Gladia speech-to-text model. Defaults to solaria-3 (newest, tuned for European languages
4244// incl. French). Override with GLADIA_MODEL, e.g. solaria-1 for 100+ languages / code-switching.
4345const GLADIA_MODEL = process . env . GLADIA_MODEL || 'solaria-3'
44- // OpenAI model used to extract keywords. Override with OPENAI_MODEL.
45- const OPENAI_MODEL = process . env . OPENAI_MODEL || 'gpt-4o-mini'
46+ // OpenRouter model used to extract keywords. Override with OPENROUTER_MODEL
47+ // (any OpenRouter model slug, e.g. openai/gpt-4o-mini, anthropic/claude-3.5-haiku).
48+ const OPENROUTER_MODEL = process . env . OPENROUTER_MODEL || 'z-ai/glm-5.2'
4649
4750async function getTranscriptionIdFromGladia ( audioUrl , customVocabulary ) {
4851 const headers = {
@@ -128,31 +131,33 @@ async function generateKeywords(session) {
128131 let keywords = [ ]
129132 try {
130133 const response = await axios . post (
131- 'https://api.openai.com/v1/chat/completions' ,
134+ OPENROUTER_ENDPOINT ,
132135 {
133- model : OPENAI_MODEL ,
136+ model : OPENROUTER_MODEL ,
134137 messages : [
135138 { role : 'system' , content : 'You are a helpful assistant.' } ,
136139 { role : 'user' , content : prompt } ,
137140 ] ,
138- max_tokens : 500 ,
141+ max_tokens : 2000 ,
139142 temperature : 0 ,
140143 } ,
141144 {
142145 headers : {
143- Authorization : `Bearer ${ OPENAI_API_KEY } ` ,
146+ Authorization : `Bearer ${ OPENROUTER_API_KEY } ` ,
144147 'Content-Type' : 'application/json' ,
145148 } ,
146149 }
147150 )
148151
149- if ( response . data && response . data . choices && response . data . choices . length > 0 ) {
150- const rawKeywords = response . data . choices [ 0 ] . message . content
151- keywords = rawKeywords . replaceAll ( '`' , '' ) . replace ( 'json' , '' )
152- return JSON . parse ( keywords )
152+ const rawKeywords = response . data ?. choices ?. [ 0 ] ?. message ?. content
153+ if ( ! rawKeywords ) {
154+ throw new Error ( 'Empty content in model response' )
153155 }
154156
155- throw new Error ( 'No keywords found in response' )
157+ // Extract the JSON array from the response (models may wrap it in ```json fences or prose)
158+ const match = rawKeywords . match ( / \[ [ \s \S ] * \] / )
159+ keywords = JSON . parse ( match ? match [ 0 ] : rawKeywords . replaceAll ( '`' , '' ) . replace ( 'json' , '' ) )
160+ return keywords
156161 } catch ( error ) {
157162 console . error ( `❌ Error generating keywords for session: ${ session . title } ` , error . message , keywords )
158163 return keywords
0 commit comments