meteatamel / genai-samples

A collection of GenAI samples
Apache License 2.0
2 stars 1 forks source link

Unable to authenticate using the provided authentication code in rest c# #21

Closed mehnoorsiddiqui closed 2 months ago

mehnoorsiddiqui commented 6 months ago

You have provided the code to authenticate to the Gemini api but this is not working at my end. I have set the service account credentials in the environment variable but it is still not working. Is there any other pre-requisite that I have to do so make this code executable?

private async static Task<string> SendRequest(string payload)
{
    GoogleCredential credential = GoogleCredential.GetApplicationDefault();
    var handler = credential.ToDelegatingHandler(new HttpClientHandler());
    using HttpClient httpClient = new(handler);

    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = await httpClient.PostAsync(EndpointUrl,
        new StringContent(payload, Encoding.UTF8, "application/json"));

    response.EnsureSuccessStatusCode();

    return await response.Content.ReadAsStringAsync();
}
meteatamel commented 6 months ago

Where exactly are you running this code (console, Cloud Function, Cloud Run)? Also, which sample are you trying exactly?

mehnoorsiddiqui commented 6 months ago

Thanks for responding.

I am trying out this code sample

I am running it locally in my visual studio

meteatamel commented 6 months ago

Have you done gcloud auth application-default login before running the sample?

mehnoorsiddiqui commented 6 months ago

No. Do I have to run it only once? As I will use this code in production. Isn't there any way I can do the authentication in the code without using this command?

meteatamel commented 6 months ago

If you're running locally, that's the easiest way to get auth working by running that gcloud command once. There are other ways, as explained here but I don't have samples for them. If running this code in GCP on Cloud Functions or Cloud Run, you don't need to do any of this.

I should also mention that, there's another version of this sample using the Vertex AI library: https://github.com/meteatamel/genai-samples/blob/main/vertexai/gemini/console/csharp/sdk/GenerateTextFromText.cs

This one handles auth under the covers and might be easier to run.

mehnoorsiddiqui commented 6 months ago

Thanks @meteatamel

I tried the Vertex AI library, but I am having a hard time figuring out how function calling is done through it. Previously I wrote my own SDK for gemini ai studio api and I was using the code below for function calling

Tools = new List<Tool>
 {
     new Tool
     {
         FunctionDeclarations = new List<FunctionDeclaration>
         {
             new FunctionDeclaration
             {
                 Name="get_relevant_endpoints",
                 Description="Retrieve Endpoint names that will solve what the user is asking in the question",
                 Parameters= new Schema
                 {
                     Properties = new
                     {
                         endpoint_name = new
                         {
                             Type = TypeEnum.ARRAY,
                             Items = new Schema
                             {
                                 Type = TypeEnum.STRING
                             }
                         }
                     },
                     Type = TypeEnum.OBJECT,

                     Required = new List<string>
                     {
                         "endpoint_name",
                     }
                 },
             },
         },
     }
 }

But now can't figure out how to pass the schema to the parameter property as it is read-only in the vertex ai library you mentioned.

meteatamel commented 6 months ago

You need to use this class somehow: https://cloud.google.com/dotnet/docs/reference/Google.Cloud.AIPlatform.V1/latest/Google.Cloud.AIPlatform.V1.FunctionDeclaration#Google_Cloud_AIPlatform_V1_FunctionDeclaration_Parameters

But I don't have a sample ready unfortunately.

If something is read-only, it usually means it's a list that you can't assign but you can add elements to that list. Not sure if this is the issue here.

I don't have time right now to look into this but if I figure it out later, I'll add a sample to the repo.

meteatamel commented 2 months ago

@mehnoorsiddiqui I have a function calling sample here now: https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/main/aiplatform/api/AIPlatform.Samples/FunctionCalling.cs

Closing this.