From 6d61b8324606539e3b5e78396e95b8dc7c5dd3f2 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Wed, 12 Apr 2023 16:25:36 +0800 Subject: [PATCH] Integrate core's updated events API Ensure that the summarization is using the correct specs from the Core module. --- .env.example | 3 +++ .gitignore | 1 + src/config.ts | 2 +- src/modules/summary/SummaryController.ts | 28 ++++++++++++++++-------- src/modules/summary/SummaryService.ts | 19 ++++++---------- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/.env.example b/.env.example index 775a8f0..90811df 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,6 @@ OPENAI_API_KEY= # OpenAI organization ID. OPENAI_ORGANIZATION_ID= + +# Directory where the prompts are stored. +OPENAI_PROMPTS_DIR= diff --git a/.gitignore b/.gitignore index 997d5f4..c06a8a3 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,4 @@ dist .npmrc .idea/ +/types/ diff --git a/src/config.ts b/src/config.ts index d49858d..29cdeb5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,7 +3,7 @@ export namespace meta { 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 organizationId = process.env.OPENAI_ORGANIZATION_ID; } diff --git a/src/modules/summary/SummaryController.ts b/src/modules/summary/SummaryController.ts index 439b010..796f717 100644 --- a/src/modules/summary/SummaryController.ts +++ b/src/modules/summary/SummaryController.ts @@ -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 {CreateSummarizerParams} from '@modal-sh/webvideo-transcript-summary-core'; +import * as config from '../../config'; +import { SummaryService, SummaryServiceImpl } from './SummaryService'; export interface SummaryController { summarizeVideoTranscript: RouteHandlerMethod; @@ -9,10 +9,7 @@ export interface SummaryController { export class SummaryControllerImpl implements SummaryController { constructor( - private readonly summaryService: SummaryService = new SummaryServiceImpl( - config.openai.apiKey, - config.openai.organizationId, - ), + private readonly summaryService: SummaryService = new SummaryServiceImpl(), ) { // noop } @@ -20,9 +17,22 @@ export class SummaryControllerImpl implements SummaryController { readonly summarizeVideoTranscript: RouteHandlerMethod = async (request, reply) => { const params = request.body as CreateSummarizerParams; 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); - } catch { + } catch (errRaw) { + const err = errRaw as Error; + request.server.log.error(err); reply .code(500) .send(); diff --git a/src/modules/summary/SummaryService.ts b/src/modules/summary/SummaryService.ts index 8ceae27..489e2b0 100644 --- a/src/modules/summary/SummaryService.ts +++ b/src/modules/summary/SummaryService.ts @@ -15,30 +15,23 @@ export interface SummaryService { } export class SummaryServiceImpl implements SummaryService { - constructor( - private readonly openAiApiKey: string, - private readonly openAiOrganizationId?: string, - ) { + constructor(private readonly logger = console) { // noop } summarizeVideoTranscript(params: CreateSummarizerParams) { return new Promise>((resolve, reject) => { - let successEvent = {} as Partial; + const successEvent = {} as Partial; 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) => { + this.logger.log('process', data); if (data.phase === 'success') { switch (data.processType) { case 'fetch-transcript': successEvent.rawTranscript = ( - JSON.parse(data.content) as { text: string }[] + JSON.parse(data.content as string) as { text: string }[] ) .map((item) => item.text).join(' '); break; @@ -55,10 +48,12 @@ export class SummaryServiceImpl implements SummaryService { }); summarizer.on('error', (err) => { + this.logger.log('error', err); error = err; }); summarizer.on('end', () => { + this.logger.log('end'); if (error) { reject(error); return;