const OpenAI = require("openai");
const { HttpsProxyAgent } = require("https-proxy-agent");
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
httpAgent: new HttpsProxyAgent(process.env.PROXY_URL),
});
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
// response_format: { type: "json_object" }
});
/**
* response body
{
id: "chatcmpl-8Id5scysUzAydGx9islUyeYSWpu4Z",
object: "chat.completion",
created: 1699450872,
model: "gpt-3.5-turbo-0613",
choices: [
{
index: 0,
message: {
role: "assistant",
content: "How may I assist you today?",
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: 13,
completion_tokens: 7,
total_tokens: 20,
},
}
*/
const chatCompletion = await openai.chat.completions.create({
model: "gpt-3.5-turbo-0613",
messages: [{ role: "user", content: "What's happening in the NBA today?" }],
functions: [
{
name: "read_website_content",
description: "Read the content on a given website",
parameters: {
type: "object",
properties: {
url: {
type: "string",
description: "The URL to the website to read ",
},
},
required: ["url"],
},
},
],
});
/**
*
{
id: "chatcmpl-8Jhn45qsHDvWGL8t8j2raYFHRxVrf",
object: "chat.completion",
created: 1699707254,
model: "gpt-3.5-turbo-0613",
choices: [
{
index: 0,
message: {
role: "assistant",
content: null,
function_call: {
name: "read_website_content",
arguments: "{\n \"url\": \"https://www.nba.com/\"\n}",
},
},
finish_reason: "function_call",
},
],
usage: {
prompt_tokens: 65,
completion_tokens: 21,
total_tokens: 86,
},
}
*/
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
// response_format: { type: "json_object" }
messages: [
{ role: "user", content: "I want a text prompt for an image" },
{ role: "system", content: "Enlighten me, please" },
{
role: "assistant",
content: "What's the image used for?",
},
{
role: "user",
content: "To design an iPhone 14 Pro Max app splash screen background",
},
{
role: "assistant",
content: "What are the contents that you want to put in?",
},
{
role: "user",
content:
"a big house with powerwall installed on a countryside, solar power panel on the roof, a power grid, all electrial devices above are connected with a reserver transformer (aka. RT), the electric flow from solar panel to RT is unidirectional, RT to power grid and powerwall are biodirectional, I can see power flow in the electric wires",
},
{ role: "assistant", content: "What kind of style do you prefer" },
{ role: "user", content: "Technical and science, simple" },
{
role: "user",
content: "What's the best prompt to ask GPT for this image?",
},
],
});
/**
* {
id: "chatcmpl-8LBPAup7hOoBMYavv1lEWYKgq0YLk",
object: "chat.completion",
created: 1700059420,
model: "gpt-3.5-turbo-0613",
choices: [
{
index: 0,
message: {
role: "assistant",
content: "\"Describe a futuristic energy-efficient countryside house featuring a big solar power panel on the roof, a Powerwall installed, and a power grid. The house is connected to a reserve transformer (RT), ensuring a steady flow of electricity. The power flow can be visually observed in the electric wires. The setting should evoke a sense of technological advancement and sustainability.\"",
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: 181,
completion_tokens: 70,
total_tokens: 251,
},
}
*/
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
// response_format: { type: "json_object" },
messages: [
{ role: "user", content: "I want a text prompt for an image" },
{ role: "system", content: "Enlighten me, please" },
{
role: "user",
content:
"a futuristic energy-efficient countryside house in the center of the image, a big solar power panel on the roof, a Powerwall installed on the wall, and a power grid. The house is connected to a reserve transformer (RT), ensuring a steady flow of electricity. The power flow can be visually observed in the electric wires. The setting should evoke a sense of technological advancement and sustainability.",
},
{
role: "assistant",
content: "What else can I do for you?",
},
{
role: "user",
content:
"the image is for iPhone 14Pro max wallpaper, please aware the size and brightness, What's the best prompt to ask GPT for this image?",
},
],
});
/**
* {
id: "chatcmpl-8M6q0y3gkSPvwazbuL9zcaTnITcs4",
object: "chat.completion",
created: 1700280192,
model: "gpt-3.5-turbo-0613",
choices: [
{
index: 0,
message: {
role: "assistant",
content: "A serene fusion of technology and sustainability awaits your gaze. Behold the iPhone 14 Pro Max wallpaper—brilliantly showcasing a futuristic, energy-efficient countryside house at its heart. The house, adorned with a large solar power panel on its roof, radiates a vibrant glow, harnessed from the sun's boundless energy. On one of its walls, a sleek Powerwall stands, embodying the harmony between modern design and renewable power storage. Electric wires traverse the image, visibly flowing with dynamic energy, symbolizing the seamless connection to a powerful, eco-conscious grid. Immerse yourself in this visionary landscape, where innovation meets serenity, as it adorns your iPhone 14 Pro Max screen with an ethereal blend of technological advancement and sustainability.",
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: 153,
completion_tokens: 154,
total_tokens: 307,
},
}
*/