|
|
|
|
@ -11,18 +11,18 @@ 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", |
|
|
|
|
}, |
|
|
|
|
"prompt": { |
|
|
|
|
"type": "string", |
|
|
|
|
"description": "The generated image prompt based on the user's input and the system's guidance.", |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
"additionalProperties": false, |
|
|
|
|
"required": [ |
|
|
|
|
"prompt", "output_text" |
|
|
|
|
"output_text","prompt" |
|
|
|
|
] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -53,13 +53,9 @@ const port = process.env.PORT || 3000; |
|
|
|
|
app.use(express.json()); |
|
|
|
|
app.use(cors()); |
|
|
|
|
|
|
|
|
|
app.post("/generate", async (req, res) => { |
|
|
|
|
app.post("/generate_stream", async (req, res) => { |
|
|
|
|
const { input } = req.body; |
|
|
|
|
// console.log(input[input.length-1], 'input received');
|
|
|
|
|
// osc_client.send('/prompt', input[input.length-1]?.content[0]?.text, (err) => {
|
|
|
|
|
// console.log('OSC', err ? `Error: ${err}` : 'Success');
|
|
|
|
|
// });
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
const response = await client.responses.create({ |
|
|
|
|
@ -82,10 +78,75 @@ app.post("/generate", async (req, res) => { |
|
|
|
|
name:"output_prompt", |
|
|
|
|
schema: Output, |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
stream:true, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
for await (const event of response){ |
|
|
|
|
console.log(event); |
|
|
|
|
if(event.type=='response.output_text.delta'){ |
|
|
|
|
|
|
|
|
|
// console.log(event.delta);
|
|
|
|
|
|
|
|
|
|
}else if(event.type=='response.output_item.done'){ |
|
|
|
|
|
|
|
|
|
console.log("Generated response:", event.item.content); |
|
|
|
|
const json=JSON.parse(event.item.content[0].text); |
|
|
|
|
|
|
|
|
|
// send prompt to TD
|
|
|
|
|
osc_client.send('/prompt', json.prompt, (error) => { |
|
|
|
|
if (error) { |
|
|
|
|
console.error('Error sending OSC message:', error); |
|
|
|
|
} else { |
|
|
|
|
console.log('OSC message sent successfully'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
console.log("Generated response:", response); |
|
|
|
|
res.json(json); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
console.error("Error generating response:", error); |
|
|
|
|
res.status(500).json({ error: "Failed to generate response" }); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.output_text); |
|
|
|
|
const json=JSON.parse(response.output_text); |
|
|
|
|
|
|
|
|
|
// send prompt to TD
|
|
|
|
|
@ -100,6 +161,7 @@ app.post("/generate", async (req, res) => { |
|
|
|
|
res.json(json); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
console.error("Error generating response:", error); |
|
|
|
|
res.status(500).json({ error: "Failed to generate response" }); |
|
|
|
|
|