Skip to content

Instantly share code, notes, and snippets.

@teramotodaiki
Created September 21, 2023 09:56
Show Gist options
  • Save teramotodaiki/5cf2bd7c62a356ef3a80d5438e7b381b to your computer and use it in GitHub Desktop.
Save teramotodaiki/5cf2bd7c62a356ef3a80d5438e7b381b to your computer and use it in GitHub Desktop.
Azure OpenAI SDKをCloudflare Workerで利用するためのモンキーパッチ
/*
Azure OpenAI SDK(@azure/openai)をCloudflare Workerで利用するとこういうエラーが出る
Trace: RestError: Error sending request: The 'credentials' field on 'RequestInitializerDict' is not implemented.
at getError (file:///private/var/folders/32/.../T/.../index.js:3407:12)
at FetchHttpClient.sendRequest (file:///private/var/folders/32/.../T/.../index.js:3317:13) {
name: RestError,
code: REQUEST_SEND_ERROR,
statusCode: undefined,
request: PipelineRequestImpl,
response: undefined
...
}
詳しくは下記のissueを参照
https://github.com/cloudflare/workers-sdk/issues/2514
これ以外は上手く動くっぽいので、モンキーパッチを当てて回避することにした
これでいいのかどうかは分からないので、コメント大歓迎です
*/
// Azure OpenAI SDKをCloudflare Workerで利用するためのモンキーパッチ
const originalFetch = fetch;
self.fetch = (...args) => {
if (args[1]) {
// Cloudflare Workerでは利用できないオプションを削除
if ("credentials" in args[1]) {
delete args[1]["credentials"];
}
if ("cache" in args[1]) {
delete args[1]["cache"];
}
}
return originalFetch(...args);
};
@teramotodaiki
Copy link
Author

This is a monkey patch for the issue cloudflare/workers-sdk#2514

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