Last active
November 16, 2023 16:10
-
-
Save akira-cn/8d50dccd6fd82c1ea2fa0e5144a07fe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
要设置环境变量 EXPERIMENTAL_ROUTE = 1