import { ClientOptions, OpenAI } from 'openai'; import { ChatCompletionMessageParam } from 'openai/resources'; enum GPTCallType { SUMMARY = 'summary', COMPLETION = 'completion', EDIT = 'edit', // MERMAID='mermaid' SORT = 'sort' } type GPTCallOpts = { model: string; maxTokens: number; temp: number; prompt: string; }; 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: '' }, sort:{model:'gpt-4-turbo',maxTokens:2048,temp:0,prompt:"I'm going to give you a list of strings. Sort them into lists by shared content. The format of should be: '[string1, string2], [string 3, string4][string5, string6]' where the actual numbers of strings in each category / total categorys is arbritray and up to your interpretation"} }; /**` * 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 opts: GPTCallOpts = callTypeMap[callType]; try { const configuration: ClientOptions = { apiKey: "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", dangerouslyAllowBrowser: true, }; const openai = new OpenAI(configuration); let messages: ChatCompletionMessageParam[] = [ { role: 'system', content: opts.prompt }, { role: 'user', content: inputText }, ]; const response = await openai.chat.completions.create({ model: opts.model, messages: messages, temperature: opts.temp, max_tokens: opts.maxTokens, }); 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: any) => data.url); // return response.data.data[0].url; } catch (err) { console.error(err); return; } }; export { gptAPICall, gptImageCall, GPTCallType };