Skip to content

Commit

Permalink
Ping API once for completition before launching ui container
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankchhabra committed Aug 15, 2023
1 parent 680ff51 commit 5e5aea9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ui/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

64 changes: 64 additions & 0 deletions ui/ping-api-to-load-model.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 5e5aea9

Please sign in to comment.