Objective: Common Objective of Requesting and Receiving Text-To-Image Generation from OpenAI using plain JavaScript and Displaying it to the User of the Script or Program.
This is the relevant code section that was put together:
const fs = require('fs'); // Import file system module
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({apiKey: "<!--insert your key string here : redacted by OP-->"});
const openai = new OpenAIApi(configuration)
try {
const response = openai.createImage({
prompt: "A cool memory from 90s Television",
n: 2,
size: "1024x1024"
});
const imageData = response.data.data[0].url; // Assuming response contains image URL
fs.writeFileSync('image.png', imageData, { encoding: 'base64' }); // Save as base64 encoded PNG
console.log("Image saved to image.png");
} catch (error) {
console.error(error);
}
Result:
We receive a SyntaxError
in the installed module file.
I suspect this is something wrong with the code, perhaps in the way I have initialised or dealt with the OpenAIApi
object. However, I’m open to receiving your diagnosis and subsequent rectification.
This is the error message produced when I run it with node v10.19.0
<dir_path>/node_modules/openai/index.js:51
constructor({ baseURL = Core.readEnv('OPENAI_BASE_URL'), apiKey = Core.readEnv('OPENAI_API_KEY'), organization = Core.readEnv('OPENAI_ORG_ID') ?? null, project = Core.readEnv('OPENAI_PROJECT_ID') ?? null, ...opts } = {}) {
^
SyntaxError: Unexpected token ?
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/nik/Desktop/img.js:3:38)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
This is a module error, I can’t be expected to solve this alone.
I have only ever come across ?
in the sense of nullability and in ternary operator.
I’m using [email protected]
installed with npm 6.14.4
on Ubuntu 20.04.6 LTS
.
Please provide anything relevant. All discussion is welcome to prove this objective.
Additional Bonus Question (optional):
Rather than saving the file, I’d really like it to pop a window into view, or within the terminal if that is possible now, when I run node filename.js
where filename.js
is the JavaScript file where I have saved this code.
I tried running the code several times, changing things here and there without any difference in error output. Currently, I am trying to consult StackOverflow. I did not find this particular error response and find it unusual and hindering the DX. Posting this question, I was expecting resolution of the problem so I can run the program with the intended output.
Nikhil Kartha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.