From a534c5a3fbf49a44f090bc50c28c8b608a303a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=B8=8FYaskur=20Dyas=E2=9A=94=E2=9A=94=EF=B8=8F=E2=9A=94?= Date: Mon, 28 Oct 2024 20:12:14 +0700 Subject: [PATCH] refactor: initiate openAI inside a function --- helpers/gpt.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helpers/gpt.js b/helpers/gpt.js index 74f217b..f83e9c0 100644 --- a/helpers/gpt.js +++ b/helpers/gpt.js @@ -1,9 +1,5 @@ import OpenAI from 'openai'; -const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, -}); - /** * @param {string} something - thing that will get a random of it * @returns {string} the prompt for gpt @@ -14,6 +10,10 @@ function generatePrompt(something) { // eslint-disable-next-line require-jsdoc export async function getRandomFromGpt(something) { + const openai = new OpenAI({ + apiKey: process.env?.OPENAI_API_KEY, + }); + const completion = await openai.chat.completions.create({ model: 'gpt-4o-mini', messages: [{role: 'user', content: generatePrompt(something)}],