MUHARDOFF commited on
Commit
0d50f85
β€’
1 Parent(s): 3c50fdf

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +151 -0
server.js ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Cloudbooklet
3
+
4
+ Home Artificial Intelligence
5
+ How to Setup OpenAI Reverse Proxy A Step-by-Step Guide
6
+ by Cloudbooklet 3 months ago
7
+ How To Setup Openai Reverse Proxy
8
+ Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.
9
+ 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 […]
10
+
11
+
12
+ ADVERTISEMENT
13
+ 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.
14
+
15
+ 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.
16
+
17
+ Table of Contents
18
+ Create a Hugging Face Space
19
+ Create Dockerfile
20
+ Configure OpenAI API Secret
21
+ Create Node.js file
22
+ Check OpenAI Reverse Proxy Deployment
23
+ Get your OpenAI Reverse Proxy URL
24
+ Errors and Troubleshooting – API connection
25
+ Conclusion
26
+
27
+ Also check: How to setup OpenAI Reverse Proxy with Nginx
28
+
29
+
30
+ ADVERTISEMENT
31
+ Create a Hugging Face Space
32
+ Login to your Hugging Face account, click on your profile icon on the right and click New Space.
33
+
34
+ YOU MIGHT ALSO LIKE
35
+ Video Stabilizer
36
+ How to Stabilize Videos for Free with Online Video Stabilizer
37
+ 19 HOURS AGO
38
+ Ai Porn Generator
39
+ 8 Best AI Porn Generators in 2023
40
+ 19 HOURS AGO
41
+ Openai Reverse Proxy Setup
42
+ Space Name: Enter the name for your space (openai-reverse-proxy).
43
+
44
+ Select Space SDK: Docker (We will use Docker to deploy).
45
+
46
+
47
+ ADVERTISEMENT
48
+ Choose Docker Template: Blank
49
+
50
+ Everything else can be default.
51
+
52
+
53
+ ADVERTISEMENT
54
+ Click Create Space.
55
+
56
+ Now a new machine with 2vCPU 16GB RAM will get provisioned for free.
57
+
58
+
59
+ ADVERTISEMENT
60
+ Create Dockerfile
61
+ Once you have created your space, you will be redirected to the App page. This page contains all details about your deployment.
62
+
63
+ Scroll a little below to see where it says β€œ(Hint: Create the Dockerfile file right in your browser alternatively)”
64
+
65
+
66
+ ADVERTISEMENT
67
+ Openai Reverse Proxy Dockerfile
68
+ Click Create to add our Dockerfile configurations.
69
+
70
+ Copy the below code and add it to the Edit section input box.
71
+
72
+ Dockerfile
73
+ FROM node:18
74
+
75
+ WORKDIR /app
76
+
77
+ RUN npm install express express-http-proxy
78
+
79
+ COPY . .
80
+
81
+ EXPOSE 7860
82
+
83
+ CMD [ "node", "server.js" ]
84
+ 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.
85
+
86
+ Openai Reverse Proxy Dockerfile
87
+ Click Commit new file to main.
88
+
89
+ This will create a new Dockerfile inside your space.
90
+
91
+ Configure OpenAI API Secret
92
+ Now go to your space settings and scroll down to find Repository secrets.
93
+
94
+ Openai Api Secret
95
+ Click New Secret.
96
+
97
+ In the popup box, enter the following.
98
+
99
+ Name: OPENAI_KEY
100
+
101
+ Secret value: Your API Key from OpenAI
102
+
103
+ Openai Api As Secret Key
104
+ Click Add new secret.
105
+
106
+ Now you have added your OpenAI API key as a secret.
107
+
108
+ Create Node.js file
109
+ Now you need to create a server.js file with the reverse proxy configurations that can be used with your OpenAI API key.
110
+
111
+ Go to Files in your space.
112
+
113
+ Click Add file and then click Create a new file.
114
+
115
+ Name your file: server.js
116
+
117
+ Copy the below in your Edit section.
118
+
119
+ server.js
120
+ const express = require('express');
121
+ const proxy = require('express-http-proxy');
122
+ const app = express();
123
+ const targetUrl = 'https://api.openai.com';
124
+ const openaiKey = process.env.OPENAI_KEY
125
+ const port = 7860;
126
+ const baseUrl = getExternalUrl(process.env.SPACE_ID);
127
+
128
+ app.use('/api', proxy(targetUrl, {
129
+ proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
130
+ // Modify the request headers if necessary
131
+ proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
132
+ return proxyReqOpts;
133
+ },
134
+ }));
135
+
136
+ app.get("/", (req, res) => {
137
+ res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
138
+ });
139
+
140
+ function getExternalUrl(spaceId) {
141
+ try {
142
+ const [username, spacename] = spaceId.split("/");
143
+ return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
144
+ } catch (e) {
145
+ return "";
146
+ }
147
+ }
148
+
149
+ app.listen(port, () => {
150
+ console.log(`Reverse proxy server running on ${baseUrl}`);
151
+ });