Get transcript summaries of Web videos.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

summarizer.ts 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { OpenAi, createAiClient } from '@modal-sh/mio-ai';
  2. import { EventEmitter } from 'events';
  3. import { BaseTranscriptItem } from './transcript';
  4. export type DataEventCallback = (event: string) => void;
  5. export type ErrorEventCallback = (event: Error) => void;
  6. export interface SummarizerEventEmitter<T = unknown> extends NodeJS.EventEmitter {
  7. normalize(transcriptItems: T[]): Promise<string>;
  8. summarize(transcript: string): void;
  9. on(eventType: 'data', callback: DataEventCallback): this;
  10. on(eventType: 'error', callback: ErrorEventCallback): this;
  11. on(eventType: 'end', callback: () => void): this;
  12. }
  13. export class SummarizerEventEmitterImpl<T extends BaseTranscriptItem> extends EventEmitter {
  14. private readonly openAiClient: OpenAi.PlatformEventEmitter;
  15. constructor(params: OpenAi.Configuration) {
  16. super();
  17. this.openAiClient = createAiClient({
  18. platform: OpenAi.PLATFORM_ID,
  19. platformConfiguration: params,
  20. });
  21. }
  22. normalize(transcript: T[]) {
  23. return new Promise<string>((resolve, reject) => {
  24. this.openAiClient.once<OpenAi.CreateEditDataEvent>('data', (data) => {
  25. const normalizedTranscript = data.choices[0].text;
  26. resolve(normalizedTranscript);
  27. });
  28. this.openAiClient.once('error', (error) => {
  29. reject(error);
  30. });
  31. this.openAiClient.createEdit({
  32. input: transcript.map((item) => item.text).join(' '),
  33. instruction: 'Put proper punctuation and correct capitalization',
  34. model: OpenAi.EditModel.TEXT_DAVINCI_EDIT_001,
  35. });
  36. });
  37. }
  38. summarize(normalizedTranscript: string) {
  39. const listener = (data: OpenAi.ChatCompletionChunkDataEvent) => {
  40. const theContent = data.choices[0].delta.content;
  41. if (typeof theContent !== 'string') {
  42. return;
  43. }
  44. this.emit('data', theContent)
  45. };
  46. this.openAiClient.on<OpenAi.ChatCompletionChunkDataEvent>('data', listener);
  47. this.openAiClient.once('error', (error) => {
  48. this.openAiClient.off('data', listener);
  49. this.emit('error', error);
  50. });
  51. this.openAiClient.once('end', () => {
  52. this.openAiClient.off('data', listener);
  53. this.emit('end');
  54. });
  55. this.openAiClient.createChatCompletion({
  56. model: OpenAi.ChatCompletionModel.GPT_3_5_TURBO,
  57. messages: [
  58. {
  59. role: OpenAi.MessageRole.SYSTEM,
  60. content: 'You are working on video transcripts.',
  61. },
  62. {
  63. role: OpenAi.MessageRole.USER,
  64. content: `Summarize the following transcript:\n\n${normalizedTranscript}`,
  65. },
  66. ],
  67. });
  68. }
  69. }