From 91cf955f04e692708275a0bf1e7d5bd6722d38b8 Mon Sep 17 00:00:00 2001 From: Mayank Chhabra Date: Wed, 16 Aug 2023 02:18:53 +0700 Subject: [PATCH] Load model with an initial ping --- ui/Dockerfile | 7 ++-- ui/ping-api-to-load-model.js | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 ui/ping-api-to-load-model.js diff --git a/ui/Dockerfile b/ui/Dockerfile index 9862658..2536137 100644 --- a/ui/Dockerfile +++ b/ui/Dockerfile @@ -22,12 +22,15 @@ COPY --from=build /app/package*.json ./ COPY --from=build /app/next.config.js ./next.config.js COPY --from=build /app/next-i18next.config.js ./next-i18next.config.js -## Add the wait script to the image +# Add the wait script to the image COPY --from=ghcr.io/ufoscout/docker-compose-wait:latest /wait /wait +# Add the ping api script to the image +COPY ./ping-api-to-load-model.js ./ping-api-to-load-model.js + # Expose the port the app will run on EXPOSE 3000 # Start the application after the API is ready -CMD /wait && npm start +CMD /wait && node ping-api-to-load-model.js && npm start diff --git a/ui/ping-api-to-load-model.js b/ui/ping-api-to-load-model.js new file mode 100644 index 0000000..dccbc43 --- /dev/null +++ b/ui/ping-api-to-load-model.js @@ -0,0 +1,64 @@ +const http = require('http'); + +const apiHost = process.env.OPENAI_API_HOST ? process.env.OPENAI_API_HOST.split("//")[1].split(":")[0] : 'localhost'; +const apiPort = process.env.OPENAI_API_HOST ? process.env.OPENAI_API_HOST.split("//")[1].split(":")[1] : 9999; +const apiKey = process.env.OPENAI_API_KEY || 'sk-XXXXXXXXXXXXXXXXXXXX'; +const apiPath = '/v1/completions'; + +const payload = JSON.stringify({ + prompt: "\n\n### Reply with \"69\"\n\n### Response:\n", + stop: ["\n", "###"], + temperature: 0, +}); + +const options = { + hostname: apiHost, + port: apiPort, + path: apiPath, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': payload.length, + 'Authorization': `Bearer ${apiKey}`, + }, +}; + +function handleRequest() { + return new Promise((resolve, reject) => { + const req = http.request(options, (res) => { + let data = ''; + + // Handle data chunks + res.on('data', (chunk) => { + data += chunk; + }); + + // Resolve the promise when the response is complete + res.on('end', () => { + resolve(JSON.parse(data)); + }); + }); + + // Handle request errors + req.on('error', (error) => { + reject(error); + }); + + // Write the payload to the request + req.write(payload); + + // End the request + req.end(); + }); +} + +// Call the API and exit on result/error +handleRequest() + .then((result) => { + console.log('API response:', result); + process.exit(0); + }) + .catch((error) => { + console.error('Error:', error); + process.exit(1); + }); \ No newline at end of file