aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/gpt/Summarization.ts
blob: c0dcc026719f5916e81dc5bc8b6d64cb92c99071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Configuration, OpenAIApi } from 'openai';

/**
 * Summarizes the inputted text with OpenAI.
 * 
 * @param text Text to summarize
 * @returns Summary of text
 */
const gptSummarize = async (text: string) => {
    text += '.';
    const model = 'text-curie-001'; // Most advanced: text-davinci-003
    const maxTokens = 200;
    const temp = 0.8; 

    try {
        const configuration = new Configuration({
            apiKey: process.env.OPENAI_KEY,
        });
        const openai = new OpenAIApi(configuration);
        const response = await openai.createCompletion({
            model: model,
            max_tokens: maxTokens,
            temperature: temp,
            prompt: `Summarize this text: ${text}`,
        });
        return response.data.choices[0].text;
    } catch (err) {
        console.log(err);
        return 'Error connecting with API.';
    }
};

export { gptSummarize };