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
43 stars 8 forks source link

Application Default Credentials (ADC) has been loaded automatically even I use API Key auth. #9

Open doggy8088 opened 4 months ago

doggy8088 commented 4 months ago

When I'm using a API Key authentication on Google AI Gemini API, the library still load my ADC automatically.

This might lead some issues. (Maybe) I'm not sure.

image

Here is my test code:

async Task Main()
{
    var model = new GenerativeModel(apiKey: Util.GetPassword("GEMINI_API_KEY"), model: Model.GeminiPro);

    var prompt = "I love Taipei. Give me a 15 words summary about it.";

    model.Uncapsulate().Dump();

    var response = await model.GenerateContent(prompt);

    response.Dump();
}

LINQPad Query: https://share.linqpad.net/ol86v9he.linq

jochenkirstaetter commented 4 months ago

Yes, the application default credentials (ADC) are currently loaded in the default constructor. However, there is no AccessToken requested when using an API key. In case that you're using the envVar GOOGLE_ACCESS_TOKEN its value gets loaded but not necessarily refreshed or used to make the API call..

It's a bit of a small culprit given that Google AI supports both API key and OAuth/ADC whereas Vertex AI works with OAuth/ADC only.

In general, I thought that the three constructors should handle it sufficiently. Following your suggestion to introduce a GoogleAI type it shall be even more clear. For future releases, the currently public constructor accepting apiKey should be internal and closing access to it. This means that the expected way would be as you suggested.

using Mscc.GenerativeAI;
// Google AI with an API key
var googleAI = new GoogleAI(apiKey: "your API key");
var model = googleAI.GenerativeModel(model: Model.GeminiPro);

// Original approach, still valid. Is going to disappear...
// var model = new GenerativeModel(apiKey: "your API key", model: Model.GeminiPro);

Sorry, although it has been implemented originally the other way I'm getting more and more accustomed to the newer approach of using the GoogleAI type to create the model object instead of public constructor.

What's your thought?

jochenkirstaetter commented 4 months ago

Using this approach would open the doors to introduce an interface IGenerativeAI which is implemented by both VertexAI and GoogleAI classes. This should streamline the use of Gemini across both API endpoints.

jochenkirstaetter commented 4 months ago

The interface IGenerativeAI has been added in release 0.9.1

doggy8088 commented 4 months ago

Sorry, although it has been implemented originally the other way I'm getting more and more accustomed to the newer approach of using the GoogleAI type to create the model object instead of public constructor.

Support for newer approach is absolutely fine for me. There is no need to keep "backward-compatibility". It because there are almost no one using this library. It probably only less than 10 people. ha .. 😅