@@ -6,14 +6,22 @@ import * as openai from './openai/index.js';
66import * as transcripts from './transcripts/index.js' ;
77import log from './log.js' ;
88
9- function getApi ( ) {
10- if ( config . use . GOOGLE ) {
9+ function getApi ( api ?: 'google' | 'hf' | 'openai' ) {
10+ if ( typeof api === 'string' ) {
11+ api = api . toLowerCase ( ) as 'google' | 'hf' | 'openai' ;
12+ }
13+
14+ const useGoogle = api === 'google' || ( ! api && config . use . GOOGLE ) ;
15+ const useHf = api === 'hf' || ( ! api && config . use . HF ) ;
16+ const useOpenai = api === 'openai' || ( ! api && config . use . OPENAI ) ;
17+
18+ if ( useGoogle ) {
1119 return {
1220 summarize : google . summarize ,
1321 store : google . store ,
1422 search : google . search ,
1523 } ;
16- } else if ( config . use . HF ) {
24+ } else if ( useHf ) {
1725 return {
1826 summarize : hf . summarize ,
1927 store : hf . store ,
@@ -28,22 +36,56 @@ function getApi() {
2836 }
2937}
3038
31- const { summarize , store , search } = getApi ( ) ;
39+ const defaultApi = getApi ( ) ;
3240
3341const router = express . Router ( ) ;
3442
43+ async function loadVideos ( {
44+ api,
45+ videos,
46+ } : {
47+ api : typeof defaultApi ;
48+ videos : string [ ] ;
49+ } ) {
50+ log . debug ( 'Loading videos...' , {
51+ location : 'router.videos.load' ,
52+ videos,
53+ } ) ;
54+
55+ const documents = await transcripts . load ( videos ) ;
56+ const summaries = await api . summarize . docs ( documents ) ;
57+ await api . store ( summaries ) ;
58+ }
59+
60+ async function search ( {
61+ api,
62+ question,
63+ } : {
64+ api : typeof defaultApi ;
65+ question : string ;
66+ } ) {
67+ const results = await api . search ( question ) ;
68+
69+ log . info ( 'Results for question' , {
70+ question,
71+ results,
72+ location : 'router.videos.search' ,
73+ } ) ;
74+
75+ return results ;
76+ }
77+
3578router . post ( '/videos' , async ( req , res ) => {
3679 const { videos } = req . body as { videos : string [ ] } ;
80+ const useApi = req . header [ 'x-use-api' ] as
81+ | 'google'
82+ | 'hf'
83+ | 'openai'
84+ | undefined ;
85+ const api = getApi ( useApi ) ;
3786
3887 try {
39- log . debug ( 'Loading videos...' , {
40- location : 'router.videos.load' ,
41- videos,
42- } ) ;
43-
44- const documents = await transcripts . load ( videos ) ;
45- const summaries = await summarize . docs ( documents ) ;
46- await store ( summaries ) ;
88+ await loadVideos ( { api, videos } ) ;
4789
4890 res . status ( 200 ) . json ( { message : 'Videos loaded' } ) ;
4991 } catch ( e ) {
@@ -52,7 +94,7 @@ router.post('/videos', async (req, res) => {
5294 message : ( e as Error ) . message ,
5395 stack : ( e as Error ) . stack ,
5496 } ,
55- location : 'router.videos.load' ,
97+ location : 'router.videos.google. load' ,
5698 } ) ;
5799
58100 res . status ( 500 ) . json ( { error : ( e as Error ) . message } ) ;
@@ -61,15 +103,15 @@ router.post('/videos', async (req, res) => {
61103
62104router . get ( '/videos/search' , async ( req , res ) => {
63105 const { question } = req . query as { question : string } ;
106+ const useApi = req . header [ 'x-use-api' ] as
107+ | 'google'
108+ | 'hf'
109+ | 'openai'
110+ | undefined ;
111+ const api = getApi ( useApi ) ;
64112
65113 try {
66- const results = await search ( question ) ;
67-
68- log . info ( 'Results for question' , {
69- question,
70- results,
71- location : 'router.videos.search' ,
72- } ) ;
114+ const results = await search ( { api, question } ) ;
73115
74116 res . status ( 200 ) . json ( { results } ) ;
75117 } catch ( e ) {
0 commit comments