MUHARDOFF's picture
Create server.js
0d50f85
Cloudbooklet
Home Artificial Intelligence
How to Setup OpenAI Reverse Proxy A Step-by-Step Guide
by Cloudbooklet 3 months ago
How To Setup Openai Reverse Proxy
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.
In this tutorial, we will guide you through the process of configuring the OpenAI Reverse Proxy. The OpenAI Reverse Proxy allows you to securely integrate OpenAI API calls into your applications while maintaining control over the requests and responses. We will cover the necessary steps to set up and configure the reverse proxy effectively. Here […]
ADVERTISEMENT
In this tutorial, we will guide you through the process of configuring the OpenAI Reverse Proxy. The OpenAI Reverse Proxy allows you to securely integrate OpenAI API calls into your applications while maintaining control over the requests and responses. We will cover the necessary steps to set up and configure the reverse proxy effectively.
Here we will use a simple Node.js script for reverse proxy and deploy it in Hugging Face. We can also deploy it in our local computer manually with Node.js. But we are using Hugging Face, so that we can get the OpenAI Reverse Proxy URL. We can then use that API url on Janitor AI API or other services.
Table of Contents
Create a Hugging Face Space
Create Dockerfile
Configure OpenAI API Secret
Create Node.js file
Check OpenAI Reverse Proxy Deployment
Get your OpenAI Reverse Proxy URL
Errors and Troubleshooting – API connection
Conclusion
Also check: How to setup OpenAI Reverse Proxy with Nginx
ADVERTISEMENT
Create a Hugging Face Space
Login to your Hugging Face account, click on your profile icon on the right and click New Space.
YOU MIGHT ALSO LIKE
Video Stabilizer
How to Stabilize Videos for Free with Online Video Stabilizer
19 HOURS AGO
Ai Porn Generator
8 Best AI Porn Generators in 2023
19 HOURS AGO
Openai Reverse Proxy Setup
Space Name: Enter the name for your space (openai-reverse-proxy).
Select Space SDK: Docker (We will use Docker to deploy).
ADVERTISEMENT
Choose Docker Template: Blank
Everything else can be default.
ADVERTISEMENT
Click Create Space.
Now a new machine with 2vCPU 16GB RAM will get provisioned for free.
ADVERTISEMENT
Create Dockerfile
Once you have created your space, you will be redirected to the App page. This page contains all details about your deployment.
Scroll a little below to see where it says β€œ(Hint: Create the Dockerfile file right in your browser alternatively)”
ADVERTISEMENT
Openai Reverse Proxy Dockerfile
Click Create to add our Dockerfile configurations.
Copy the below code and add it to the Edit section input box.
Dockerfile
FROM node:18
WORKDIR /app
RUN npm install express express-http-proxy
COPY . .
EXPOSE 7860
CMD [ "node", "server.js" ]
The above configuration configures a Debian 11 machine with Node.js 18. Then installs the required packages and deploys the application to run on port 7860.
Openai Reverse Proxy Dockerfile
Click Commit new file to main.
This will create a new Dockerfile inside your space.
Configure OpenAI API Secret
Now go to your space settings and scroll down to find Repository secrets.
Openai Api Secret
Click New Secret.
In the popup box, enter the following.
Name: OPENAI_KEY
Secret value: Your API Key from OpenAI
Openai Api As Secret Key
Click Add new secret.
Now you have added your OpenAI API key as a secret.
Create Node.js file
Now you need to create a server.js file with the reverse proxy configurations that can be used with your OpenAI API key.
Go to Files in your space.
Click Add file and then click Create a new file.
Name your file: server.js
Copy the below in your Edit section.
server.js
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
const targetUrl = 'https://api.openai.com';
const openaiKey = process.env.OPENAI_KEY
const port = 7860;
const baseUrl = getExternalUrl(process.env.SPACE_ID);
app.use('/api', proxy(targetUrl, {
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
// Modify the request headers if necessary
proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
return proxyReqOpts;
},
}));
app.get("/", (req, res) => {
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
});
function getExternalUrl(spaceId) {
try {
const [username, spacename] = spaceId.split("/");
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
} catch (e) {
return "";
}
}
app.listen(port, () => {
console.log(`Reverse proxy server running on ${baseUrl}`);
});