srcnalt / OpenAI-Unity

An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
MIT License
672 stars 150 forks source link

Auth Error on WebGL #45

Closed ChrisKlingler closed 1 year ago

ChrisKlingler commented 1 year ago

Within the editor things communicate fine with OpenAI but when building and running a WebGL build I get the following response:

{ "error": { "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.", "type": "invalid_request_error", "param": null, "code": null } }

Why is it the apiKey headers are not being passed on WebGL builds?

srcnalt commented 1 year ago

Hi @ChrisKlingler, WebGL app will not be able to reach the key through your local files. Alternatively, you can provide the API key to your OpenAiAPI ctor as a parameter to make it work. I made a video for ht here https://www.youtube.com/watch?v=DjX8mjBCMcg

ChrisKlingler commented 1 year ago

Thanks for sharing @srcnalt - I don't know if this helps me as I am just trying to do ChatCompletions on WebGL and am already passing the API Key as a parameter like so:

openai = new OpenAIApi(currentEnvironment.MyApiKey);

I've made sure the environment variables are coming down and being passed successfully to the OpenAIApi Configuration, but somehow the headers must be getting wiped out on WebGL builds with ChatCompletion. Right now trying to run the ChatGPT Sample by injecting the API Key in the ChatGPT.cs script is having the same behavior.

Screen Shot 2023-04-24 at 8 01 42 AM Screen Shot 2023-04-24 at 8 02 10 AM
srcnalt commented 1 year ago

Ah, this is interesting, it is a direct server error. I wonder if this could be related to firewalls etc. Could you tell me which version of Unity are you using so I can try to replicate it, this never happened to me before.

ChrisKlingler commented 1 year ago

Sure. I was using the latest version of Unity until I ran into an issue with other libraries I need to function for this project. They recommend I use 2020 to get those issues to work. Currently, I am running 2020.3.47f1 Personal

srcnalt commented 1 year ago

So if I understand correctly, this works in 2020 but not in 2022?

ChrisKlingler commented 1 year ago

I have not checked in 2022. So far only in 2020

ChrisKlingler commented 1 year ago

It may be helpful to know that this also appears to be a problem when running locally with a built and run WebGL build on 2020

Screen Shot 2023-04-24 at 11 17 28 AM
srcnalt commented 1 year ago

Building from Unity 2020.3.0f1 It works all right for me. I suspect it might be a network/firewall-related issue.

image
ChrisKlingler commented 1 year ago

Got it. I'll take another look at it tomorrow. Thanks

MaximilianCroissant commented 1 year ago

Is this solved? I run into the same issue. For test purposes the API key is hardcoded and everything works in the editor. But the WebGL build has the authentication error. I've tried 2020.3.0f1, 2021.3.22f1, and 2022.2.19f1.