I’ve been trying to use GPT to generate some original text for me to use in a little web-game I’ve been working on. When a certain text box loads, I wanted it to generate a slightly different but similar bit of text each time.
In VS Code itself, I’ve managed to get the system mostly working. My code for generating it is here:
console.log("Opening generate.mjs")
import OpenAI from './node_modules/openai/index.mjs'
const openai = new OpenAI({
apiKey: '■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■G0bmhSCG'
});
async function generateArrivalText() {
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: `Suggest a short arrival text message for a fleet pulling into new land from the sea.
Examples:
'The fleet pulls into a small cape, the sun low in the evening sky. Tiny waves lap against the side of the boat.',
'The fleet passes around a bend, revealing a new land ahead. The evening sunbeams shatter across the cresting waves.',
'The fleet glides along a shoreline of white sand, cliffs framing a beach ahead. Gusts of wind billow in the sails.',
'The fleet drops anchor at the base of a shore, its sailors weary after their journey. The sand sparkles under the bright sunlight.'
`}]
})
console.log(completion.choices[0].message.content);
}
generateArrivalText()
export {generateArrivalText}
By running “npm run dev” with this file (generate.mjs) set to dev in my package.json file, I’ve been able to see that it’s generating the text properly. Furthermore, I set up a test.mjs file to make sure my importing the generateArrivalText function was working correctly, and it is - doing “npm run” on the test file with the import calls the function properly. I’m importing the function the same way into my main game file, game.mjs.
However, when I use http-server or push the site to GitHub Pages to test it, the page fails to properly load, and I get the following error in the console:
Uncaught TypeError: Failed to resolve module specifier “openai”. Relative references must start with either “/”, “./”, or “…/”.
I tried to change the import to just “import OpenAI from ‘openai’” which still worked for the npm run but continued to give the same error otherwise.
The stack trace is just to the first line of the html file, game.html:1. Obviously there’s no “openai” reference there, so I assume this is just because the generate.mjs file (which imports openai) is used on the page.
After trying to see if ChatGPT itself could help me with this issue for about 4 hours, with me trying all kinds of different ideas, I’ve still been unable to fix this bug. I have reinstalled openai multiple times, tested in every way I know how, and I still get the bug.
I’m primarily confused why the generate.mjs file works properly when run in VS Code alone (importing everything successfully) but then seems to be the source of an error after I start up the server.
Any help would be amazing!
(Smaller side-issue: how can I use the API on my GitHub Pages website? Is it possible, or no because it always wipes the APIKey when I push?)