Get transcript summaries of Web videos.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

85 lignes
2.2 KiB

  1. import fetchPonyfill from 'fetch-ponyfill';
  2. import Handlebars from 'handlebars';
  3. import { resolve } from 'path';
  4. import { readFile } from 'fs/promises';
  5. const makeAiCall = async (prompts: string[], apiKey: string, organizationId?: string): Promise<string> => {
  6. const headers: Record<string, string> = {
  7. 'Content-Type': 'application/json',
  8. Accept: 'application/json',
  9. Authorization: `Bearer ${apiKey}`,
  10. };
  11. if (organizationId) {
  12. headers['OpenAI-Organization'] = organizationId;
  13. }
  14. const { fetch } = fetchPonyfill();
  15. const response = await fetch(
  16. new URL('/v1/chat/completions', 'https://api.openai.com'),
  17. {
  18. method: 'POST',
  19. headers,
  20. body: JSON.stringify({
  21. //model: 'gpt-4',
  22. model: 'gpt-3.5-turbo',
  23. temperature: 0.6,
  24. messages: [
  25. {
  26. role: 'user',
  27. content: prompts[Math.floor(Math.random() * prompts.length)].trim(),
  28. },
  29. ],
  30. }),
  31. },
  32. );
  33. if (!response.ok) {
  34. const responseText = await response.text();
  35. console.log(responseText);
  36. throw new Error(`OpenAI API call failed with status ${response.status}`);
  37. }
  38. const { choices } = await response.json();
  39. // should we use all the response choices?
  40. return choices[0].message.content;
  41. };
  42. const compilePrompts = async (filename: string, params: Record<string, unknown>): Promise<string[]> => {
  43. const rawPromptText = await readFile(resolve(__dirname, filename), 'utf-8');
  44. const fill = Handlebars.compile(rawPromptText, { noEscape: true });
  45. const filledText = fill(params);
  46. return filledText.split('---').map((s) => s.trim());
  47. };
  48. export const normalizeTranscriptText = async (
  49. rawTranscriptText: string,
  50. apiKey: string,
  51. organizationId?: string,
  52. ) => {
  53. const prompts = await compilePrompts(
  54. '../prompts/normalize-transcript-text.hbs',
  55. {
  56. transcript: rawTranscriptText,
  57. },
  58. );
  59. return makeAiCall(prompts, apiKey, organizationId);
  60. };
  61. export const summarizeTranscript = async (
  62. transcript: string,
  63. apiKey: string,
  64. organizationId?: string,
  65. ) => {
  66. const prompts = await compilePrompts(
  67. '../prompts/summarize-transcript.hbs',
  68. {
  69. transcript,
  70. },
  71. );
  72. return makeAiCall(prompts, apiKey, organizationId);
  73. };