diff options
Diffstat (limited to 'src/client/apis/gpt/GPT.ts')
| -rw-r--r-- | src/client/apis/gpt/GPT.ts | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/src/client/apis/gpt/GPT.ts b/src/client/apis/gpt/GPT.ts index 3b3b3b9a0..6857246da 100644 --- a/src/client/apis/gpt/GPT.ts +++ b/src/client/apis/gpt/GPT.ts @@ -7,6 +7,8 @@ enum GPTCallType { EDIT = 'edit', SORT = 'sort', DESCRIBE = 'describe', + MERMAID = 'mermaid', + DATA = 'data', } type GPTCallOpts = { @@ -17,9 +19,22 @@ type GPTCallOpts = { }; const callTypeMap: { [type: string]: GPTCallOpts } = { - summary: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: 'Summarize this text in simpler terms: ' }, - edit: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: 'Reword this: ' }, - completion: { model: 'gpt-3.5-turbo-instruct', maxTokens: 256, temp: 0.5, prompt: '' }, + // 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." }, + mermaid: { + model: 'gpt-4-turbo', + maxTokens: 2048, + temp: 0, + prompt: "(Heres an example of changing color of a pie chart to help you pie title Example \"Red\": 20 \"Blue\": 50 \"Green\": 30 %%{init: {'theme': 'base', 'themeVariables': {'pie1': '#0000FF', 'pie2': '#00FF00', 'pie3': '#FF0000'}}}%% keep in mind that pie1 is the highest since its sorted in descending order. Heres an example of a mindmap: mindmap root((mindmap)) Origins Long history ::icon(fa fa-book) Popularisation British popular psychology author Tony Buzan Research On effectivness<br/>and features On Automatic creation Uses Creative techniques Strategic planning Argument mapping Tools Pen and paper Mermaid. ", + }, + 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 only return a JSON with a correlation column 1 propert, a correlation column 2 property, and an analysis property. ", + }, sort: { model: 'gpt-4o', maxTokens: 2048, @@ -29,25 +44,29 @@ const callTypeMap: { [type: string]: GPTCallOpts } = { describe: { model: 'gpt-4-vision-preview', maxTokens: 2048, temp: 0, prompt: 'Describe these images in 3-5 words' }, }; -/**` +let lastCall = ''; +let lastResp = ''; +/** * Calls the OpenAI API. * * @param inputText Text to process * @returns AI Output */ - -const gptAPICall = async (inputText: string, callType: GPTCallType) => { - if (callType === GPTCallType.SUMMARY) inputText += '.'; +const gptAPICall = async (inputTextIn: string, callType: GPTCallType, prompt?: any) => { + const inputText = callType === GPTCallType.SUMMARY ? inputTextIn + '.' : inputTextIn; const opts: GPTCallOpts = callTypeMap[callType]; + if (lastCall === inputText) return lastResp; try { const configuration: ClientOptions = { apiKey: process.env.OPENAI_KEY, dangerouslyAllowBrowser: true, }; + lastCall = inputText; const openai = new OpenAI(configuration); - let messages: ChatCompletionMessageParam[] = [ - { role: 'system', content: opts.prompt }, + const usePrompt = prompt ? opts.prompt + prompt : opts.prompt; + const messages: ChatCompletionMessageParam[] = [ + { role: 'system', content: usePrompt }, { role: 'user', content: inputText }, ]; @@ -57,8 +76,8 @@ const gptAPICall = async (inputText: string, callType: GPTCallType) => { temperature: opts.temp, max_tokens: opts.maxTokens, }); - const content = response.choices[0].message.content; - return content; + lastResp = response.choices[0].message.content ?? ''; + return lastResp; } catch (err) { console.log(err); return 'Error connecting with API.'; @@ -68,7 +87,7 @@ const gptAPICall = async (inputText: string, callType: GPTCallType) => { const gptImageLabel = async (imgUrl: string): Promise<string> => { try { const configuration: ClientOptions = { - apiKey: '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', + apiKey: process.env.OPENAI_KEY, dangerouslyAllowBrowser: true, }; @@ -118,8 +137,8 @@ const gptImageCall = async (prompt: string, n?: number) => { // return response.data.data[0].url; } catch (err) { console.error(err); - return; } + return undefined; }; export { gptAPICall, gptImageCall, gptImageLabel, GPTCallType }; |
