|
- import fetchPonyfill from 'fetch-ponyfill';
- import Handlebars from 'handlebars';
- import { resolve } from 'path';
- import { readFile } from 'fs/promises';
-
- const makeAiCall = async (prompts: string[], apiKey: string, organizationId?: string): Promise<string> => {
- const headers: Record<string, string> = {
- 'Content-Type': 'application/json',
- Accept: 'application/json',
- Authorization: `Bearer ${apiKey}`,
- };
-
- if (organizationId) {
- headers['OpenAI-Organization'] = organizationId;
- }
-
- const { fetch } = fetchPonyfill();
- const response = await fetch(
- new URL('/v1/chat/completions', 'https://api.openai.com'),
- {
- method: 'POST',
- headers,
- body: JSON.stringify({
- //model: 'gpt-4',
- model: 'gpt-3.5-turbo',
- temperature: 0.6,
- messages: [
- {
- role: 'user',
- content: prompts[Math.floor(Math.random() * prompts.length)].trim(),
- },
- ],
- }),
- },
- );
-
- if (!response.ok) {
- const responseText = await response.text();
- console.log(responseText);
- throw new Error(`OpenAI API call failed with status ${response.status}`);
- }
-
- const { choices } = await response.json();
-
- // should we use all the response choices?
- return choices[0].message.content;
- };
-
- const compilePrompts = async (filename: string, params: Record<string, unknown>): Promise<string[]> => {
- const rawPromptText = await readFile(resolve(__dirname, filename), 'utf-8');
- const fill = Handlebars.compile(rawPromptText, { noEscape: true });
- const filledText = fill(params);
- return filledText.split('---').map((s) => s.trim());
- };
-
- export const normalizeTranscriptText = async (
- rawTranscriptText: string,
- apiKey: string,
- organizationId?: string,
- ) => {
- const prompts = await compilePrompts(
- '../prompts/normalize-transcript-text.hbs',
- {
- transcript: rawTranscriptText,
- },
- );
-
- return makeAiCall(prompts, apiKey, organizationId);
- };
-
- export const summarizeTranscript = async (
- transcript: string,
- apiKey: string,
- organizationId?: string,
- ) => {
- const prompts = await compilePrompts(
- '../prompts/summarize-transcript.hbs',
- {
- transcript,
- },
- );
-
- return makeAiCall(prompts, apiKey, organizationId);
- };
|