mscraftsman / generative-ai

Gemini AI SDK for .NET and ASP.NET Core enables developers to use Google's state-of-the-art generative AI models to build AI-powered features and applications.
https://mscraftsman.github.io/generative-ai/
Apache License 2.0
49 stars 9 forks source link

Exception thrown in Google App Engine #26

Open sebastianbk opened 5 months ago

sebastianbk commented 5 months ago

When deploying an app using this library to Google App Engine, the exception below is thrown.

This happens despite the fact that the application default credentials indeed are available and are being consumed without any issues by Google's own SDKs in .NET.

System.Exception: OS error while executing 'gcloud auth application-default print-access-token': An error occurred trying to start process 'gcloud' with working directory '/app'. No such file or directory ---> System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'gcloud' with working directory '/app'. No such file or directory

at System.Diagnostics.Process.ForkAndExecProcess
at System.Diagnostics.Process.StartCore
at Mscc.GenerativeAI.GenerativeModel.RunExternalExe
at Mscc.GenerativeAI.GenerativeModel.RunExternalExe
at Mscc.GenerativeAI.GenerativeModel.GetAccessTokenFromAdc
sebastianbk commented 5 months ago

I was able to remediate this issue with the following code:

// Get access token
var credential = await GoogleCredential.GetApplicationDefaultAsync();
var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
Environment.SetEnvironmentVariable("GOOGLE_ACCESS_TOKEN", token);

// Create model
var model = _vertexAi.GenerativeModel(Model.GeminiPro);
model.AccessToken = token;

Though, it's not ideal. Could you use the GoogleCredential class instead of using the CLI?

doggy8088 commented 5 months ago

I agreed that bundled with Google.Apis.Auth is a better solution.

Here is the current implementation:

https://github.com/mscraftsman/generative-ai/blob/96a4602796a6e2a200b26f6824910f35601211c3/src/Mscc.GenerativeAI/GenerativeModel.cs#L1543-L1560

jochenkirstaetter commented 5 months ago

Hello @sebastianbk

Thanks for reporting the issue.

Please see the additional package Mscc.GenerativeAI.Google which uses the Google Client Libraries for .NET under the hood to handle the access token required to authenticate. It's a wrapper package for the main Mscc.GenerativeAI package and it uses the GoogleCredential class already.

That should solve the issue already. And I have to improve the README.md to refer to the second package better.

I'm going to look into this to see myself how this could be improved

Cheers, JoKi

PS: Are you using the latest package already? I added some improvements to avoid the exception... Anyways, going to check.