You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
87 lines
2.3 KiB
import OpenAI from "openai";
|
|
import express from "express";
|
|
import cors from "cors";
|
|
import { system_prompt } from "./system_prompt.js";
|
|
|
|
import { z } from "zod";
|
|
import { zodResponseFormat } from "openai/helpers/zod";
|
|
|
|
import { config } from "dotenv";
|
|
config(); // Load environment variables from .env file
|
|
|
|
|
|
const Output = {
|
|
"type": "object",
|
|
"properties": {
|
|
"prompt": {
|
|
"type": "string",
|
|
"description": "The generated image prompt based on the user's input and the system's guidance.",
|
|
},
|
|
"output_text": {
|
|
"type": "string",
|
|
"description": "The final output text generated by the model, without image prompt",
|
|
}
|
|
},
|
|
"additionalProperties": false,
|
|
"required": [
|
|
"prompt", "output_text"
|
|
]
|
|
}
|
|
|
|
|
|
const client = new OpenAI({
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
});
|
|
|
|
// const response = await client.responses.create({
|
|
// model: "gpt-4.1",
|
|
// input: "Write a one-sentence bedtime story about a unicorn."
|
|
// });
|
|
|
|
// console.log(response.output_text);
|
|
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 3000;
|
|
app.use(express.json());
|
|
app.use(cors());
|
|
|
|
app.post("/generate", async (req, res) => {
|
|
const { input } = req.body;
|
|
|
|
try {
|
|
const response = await client.responses.create({
|
|
model: "gpt-4.1",
|
|
input: [
|
|
{
|
|
role: "system",
|
|
content: [
|
|
{
|
|
type:'input_text',
|
|
text: system_prompt,
|
|
}
|
|
]
|
|
},
|
|
...input
|
|
],
|
|
text:{
|
|
format:{
|
|
type:'json_schema',
|
|
name:"output_prompt",
|
|
schema: Output,
|
|
}
|
|
}
|
|
});
|
|
|
|
console.log("Generated response:", response);
|
|
|
|
res.json(JSON.parse(response.output_text));
|
|
} catch (error) {
|
|
console.error("Error generating response:", error);
|
|
res.status(500).json({ error: "Failed to generate response" });
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server is running on http://localhost:${port}`);
|
|
}); |