Skip to content

Instantly share code, notes, and snippets.

@abhiaiyer91
Created May 2, 2023 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhiaiyer91/fc87c9665d394d1793ce3daf3f68036f to your computer and use it in GitHub Desktop.
Save abhiaiyer91/fc87c9665d394d1793ce3daf3f68036f to your computer and use it in GitHub Desktop.
import fs from "fs";
import { OpenAI } from "langchain/llms/openai";
import { loadSummarizationChain } from "langchain/chains";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { PromptTemplate } from "langchain/prompts";
const categories = [
"News",
"Business",
"Technology",
"Health",
"Lifestyle",
"Education",
"Science",
"Sports",
"Entertainment",
"Other",
];
const promptBuilder = (text: string, style: string, wordCount: number = 500) => {
const introPrompt = `Please summarize the following text. The summary should be in the format of an episode of a podcast.`;
const stylePrompt = `Imagine you are an expert podcast host known for your ${style} style.`;
const lengthPrompt = `The content should be a maximum of ${wordCount} words long. The length should be based on the complexity and length of the original text. A small amount of text should receive a short summary while a larger amount of text should recieve a longer summary.`;
const negativePrompt = `Only summarize the content provided, do not make up or bring in external information unless it is to add commentary based on your personal conversational style. Do not mention the word 'Podcast'. Do not mention the word 'Episode'.`;
let categoriesPrompt = `We have the following categories: ${categories.slice(0, -1).join(", ")}${
categories.length > 1 ? ", and " : ""
}${categories[categories.length - 1]}`;
categoriesPrompt = `${categoriesPrompt} Give it category that fits the best in format of [CategoryStart: **CATEGORY HERE** :CategoryEnd].`;
const titlePrompt = `Give the podcast a title that is relevant in format of [TitleStart: **TITLE HERE** :TitleEnd].`;
const metadataPrompt = `As an example a category of 'Sports' and title of 'My Title' would show up like [CategoryStart: Sports :CategoryEnd]\n[TitleStart: My Title :TitleEnd] seperated with a new line character. Only include exactly one category and one title.`;
return `${introPrompt} ${stylePrompt} ${lengthPrompt} ${negativePrompt} ${categoriesPrompt} ${titlePrompt} ${metadataPrompt} {"text"}`;
};
async function createSummaryText(text: string) {
const model = new OpenAI({ temperature: 0, openAIApiKey: `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` });
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1400 });
const docs = await textSplitter.createDocuments([text]);
const introPrompt = `Please summarize the following text. The summary should be in the format of an episode of a podcast.`;
const stylePrompt = `Imagine you are an expert podcast host known for your funny style.`;
const lengthPrompt = `The content should be a maximum of 500 words long. The length should be based on the complexity and length of the original text. A small amount of text should receive a short summary while a larger amount of text should recieve a longer summary.`;
const negativePrompt = `Only summarize the content provided, do not make up or bring in external information unless it is to add commentary based on your personal conversational style. Do not mention the word 'Podcast'. Do not mention the word 'Episode'.`;
const template = `${introPrompt}${stylePrompt} ${negativePrompt} {text}`;
const promptTemp = new PromptTemplate({
template,
inputVariables: ["text"],
});
console.log(promptTemp, promptTemp.template?.length);
const chain = loadSummarizationChain(model, {
type: `map_reduce`,
combinePrompt: promptTemp,
combineMapPrompt: new PromptTemplate({
template: `
${introPrompt}${negativePrompt} {text}
`,
inputVariables: ["text"],
}),
});
console.log(text.length, "Text length");
console.log(docs.length, "Total Chunks");
console.time("Chain Call");
const res = await chain.call({ input_documents: docs });
console.timeEnd("Chain Call");
console.log(res);
return `suh`;
}
async function main() {
const text = fs.readFileSync(process.cwd() + "/__test__/sou.txt", "utf8");
console.log(await createSummaryText(text));
}
main().catch((e) => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment