Voltboy1 commited on
Commit
e410b24
β€’
1 Parent(s): 5736178

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +150 -0
server.js ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ News
2
+ Artificial Intelligence
3
+ Applications
4
+ Linux
5
+
6
+
7
+ Home Artificial Intelligence
8
+ How to Setup OpenAI Reverse Proxy A Step-by-Step Guide
9
+ Cloudbooklet by Cloudbooklet June 24, 2023 in Artificial Intelligence, Deep Learning Reading Time: 8 mins read
10
+ How To Setup Openai Reverse Proxy
11
+ Share
12
+ Tweet
13
+ Send
14
+ Share
15
+ 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.
16
+
17
+ 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.
18
+
19
+ Table of Contents
20
+ Create a Hugging Face Space
21
+ Create Dockerfile
22
+ Configure OpenAI API Secret
23
+ Create Node.js file
24
+ Check OpenAI Reverse Proxy Deployment
25
+ Get your OpenAI Reverse Proxy URL
26
+ Errors and Troubleshooting – API connection
27
+ Conclusion
28
+
29
+ Also check: How to setup OpenAI Reverse Proxy with Nginx
30
+
31
+ Powered By
32
+ VDO.AI
33
+ PauseUnmute
34
+ Fullscreen
35
+ Create a Hugging Face Space
36
+ Login to your Hugging Face account, click on your profile icon on the right and click New Space.
37
+
38
+ YOU MIGHT ALSO LIKE
39
+ Stability Ai 1.0
40
+ Stability AI 1.0 on AWS Bedrock: A Step-by-Step Guide
41
+ JULY 28, 2023
42
+ Stability Ai Freewilly
43
+ How to Get Started with Stability AI FreeWilly
44
+ JULY 28, 2023
45
+ Openai Reverse Proxy Setup
46
+ Space Name: Enter the name for your space (openai-reverse-proxy).
47
+
48
+ Select Space SDK: Docker (We will use Docker to deploy).
49
+
50
+
51
+ Choose Docker Template: Blank
52
+
53
+ Everything else can be default.
54
+
55
+ Click Create Space.
56
+
57
+
58
+ Now a new machine with 2vCPU 16GB RAM will get provisioned for free.
59
+
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
+
64
+ Scroll a little below to see where it says β€œ(Hint: Create the Dockerfile file right in your browser alternatively)”
65
+
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
+ });