Bläddra i källkod

Adjust consumption of core module

Use improved API of core module.
master
TheoryOfNekomata 1 år sedan
förälder
incheckning
cacbf54279
2 ändrade filer med 31 tillägg och 15 borttagningar
  1. +11
    -6
      src/modules/summary/SummaryController.ts
  2. +20
    -9
      src/modules/summary/SummaryService.ts

+ 11
- 6
src/modules/summary/SummaryController.ts Visa fil

@@ -1,5 +1,5 @@
import { RouteHandlerMethod } from 'fastify';
import {CreateSummarizerParams} from '@modal-sh/webvideo-transcript-summary-core';
import {CreateSummarizerParams, SummarizerProcessParams} from '@modal-sh/webvideo-transcript-summary-core';
import * as config from '../../config';
import { SummaryService, SummaryServiceImpl } from './SummaryService';

@@ -15,10 +15,9 @@ export class SummaryControllerImpl implements SummaryController {
}

readonly summarizeVideoTranscript: RouteHandlerMethod = async (request, reply) => {
const params = request.body as CreateSummarizerParams;
const params = request.body as CreateSummarizerParams & SummarizerProcessParams;
try {
const summaryResult = await this.summaryService.summarizeVideoTranscript({
url: params.url,
this.summaryService.initializeSummarizer({
type: params.type,
openAiParams: {
apiKey: config.openAi.apiKey,
@@ -26,9 +25,15 @@ export class SummaryControllerImpl implements SummaryController {
temperature: params.openAiParams?.temperature ?? 0.6,
model: params.openAiParams?.model ?? 'gpt-3.5-turbo',
},
language: params.language ?? 'en',
country: params.country ?? 'US',
});

const summaryResult = await this.summaryService.summarizeVideoTranscript(
{
url: params.url,
language: params.language ?? 'en',
country: params.country ?? 'US',
},
);
reply.send(summaryResult);
} catch (errRaw) {
const err = errRaw as Error;


+ 20
- 9
src/modules/summary/SummaryService.ts Visa fil

@@ -1,7 +1,7 @@
import {
createSummarizer,
CreateSummarizerParams,
VideoType,
CreateSummarizerParams, SummarizerEventEmitter,
SummarizerProcessParams,
} from '@modal-sh/webvideo-transcript-summary-core';

export interface SummaryResult {
@@ -11,21 +11,32 @@ export interface SummaryResult {
}

export interface SummaryService {
summarizeVideoTranscript(params: CreateSummarizerParams): Promise<Partial<SummaryResult>>
initializeSummarizer(params: CreateSummarizerParams): void;
summarizeVideoTranscript(processParams: SummarizerProcessParams): Promise<Partial<SummaryResult>>;
}

export class SummaryServiceImpl implements SummaryService {
private summarizer?: SummarizerEventEmitter;

constructor(private readonly logger = console) {
// noop
}

summarizeVideoTranscript(params: CreateSummarizerParams) {
initializeSummarizer(params: CreateSummarizerParams): void {
this.summarizer = createSummarizer(params);
}

summarizeVideoTranscript(processParams: SummarizerProcessParams) {
return new Promise<Partial<SummaryResult>>((resolve, reject) => {
if (!this.summarizer) {
reject(new Error('Summarizer is not initialized'));
return;
}

const successEvent = {} as Partial<SummaryResult>;
let error: Error;
const summarizer = createSummarizer(params);

summarizer.on('process', (data) => {
this.summarizer.on('process', (data) => {
this.logger.log('process', data);
if (data.phase === 'success') {
switch (data.processType) {
@@ -47,12 +58,12 @@ export class SummaryServiceImpl implements SummaryService {
}
});

summarizer.on('error', (err) => {
this.summarizer.on('error', (err) => {
this.logger.log('error', err);
error = err;
});

summarizer.on('end', () => {
this.summarizer.on('end', () => {
this.logger.log('end');
if (error) {
reject(error);
@@ -61,7 +72,7 @@ export class SummaryServiceImpl implements SummaryService {
resolve(successEvent);
});

summarizer.process();
this.summarizer.process(processParams);
});
}
}

Laddar…
Avbryt
Spara