aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/gpt/GPT.ts
blob: 30194f9f8201143d18336ecc3b84b4d1cb7efb45 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { ClientOptions, OpenAI } from 'openai';
import { ChatCompletionMessageParam } from 'openai/resources';

enum GPTCallType {
    SUMMARY = 'summary',
    COMPLETION = 'completion',
    EDIT = 'edit',
    DATA = 'data',
}

type GPTCallOpts = {
    model: string;
    maxTokens: number;
    temp: number;
    prompt: string;
};

/**
 * Replace completions (deprecated) with chat
 */

const callTypeMap: { [type: string]: GPTCallOpts } = {
    // newest model: gpt-4
    summary: { model: 'gpt-3.5-turbo', maxTokens: 256, temp: 0.5, prompt: 'Summarize the text given in simpler terms.' },
    edit: { model: 'gpt-3.5-turbo', maxTokens: 256, temp: 0.5, prompt: 'Reword the text.' },
    completion: { model: 'gpt-3.5-turbo', maxTokens: 256, temp: 0.5, prompt: "You are a helpful assistant. Answer the user's prompt." },
    data: { model: 'gpt-3.5-turbo', maxTokens: 256, temp: 0.5, prompt: "You are a helpful resarch assistant. Analyze the user's data to find meaningful patterns and/or correlation. Please keep your response short and to the point." },
};

/**
 * Calls the OpenAI API.
 *
 * @param inputText Text to process
 * @returns AI Output
 */
const gptAPICall = async (inputText: string, callType: GPTCallType, prompt?: any) => {
    if (callType === GPTCallType.SUMMARY) inputText += '.';
    const opts: GPTCallOpts = callTypeMap[callType];
    try {
        const configuration: ClientOptions = {
            apiKey: process.env.OPENAI_KEY,
            dangerouslyAllowBrowser: true,
        };
        const openai = new OpenAI(configuration);

        let usePrompt = prompt ? opts.prompt + prompt : opts.prompt;
        let messages: ChatCompletionMessageParam[] = [
            { role: 'system', content: usePrompt },
            { role: 'user', content: inputText },
        ];

        const response = await openai.chat.completions.create({
            model: opts.model,
            max_tokens: opts.maxTokens,
            temperature: opts.temp,
            messages,
        });
        const content = response.choices[0].message.content;
        return content;
    } catch (err) {
        console.log(err);
        return 'Error connecting with API.';
    }
};

const gptImageCall = async (prompt: string, n?: number) => {
    try {
        const configuration: ClientOptions = {
            apiKey: process.env.OPENAI_KEY,
            dangerouslyAllowBrowser: true,
        };

        const openai = new OpenAI(configuration);
        const response = await openai.images.generate({
            prompt: prompt,
            n: n ?? 1,
            size: '1024x1024',
        });
        return response.data.map(data => data.url);
    } catch (err) {
        console.error(err);
        return;
    }
};

export { gptAPICall, gptImageCall, GPTCallType };