Pārlūkot izejas kodu

Integrate core's updated events API

Ensure that the summarization is using the correct specs from the Core
module.
master
TheoryOfNekomata pirms 1 gada
vecāks
revīzija
6d61b83246
5 mainītis faili ar 31 papildinājumiem un 22 dzēšanām
  1. +3
    -0
      .env.example
  2. +1
    -0
      .gitignore
  3. +1
    -1
      src/config.ts
  4. +19
    -9
      src/modules/summary/SummaryController.ts
  5. +7
    -12
      src/modules/summary/SummaryService.ts

+ 3
- 0
.env.example Parādīt failu

@@ -3,3 +3,6 @@ OPENAI_API_KEY=

# OpenAI organization ID.
OPENAI_ORGANIZATION_ID=

# Directory where the prompts are stored.
OPENAI_PROMPTS_DIR=

+ 1
- 0
.gitignore Parādīt failu

@@ -106,3 +106,4 @@ dist

.npmrc
.idea/
/types/

+ 1
- 1
src/config.ts Parādīt failu

@@ -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;
}

+ 19
- 9
src/modules/summary/SummaryController.ts Parādīt failu

@@ -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();


+ 7
- 12
src/modules/summary/SummaryService.ts Parādīt failu

@@ -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<Partial<SummaryResult>>((resolve, reject) => {
let successEvent = {} as Partial<SummaryResult>;
const successEvent = {} as Partial<SummaryResult>;
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;


Notiek ielāde…
Atcelt
Saglabāt