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.

111 lines
3.1 KiB

6 months ago
import OpenAI from "openai";
import express from "express";
import cors from "cors";
import { system_prompt } from "./system_prompt.js";
6 months ago
import { Client } from 'node-osc';
6 months ago
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,
});
6 months ago
const osc_client = new Client('127.0.0.1', 8787);
osc_client.send('/test', 55555, (error) => {
if (error) {
console.error('Error sending OSC message:', error);
} else {
console.log('OSC message sent successfully');
}
});
6 months ago
// 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;
6 months ago
// 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;
6 months ago
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);
6 months ago
const json=JSON.parse(response.output_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');
}
});
res.json(json);
6 months ago
} 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}`);
});