Ensure that the summarization is using the correct specs from the Core module.master
@@ -3,3 +3,6 @@ OPENAI_API_KEY= | |||||
# OpenAI organization ID. | # OpenAI organization ID. | ||||
OPENAI_ORGANIZATION_ID= | OPENAI_ORGANIZATION_ID= | ||||
# Directory where the prompts are stored. | |||||
OPENAI_PROMPTS_DIR= |
@@ -106,3 +106,4 @@ dist | |||||
.npmrc | .npmrc | ||||
.idea/ | .idea/ | ||||
/types/ |
@@ -3,7 +3,7 @@ export namespace meta { | |||||
export const host = process.env.HOST ?? '0.0.0.0'; | export const host = process.env.HOST ?? '0.0.0.0'; | ||||
} | } | ||||
export namespace openai { | |||||
export namespace openAi { | |||||
export const apiKey = process.env.OPENAI_API_KEY as string; | export const apiKey = process.env.OPENAI_API_KEY as string; | ||||
export const organizationId = process.env.OPENAI_ORGANIZATION_ID; | export const organizationId = process.env.OPENAI_ORGANIZATION_ID; | ||||
} | } |
@@ -1,7 +1,7 @@ | |||||
import { CreateSummarizerParams } from '@modal-sh/webvideo-transcript-summary-core'; | |||||
import { SummaryService, SummaryServiceImpl } from './SummaryService'; | |||||
import * as config from '../../config'; | |||||
import { RouteHandlerMethod } from 'fastify'; | import { RouteHandlerMethod } from 'fastify'; | ||||
import {CreateSummarizerParams} from '@modal-sh/webvideo-transcript-summary-core'; | |||||
import * as config from '../../config'; | |||||
import { SummaryService, SummaryServiceImpl } from './SummaryService'; | |||||
export interface SummaryController { | export interface SummaryController { | ||||
summarizeVideoTranscript: RouteHandlerMethod; | summarizeVideoTranscript: RouteHandlerMethod; | ||||
@@ -9,10 +9,7 @@ export interface SummaryController { | |||||
export class SummaryControllerImpl implements SummaryController { | export class SummaryControllerImpl implements SummaryController { | ||||
constructor( | constructor( | ||||
private readonly summaryService: SummaryService = new SummaryServiceImpl( | |||||
config.openai.apiKey, | |||||
config.openai.organizationId, | |||||
), | |||||
private readonly summaryService: SummaryService = new SummaryServiceImpl(), | |||||
) { | ) { | ||||
// noop | // noop | ||||
} | } | ||||
@@ -20,9 +17,22 @@ export class SummaryControllerImpl implements SummaryController { | |||||
readonly summarizeVideoTranscript: RouteHandlerMethod = async (request, reply) => { | readonly summarizeVideoTranscript: RouteHandlerMethod = async (request, reply) => { | ||||
const params = request.body as CreateSummarizerParams; | const params = request.body as CreateSummarizerParams; | ||||
try { | try { | ||||
const summaryResult = await this.summaryService.summarizeVideoTranscript(params); | |||||
const summaryResult = await this.summaryService.summarizeVideoTranscript({ | |||||
url: params.url, | |||||
type: params.type, | |||||
openAiParams: { | |||||
apiKey: config.openAi.apiKey, | |||||
organizationId: config.openAi.organizationId, | |||||
temperature: params.openAiParams?.temperature ?? 0.6, | |||||
model: params.openAiParams?.model ?? 'gpt-3.5-turbo', | |||||
}, | |||||
language: params.language ?? 'en', | |||||
country: params.country ?? 'US', | |||||
}); | |||||
reply.send(summaryResult); | reply.send(summaryResult); | ||||
} catch { | |||||
} catch (errRaw) { | |||||
const err = errRaw as Error; | |||||
request.server.log.error(err); | |||||
reply | reply | ||||
.code(500) | .code(500) | ||||
.send(); | .send(); | ||||
@@ -15,30 +15,23 @@ export interface SummaryService { | |||||
} | } | ||||
export class SummaryServiceImpl implements SummaryService { | export class SummaryServiceImpl implements SummaryService { | ||||
constructor( | |||||
private readonly openAiApiKey: string, | |||||
private readonly openAiOrganizationId?: string, | |||||
) { | |||||
constructor(private readonly logger = console) { | |||||
// noop | // noop | ||||
} | } | ||||
summarizeVideoTranscript(params: CreateSummarizerParams) { | summarizeVideoTranscript(params: CreateSummarizerParams) { | ||||
return new Promise<Partial<SummaryResult>>((resolve, reject) => { | return new Promise<Partial<SummaryResult>>((resolve, reject) => { | ||||
let successEvent = {} as Partial<SummaryResult>; | |||||
const successEvent = {} as Partial<SummaryResult>; | |||||
let error: Error; | let error: Error; | ||||
const summarizer = createSummarizer({ | |||||
type: VideoType.YOUTUBE, | |||||
url: params.url, | |||||
openaiApiKey: this.openAiApiKey, | |||||
openaiOrganizationId: this.openAiOrganizationId, | |||||
}); | |||||
const summarizer = createSummarizer(params); | |||||
summarizer.on('process', (data) => { | summarizer.on('process', (data) => { | ||||
this.logger.log('process', data); | |||||
if (data.phase === 'success') { | if (data.phase === 'success') { | ||||
switch (data.processType) { | switch (data.processType) { | ||||
case 'fetch-transcript': | case 'fetch-transcript': | ||||
successEvent.rawTranscript = ( | successEvent.rawTranscript = ( | ||||
JSON.parse(data.content) as { text: string }[] | |||||
JSON.parse(data.content as string) as { text: string }[] | |||||
) | ) | ||||
.map((item) => item.text).join(' '); | .map((item) => item.text).join(' '); | ||||
break; | break; | ||||
@@ -55,10 +48,12 @@ export class SummaryServiceImpl implements SummaryService { | |||||
}); | }); | ||||
summarizer.on('error', (err) => { | summarizer.on('error', (err) => { | ||||
this.logger.log('error', err); | |||||
error = err; | error = err; | ||||
}); | }); | ||||
summarizer.on('end', () => { | summarizer.on('end', () => { | ||||
this.logger.log('end'); | |||||
if (error) { | if (error) { | ||||
reject(error); | reject(error); | ||||
return; | return; | ||||