How to Setup OpenAI Reverse Proxy A Step-by-Step Guide

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.

Also check: How to setup OpenAI Reverse Proxy with Nginx

Create a Hugging Face Space

Login to your Hugging Face account, click on your profile icon on the right and click New Space.

Space Name: Enter the name for your space (openai-reverse-proxy).

Select Space SDK: Docker (We will use Docker to deploy).

Choose Docker Template: Blank

Everything else can be default.

Click Create Space.

Now a new machine with 2vCPU 16GB RAM will get provisioned for free.

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)”

See also  Mojo vs Python: Which Programming Language is Right for You?

Click Create to add our Dockerfile configurations.

Copy the below code and add it to the Edit section input box.

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.

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.

Click New Secret.

In the popup box, enter the following.

Name: OPENAI_KEY

Secret value: Your API Key from OpenAI

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.

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}`); });

See also  Google AI Journalist: Can Google Genesis Replace Human Reporters?

Click Commit new file to main.

Now a server.js file will get created in your space.

Check OpenAI Reverse Proxy Deployment

Now you can go to your App page and see the deployment status. You will see something similar to the one below.

You can copy the url and safely close it.

If you check the URL in your browser you will see this output on your screen, which means reverse proxy API is working fine. The error is shown because you haven’t passed any parameters in your API url.

{ “error”: { “message”: “Invalid URL (GET /)”, “type”: “invalid_request_error”, “param”: null, “code”: null } }

Get your OpenAI Reverse Proxy URL

You can get your reverse proxy url of your deployment from your log or you can generate using the below method.

Syntax: https://<username>-<spacename>.hf.space/api/v1

In this case my reverse proxy url is https://cloudbooklet-reverse-proxy.hf.space/api/v1

You can use the URL as the OpenAI Reverse Proxy URL of your applications.

Also read: How to Configure OpenAI Reverse Proxy in Janitor AI API

Errors and Troubleshooting – API connection

You may face some errors while using the Reverse proxy URL or API key in Janitor AI or Venus AI or some other platforms. Please check the below article to troubleshoot your errors.

Janitor AI not working: Errors and Solutions

Conclusion

Now you have learned how to setup and configure OpenAI reverse proxy on Hugging Face with Debian 11, Docker and Node.js.