MUHARDOFF commited on
Commit
6a16c7e
1 Parent(s): 77ea696

Create server.js

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