|
|
|
|
@ -2,9 +2,7 @@ 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 { Client } from 'node-osc'; |
|
|
|
|
|
|
|
|
|
import { config } from "dotenv"; |
|
|
|
|
config(); // Load environment variables from .env file
|
|
|
|
|
@ -33,6 +31,15 @@ const client = new OpenAI({ |
|
|
|
|
apiKey: process.env.OPENAI_API_KEY,
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// const response = await client.responses.create({
|
|
|
|
|
// model: "gpt-4.1",
|
|
|
|
|
// input: "Write a one-sentence bedtime story about a unicorn."
|
|
|
|
|
@ -48,6 +55,11 @@ app.use(cors()); |
|
|
|
|
|
|
|
|
|
app.post("/generate", 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({ |
|
|
|
|
@ -74,8 +86,20 @@ app.post("/generate", async (req, res) => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
console.log("Generated response:", response); |
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.json(JSON.parse(response.output_text)); |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error("Error generating response:", error); |
|
|
|
|
res.status(500).json({ error: "Failed to generate response" }); |
|
|
|
|
|