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

Tuned Model #51

Open
davidec1976 opened this issue Dec 27, 2024 · 1 comment
Open

Tuned Model #51

davidec1976 opened this issue Dec 27, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@davidec1976
Copy link

Hi, I'm trying to use "Mscc.Generative AI" library with APIkey work well, but win OAuth2 doesn't work.
I always get the exception on the CreateTunedModel call.


System.Net.Http.HttpRequestException
HResult=0x80131500
Message=Request failed
Request failed with Status Code: Unauthorized
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}

Source=Mscc.GenerativeAI
StackTrace:
at Mscc.GenerativeAI.GenerativeAIExtensions.d__17.MoveNext()
at Mscc.GenerativeAI.GenerativeModel.d__46.MoveNext()
at Program.<

$>d__0.MoveNext() in C:\Users\david\Desktop\Progetti\ResponderIA\ResponderIA\Program.cs:line 32


This is my code:
using System.Reflection;
using Mscc.GenerativeAI;
using System.Collections.Generic;

// I tried different codes:
// gcloud auth application-default print-access-token
// client secret code
var googleAI = new GoogleAI(apiKey: null,"");

var model = googleAI.GenerativeModel(model: Model.Gemini10Pro001);
model.ProjectId = "gen-lang-client-0860587592";

var parameters = new HyperParameters() { BatchSize = 2, LearningRate = 0.001f, EpochCount = 3 };
var dataset = new List
{
new() { TextInput = "What's magi number?", Output = "200" },
};
var request = new CreateTunedModelRequest(
Model.Gemini10Pro001,
"My Test Model",
dataset,
parameters);

var response = await model.CreateTunedModel(request);


Regards Davide

@jochenkirstaetter
Copy link
Contributor

jochenkirstaetter commented Dec 30, 2024

Hi @davidec1976

Thanks for the response and sample code to understand the issue better.
Referring to the README, you need to have gcloud CLI command line tool installed on the system running your C# code. Then one needs to enable and use OAuth for the Google AI as described here - https://ai.google.dev/gemini-api/docs/oauth - first. This provides the necessary scopes for the library to access the resources to tune a model.

After you set up OAuth in your project you can proceed using the AccessToken instead of the ApiKey. The initialization is described here: https://github.com/mscraftsman/generative-ai?tab=readme-ov-file#choose-an-api-and-authentication-mode

Google AI with OAuth. Use gcloud auth application-default print-access-token to get the access token.

using Mscc.GenerativeAI;
// Google AI with OAuth. Use `gcloud auth application-default print-access-token` to get the access token.
var googleAi = new GoogleAI(accessToken: "<your value>");
var model = googleAi.GenerativeModel(model: Model.Gemini10Pro001);
...

Your code var googleAI = new GoogleAI(apiKey: null,""); does not provide the access token and therefore your call is unauthenticated and returned as an HTTP 401. You have to specify an access token not an empty string.

Also, see the Troubleshooting section at the end of the README file.

In case that you are getting an HTTP 403 due to missing authorization you need to extend the scopes of your OAuth access token to include "https://www.googleapis.com/auth/generative-language.tuning" and enable it in the consent dialog when you log in. Refer to Set up application default credentials to specify the scopes.

image

There are also tests here in the repository regarding the creation and handling of tuned models.
See here to inspect the test Create_Tuned_Model.

I hope this helps.

Cheers, JoKi

@jochenkirstaetter jochenkirstaetter added the documentation Improvements or additions to documentation label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants