Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown in Google App Engine #26

Closed
sebastianbk opened this issue Apr 27, 2024 · 4 comments
Closed

Exception thrown in Google App Engine #26

sebastianbk opened this issue Apr 27, 2024 · 4 comments
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request

Comments

@sebastianbk
Copy link

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
Copy link
Author

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
Copy link
Contributor

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

Here is the current implementation:

/// <summary>
/// This method uses the gcloud command-line tool to retrieve an access token from the Application Default Credentials (ADC).
/// It is specific to Google Cloud Platform and allows easy authentication with the Gemini API on Google Cloud.
/// Reference: https://cloud.google.com/docs/authentication
/// </summary>
/// <returns>The access token.</returns>
private string GetAccessTokenFromAdc()
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
return RunExternalExe("cmd.exe", "/c gcloud auth application-default print-access-token").TrimEnd();
}
else
{
return RunExternalExe("gcloud", "auth application-default print-access-token").TrimEnd();
}
}

@jochenkirstaetter
Copy link
Contributor

jochenkirstaetter commented May 3, 2024

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.

@jochenkirstaetter jochenkirstaetter added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request labels May 3, 2024
@jochenkirstaetter jochenkirstaetter self-assigned this May 3, 2024
@jochenkirstaetter
Copy link
Contributor

Hi, this has been improved and (hopefully) resolved in release v1.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants