openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Internal use of axios causes error inside Cloudflare Workers

Open randomdevpete opened this issue 3 years ago • 3 comments

Describe the bug

I am trying to use the client inside a Cloudflare Worker and I get an error as follows:

TypeError: adapter is not a function
    at dispatchRequest (index.js:35781:14)
    at Axios.request (index.js:36049:19)
    at Function.wrap [as request] (index.js:34878:20)

Seems to be a common problem as the way Axios checks for XHR breaks in CF workers which is a reduced node environment:

https://community.cloudflare.com/t/typeerror-e-adapter-s-adapter-is-not-a-function/166469/2

Recommendation is to use fetch instead.

To Reproduce

Try to use API in a cloudflare worker

Code snippets

No response

OS

Windows 10

Node version

Node v16

Library version

openai v3.1.0

randomdevpete avatar Nov 29 '22 21:11 randomdevpete

I got the same error, because of axios adapters used XMLHttpRequest and node http, but workers used fetch to send request, so we should specify a fetch adapter, I found a package @vespaiach/axios-fetch-adapter, you can refer to the following code

const { Configuration, OpenAIApi } = require("openai");
+ const fetchAdapter = require("@vespaiach/axios-fetch-adapter")

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
+  baseOptions: {
+    adapter: fetchAdapter
+  }
});
const openai = new OpenAIApi(configuration);

Import

import { Configuration, OpenAIApi }  from 'openai'
+ import fetchAdapter from '@vespaiach/axios-fetch-adapter'

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
+  baseOptions: {
+    adapter: fetchAdapter
+  }
});
const openai = new OpenAIApi(configuration);

aliuq avatar Dec 08 '22 09:12 aliuq

Think you should ditch axios in favour of using fetch directly instead. Either use node-fetch, undici or global fetch (NodeJS v18 provides fetch built in - coming from undici) using fetch instead means it would be more functional inside of service worker, deno, bun.js and any other env. it would be more cross compatible.

also form-data is now a piece of legacy and more proper, spec'ed FormData is available to use.

When logging anything from response.data then it dumps way too much unnecessary information that cluters the hole terminal

if you got problem importing node-fetch@3 b/c it's ESM only then you could opt for something like this: https://github.com/node-fetch/node-fetch/issues/1279#issuecomment-915060754

jimmywarting avatar Dec 28 '22 01:12 jimmywarting

Looks like this was resurfaced in #47 and I am guessing it will be fixed if / when #45 is merged.

manovotny avatar Jan 18 '23 23:01 manovotny

would love to see this fixed — it's a blocker for using this library in Cloudflare, and as other comments have pointed out, there is no reason to use axios when fetch is widely supported

aroman avatar Jun 09 '23 22:06 aroman

This is fixed in our upcoming v4! Please give it a try on Cloudflare Workers and let us know if you run into any issues.

rattrayalex avatar Jul 10 '23 00:07 rattrayalex