Hansimov commited on
Commit
edf7164
1 Parent(s): 2544ded

:gem: [Feature] Enbale http_proxy for axios request

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. README.md +1 -0
  3. server.js +38 -9
.gitignore CHANGED
@@ -2,4 +2,5 @@ node_modules
2
  package-lock.json
3
  .vscode
4
  live.js
5
- # configs/endpoints.json
 
 
2
  package-lock.json
3
  .vscode
4
  live.js
5
+ # configs/endpoints.json
6
+ configs/secrets.json
README.md CHANGED
@@ -9,6 +9,7 @@ app_port: 23456
9
 
10
  Mirrors:
11
  * https://hansimov-llm-mixer.hf.space
 
12
 
13
 
14
  ## Run
 
9
 
10
  Mirrors:
11
  * https://hansimov-llm-mixer.hf.space
12
+ * https://hf-llm-api.vercel.app
13
 
14
 
15
  ## Run
server.js CHANGED
@@ -49,43 +49,72 @@ app.get("/agents", async (req, res) => {
49
  }
50
  });
51
 
52
- app.post("/chat/completions", async (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  try {
54
  const {
55
  openai_endpoint,
56
  openai_request_method,
57
  openai_request_headers,
58
- openai_request_body,
59
  } = req.body;
60
 
61
  const response = await axios({
62
  method: openai_request_method,
63
- url: openai_endpoint + "/chat/completions",
64
- data: openai_request_body,
65
  headers: openai_request_headers,
66
- responseType: "stream",
67
  });
68
- response.data.pipe(res);
69
  } catch (error) {
70
  console.error(error);
71
  res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
72
  }
73
  });
74
 
75
- app.post("/models", async (req, res) => {
76
  try {
77
  const {
78
  openai_endpoint,
79
  openai_request_method,
80
  openai_request_headers,
 
81
  } = req.body;
82
 
83
  const response = await axios({
84
  method: openai_request_method,
85
- url: openai_endpoint + "/models",
 
86
  headers: openai_request_headers,
 
 
87
  });
88
- res.json(response.data);
89
  } catch (error) {
90
  console.error(error);
91
  res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
 
49
  }
50
  });
51
 
52
+ let httpProxyDict = {};
53
+ const loadHttpProxy = async () => {
54
+ try {
55
+ let secrets_configs_path = path.join(
56
+ __dirname,
57
+ "configs",
58
+ "secrets.json"
59
+ );
60
+ const data = await fs.readFile(secrets_configs_path, "utf-8");
61
+ const secrets = JSON.parse(data);
62
+ if (secrets.http_proxy) {
63
+ const url = new URL(secrets.http_proxy);
64
+ httpProxyDict = {
65
+ protocol: url.protocol.slice(0, -1),
66
+ host: url.hostname,
67
+ port: parseInt(url.port),
68
+ };
69
+ }
70
+ } catch (error) {
71
+ console.warn(
72
+ "Failed to load http_proxy: Maybe configs/secrets.json not existed?"
73
+ );
74
+ }
75
+ };
76
+
77
+ loadHttpProxy();
78
+
79
+ app.post("/models", async (req, res) => {
80
  try {
81
  const {
82
  openai_endpoint,
83
  openai_request_method,
84
  openai_request_headers,
 
85
  } = req.body;
86
 
87
  const response = await axios({
88
  method: openai_request_method,
89
+ url: openai_endpoint + "/models",
 
90
  headers: openai_request_headers,
91
+ proxy: httpProxyDict,
92
  });
93
+ res.json(response.data);
94
  } catch (error) {
95
  console.error(error);
96
  res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
97
  }
98
  });
99
 
100
+ app.post("/chat/completions", async (req, res) => {
101
  try {
102
  const {
103
  openai_endpoint,
104
  openai_request_method,
105
  openai_request_headers,
106
+ openai_request_body,
107
  } = req.body;
108
 
109
  const response = await axios({
110
  method: openai_request_method,
111
+ url: openai_endpoint + "/chat/completions",
112
+ data: openai_request_body,
113
  headers: openai_request_headers,
114
+ responseType: "stream",
115
+ proxy: httpProxyDict,
116
  });
117
+ response.data.pipe(res);
118
  } catch (error) {
119
  console.error(error);
120
  res.status(500).json({ error: "Failed to request OpenAI Endpoint" });