File size: 4,662 Bytes
0d50f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

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