I am am unable to connect to Gemini / Vertex AI. I have tried two different code suggestions made by Gemini chat (see below). I am quite sure the following information used in the URL is correct (there certainly are no typos):
API key
project id
I am less certain regarding the location, there is none shown in the console, and Gemini chat says there isn't a definitive location for my project. As suggested by the bot I requested a list via Cloud SDK for my project id, and tried several locations from the resulting list. The location does not seem to make a difference to the responses though.
The Gemini and Vertex AI APIs are enabled for the project. I also have user roles "owner" and "Vertex AI user" defined in the Cloud console, and there are no restrictions on the API key. Also the environment variable GOOGLE_APPLICATION_CREDENTIALS has been set with the appropriate information.
According to the Gemini chat bot, the generated URL should work "with high probability". As far as I can see, the conditions listed by the bot are also fulfilled, but obviously something is amiss.
Here are the two code versions (although both use the same URL, the HTTP error is different):
The following variables are the same for both:
private static readonly string projectId = "xxxx";
private static readonly string location = "europe-west1"; // one of several tried
private static readonly string geminiApiKey = "xxxx";
private static readonly string url = $"https://{location}-aiplatform.googleapis.com/v1/projects/{projectId}/locations/{location}/publishers/google/models/gemini-pro:generateContent";
// attempt 1 - results in HTTP 401 "unauthorised"
var content = new StringContent(CreateTextRequestBody(text), Encoding.UTF8, "application/json");
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", geminiApiKey);
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
// Parse the JSON response to get the generated text
return responseContent;
}
else
{
throw new Exception($"Gemini request error: {response.StatusCode} {response.ReasonPhrase}");
}
// attempt 2 - results in HTTP 404 "Bad gRPC response."
var instance = new Google.Protobuf.WellKnownTypes.Value
{
StructValue = new Struct
{
Fields =
{
{ "input_text", Google.Protobuf.WellKnownTypes.Value.ForString("Hello, world!") }
}
}
};
// Convert instance to a list of values
var instances = new List<Google.Protobuf.WellKnownTypes.Value> { instance };
// Optional: Additional parameters for prediction
var parameters = new Google.Protobuf.WellKnownTypes.Value
{
StructValue = new Struct
{
Fields =
{
{ "param_key", Google.Protobuf.WellKnownTypes.Value.ForString("param_value") }
}
}
};
try
{
// Making the predict request
PredictResponse response = predictionClient.Predict(url, instances, parameters);
// Print the response
foreach (var prediction in response.Predictions)
{
Debug.WriteLine($"Prediction: {prediction}");
}
}
catch (Grpc.Core.RpcException e)
{
Debug.WriteLine($"RPC failed: {e.Status}");
}
I have no idea what else to try.
There are two ways to connect (call) Gemini model APIs: Google AI or GCP Vertex AI. Please see this discussion for detail on when to use which methods and their difference. Google AI uses API Key as authentication credential while Vertex AI uses GCP Service account. Your description and code seem mixing the two approaches (you don't need API key when invoking Vertex AI endpoint).
Since you have GCP project configured, you can use Vertex AI C# Client libraries to connect to Gemini API. Here is detailed instruction. tl;dr:
And here is the sample code snippet:
var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
GenerationConfig = new GenerationConfig
{
Temperature = 0.4f,
TopP = 1,
TopK = 32,
MaxOutputTokens = 2048
},
Contents =
{
new Content
{
Role = "USER",
Parts =
{
new Part { Text = "What's in this photo?" },
new Part { FileData = new() { MimeType = "image/png", FileUri = "gs://generativeai-downloads/images/scones.jpg" } }
}
}
}
};
// Make the request, returning a streaming response
using PredictionServiceClient.StreamGenerateContentStream response = predictionServiceClient.StreamGenerateContent(generateContentRequest);
Vertex AI Gemini API is also exposed as REST API as documented here. However, it is suggested to use Client library first.