aboutsummaryrefslogtreecommitdiff
path: root/src/client/apis/gpt/customization.ts
blob: 8960bc651f10a7f104c0cde2dfe0dcd23604da04 (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
import { ChatCompletionMessageParam } from 'openai/resources';
import { openai } from './setup';
import { ClientOptions, OpenAI } from 'openai';

export enum CustomizationType {
    PRES_TRAIL_SLIDE = 'trails',
}

interface PromptInfo {
    description: string;
    features: { name: string; description: string; values?: string[] }[];
}
const prompts: { [key: string]: PromptInfo } = {
    trails: {
        description:
            'We are customizing the properties and transition of a slide in a presentation. You are given the current properties of the slide in a json with the fields [title, presentation_transition, presentation_effect, config_zoom, presentation_effectDirection], as well as the prompt for how the user wants to change it. Return a json with the required fields: [title, presentation_transition, presentation_effect, config_zoom, presentation_effectDirection] by applying the changes in the prompt to the current state of the slide.',
        features: [],
    },
};

export const addCustomizationProperty = (type: CustomizationType, name: string, description: string, values?: string[]) => {
    values ? prompts[type].features.push({ name, description, values }) : prompts[type].features.push({ name, description });
};

export const gptSlideProperties = ['title', 'presentation_transition', 'presentation_effect', 'presentation_effectDirection', 'presEffectTiming', 'config_zoom'];

const setupPresSlideCustomization = () => {
    addCustomizationProperty(CustomizationType.PRES_TRAIL_SLIDE, 'title', 'is the title/name of the slide.');
    addCustomizationProperty(CustomizationType.PRES_TRAIL_SLIDE, 'presentation_transition', 'is a number in milliseconds for how long it should take to transition/move to a slide.');
    addCustomizationProperty(CustomizationType.PRES_TRAIL_SLIDE, 'presentation_effect', 'is an effect applied to the slide when we transition to it.', ['None', 'Zoom', 'Fade in', 'Bounce', 'Flip', 'Rotate', 'Roll']);
    addCustomizationProperty(CustomizationType.PRES_TRAIL_SLIDE, 'presentation_effectDirection', 'is what direction the effect is applied.', ['Enter from left', 'Enter from right', 'Enter from bottom', 'Enter from Top', 'Enter from center']);
    addCustomizationProperty(
        CustomizationType.PRES_TRAIL_SLIDE,
        'presEffectTiming',
        "is a json object of the format: {type: string, stiffness: number, damping: number, mass: number}. Type is always “custom”. Controls the spring-based timing of the presentation effect animation. Stiffness, damping, and mass control the physics-based properties of spring animations. This is used to create a more natural looking timing, bouncy effects, etc. Use spring physics to adjust these parameters to match the user's description of how they want to animate the effect."
    );
    addCustomizationProperty(CustomizationType.PRES_TRAIL_SLIDE, 'config_zoom', 'is a number from 0 to 1.0 indicating the percentage we should zoom into the slide.');
};

setupPresSlideCustomization();

export const gptTrailSlideCustomization = async (inputText: string, properties: any) => {
    console.log('properties', properties);
    let prompt = prompts.trails.description;

    prompts.trails.features.forEach(feature => {
        prompt += feature.name + ' ' + feature.description;
        if (feature.values) {
            prompt += `Its only possible values are [${feature.values.join(', ')}].`;
        }
    });

    // prompt +=
    //     'title is the title/name of the slide. presentation_transition is a number in milliseconds for how long it should take to transition/move to a slide. presentation_effect is an effect applied to the slide when we transition to it. Its only possible values are: [None, Fade in, Flip, Rotate, Bounce, Roll]. presentation_effectDirection is what direction the effect is applied. Its only possible values are:  [Enter from left, Enter from right, Enter from bottom, Enter from Top, Enter from center]. config_zoom is a number from 0 to 1.0 indicating the percentage we should zoom into the slide.';

    prompt += 'Set unchanged values to null. Please only return the json with these keys and their values.';

    console.log('messages', [
        { role: 'system', content: prompt },
        { role: 'user', content: `Prompt: ${inputText}, Current properties: ${JSON.stringify(properties)}` },
    ]);

    try {
        const response = await openai.chat.completions.create({
            model: 'gpt-4',
            messages: [
                { role: 'system', content: prompt },
                { role: 'user', content: `Prompt: ${inputText}, Current properties: ${JSON.stringify(properties)}` },
            ],
            temperature: 0,
            max_tokens: 1000,
        });
        return response.choices[0].message?.content;
    } catch (err) {
        console.log(err);
        return 'Error connecting with API.';
    }
};