Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.05k stars 1.19k forks source link

`Error: proxyPolicy is not supported in browser environment` when using @azure/openai@1.0.0-beta.11 #28672

Closed nietsmmar closed 6 months ago

nietsmmar commented 6 months ago

Describe the bug Using Azure/Openai SDK in a Deno environment causing the following issue:

Error: proxyPolicy is not supported in browser environment
    at K (https://esm.sh/v135/@azure/core-rest-pipeline@1.13.0/esnext/core-rest-pipeline.mjs:2:12511)
    at At (https://esm.sh/v135/@azure/core-rest-pipeline@1.13.0/esnext/core-rest-pipeline.mjs:9:1276)
    at S (https://esm.sh/v135/@azure-rest/core-client@1.1.7/esnext/core-client.mjs:2:1692)
    at he (https://esm.sh/v135/@azure-rest/core-client@1.1.7/esnext/core-client.mjs:2:6564)
    at h (https://esm.sh/v135/@azure/openai@1.0.0-alpha.20240223.1/esnext/rest.js:2:965)
    at K (https://esm.sh/v135/@azure/openai@1.0.0-alpha.20240223.1/esnext/api.js:2:102)
    at new y (https://esm.sh/v135/@azure/openai@1.0.0-alpha.20240223.1/esnext/openai.mjs:2:7379)

To Reproduce Steps to reproduce the behavior: Import azure/openai in a deno environment. I am using supabase functions.

Expected behavior A clear and concise description of what you expected to happen.

Screenshots image

Additional context This is in a Supabase environment

Version 1.0.0-beta.7 still works.

Might be the same error as https://github.com/Azure/azure-sdk-for-js/issues/27077

mpodwysocki commented 6 months ago

@nietsmmar

Using a plain Deno environment, I am not able to reproduce this error. Sample code is available here https://github.com/mpodwysocki/azure-openai-deno

How are you referencing the package?

nietsmmar commented 6 months ago

@mpodwysocki I am using an import_map and import it like this:

"azureai": "https://esm.sh/@azure/openai@1.0.0-beta.11"

when importing beta.7 instead I don't get the error and everything works fine. So the change has to come from beta.7 to beta.8.

Then in my code I do this:

import { OpenAIClient, AzureKeyCredential } from "azureai";

export const azureOpenAi = new OpenAIClient(
  "my_url", 
  new AzureKeyCredential(Deno.env.get('AZURE_OPENAI_API_KEY'))
);
mpodwysocki commented 6 months ago

@nietsmmar

Instead of using the ESM site, can you try directly with the npm specifier such as npm:@azure/openai@1.0.0-beta.11 to see if it's a bug with the ESM site? I think the issue is that the ESM site is picking up our browser builds and not our npm compat builds, which are two separate bundles.

nietsmmar commented 6 months ago

@mpodwysocki

oh okay. I did not know that these builds are different. I tried using it directly from npm first. But it did not work, giving me this error:

Error: unreachable
    at arch (ext:deno_node/_process/process.ts:16:11)
    at get arch [as arch] (node:process:301:14)
    at Object.arch (node:os:35:18)
    at setPlatformSpecificData (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure/core-rest-pipeline/1.14.0/dist/index.js:542:37)
    at getUserAgentValue (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure/core-rest-pipeline/1.14.0/dist/index.js:572:5)
    at userAgentPolicy (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure/core-rest-pipeline/1.14.0/dist/index.js:591:28)
    at Object.createPipelineFromOptions (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure/core-rest-pipeline/1.14.0/dist/index.js:1826:24)
    at createDefaultPipeline (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure-rest/core-client/1.2.0/dist/index.js:107:39)
    at getClient (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure-rest/core-client/1.2.0/dist/index.js:436:22)
    at createClient (file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@azure/openai/1.0.0-beta.11/dist-esm/src/rest/openAIClient.js:35:20)
failed to send request to user worker: connection error: Connection reset by peer (os error 104)
InvalidWorkerResponse: user worker failed to respond
    at async Promise.all (index 1)
    at async UserWorker.fetch (ext:sb_user_workers/user_workers.js:64:19)
    at async Server.<anonymous> (file:///home/deno/main/index.ts:146:12)
    at async #respond (https://deno.land/std@0.182.0/http/server.ts:220:18) {
  name: "InvalidWorkerResponse"
}

I thought this might be due to supabase not perfectly integrationg npm packages directly. Although it should work now as stated here.

mpodwysocki commented 6 months ago

@nietsmmar Given that you are using a year old Deno version in 0.182.0, would you be able to upgrade to see if that fixes the issues?

nietsmmar commented 6 months ago

@mpodwysocki

This is a version used inside supabase-cli. I can't change that.

What I just tried: Deploy my edge function on the supabase remote project. There it should use a more recent Deno version. This is the result of Deno.version: supabase-edge-runtime-1.33.5 (compatible with Deno v1.39.2)

Interestingly, deployed there the beta.7 version also does not work and I get the same error.

Locally after upgrading to the newsest supabase-cli it also is not working anymore with beta.7.

nietsmmar commented 6 months ago

As Supabase fixed the npm import issue here, it is working for me now.

The error is only coming when using the CDN.

mpodwysocki commented 6 months ago

@nietsmmar appreciate it! I assume it's because it's picking up the browser bundles of our libraries. I don't anticipate us changing this behavior from our side, although it would be nice if ESM allowed you to specify which set of exports you could use such as import/require/browser.

In the future, we are shipping more with exports such as the following in our package.json. What would be nice is for ESM to allow you to specify which specify which you could use without defaulting to browser. I think that should be a feature request to the ESM folks. Not sure what else is actionable here. Please re-open if that remains an issue.

  "exports": {
    "./package.json": "./package.json",
    ".": {
      "browser": {
        "types": "./dist/browser/index.d.ts",
        "default": "./dist/browser/index.js"
      },
      "react-native": {
        "types": "./dist/react-native/index.d.ts",
        "default": "./dist/react-native/index.js"
      },
      "import": {
        "types": "./dist/esm/index.d.ts",
        "default": "./dist/esm/index.js"
      },
      "require": {
        "types": "./dist/commonjs/index.d.ts",
        "default": "./dist/commonjs/index.js"
      }
    }
  }