Skip to content

Instantly share code, notes, and snippets.

@akira-cn
Last active November 16, 2023 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira-cn/8d50dccd6fd82c1ea2fa0e5144a07fe2 to your computer and use it in GitHub Desktop.
Save akira-cn/8d50dccd6fd82c1ea2fa0e5144a07fe2 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
const endpoint = "api.openai.com";
async function handleRequest(params, context) {
const path = context.request.url.replace(/\/[^\/]+/, '');
const fetchAPI = `https://${endpoint}/v1${path}`;
console.log(fetchAPI);
const headers = {
...context.headers,
};
delete headers['content-length'];
delete headers['host'];
const payload = {
method: context.method,
headers,
body: JSON.stringify({
...params
}),
};
let response = await fetch(fetchAPI, payload);
// console.log(response.body);
// console.log(response.headers.get('content-type'));
const streaming = params.stream;
for(const key of Object.keys(response.headers)) {
context.set(key, response.headers.get(key));
}
if(streaming) {
return response.body;
} else {
return response.json();
}
}
module.exports = async function (params, context) {
return await handleRequest(params, context);
};
@akira-cn
Copy link
Author

要设置环境变量 EXPERIMENTAL_ROUTE = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment