Hi, im having issues using gemini-pro in a Csharp code

down there ill put the entire code:
{ private async void Ask_Click(object sender, RoutedEventArgs e)
{
string userQuestion = What.Text;
if (!string.IsNullOrEmpty(userQuestion))
{
string predefinedPrompt = “Be nice while answering.”;
string combinedPrompt = $“{predefinedPrompt}\n\nUser: {userQuestion}”;
string response = await GetGeminiResponse(combinedPrompt);
Response1.Text = response;
}
else
{
MessageBox.Show(“Please enter a question.”);
}
}

public static async Task GetGeminiResponse(string prompt)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add(“Authorization”, $“Bearer {apiKey}”);
var requestBody = new
{
model = “gemini-pro”,
prompt = prompt,
max_tokens = 100
};
var content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, “application/json”);
var response = await client.PostAsync(endpoint, content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response from API: " + responseString);
dynamic responseJson = JsonConvert.DeserializeObject(responseString)
if (responseJson != null && responseJson.choices != null && responseJson.choices.Count > 0)
{
return responseJson.choices[0].text.ToString();
}
else
{
Console.WriteLine(“Invalid response structure or empty response.”);
return “API response is invalid or empty.”;
}
}
}
so basically you write down your question in a textbox called “What” and press a button called “Ask” to submit it and the result will show in another textbox called “Response1”
but the response is a error because it was null… the api key is a working one but im not really sure about the endpoint URL, in visual studio the code has 0 errors but i dont know how to fix this, can someone help me?

Welcome to the forums!

It is a little difficult to read your code - it would be great if you could go edit it so all the code is in a code formatting block.

That said, a quick glance shows at least a few errors:

  • I think the endpoint is correct. Sort of. But it looks like you’re specifying the AQA model, which is probably not what you’re looking for.
  • If you are using an API Key, you don’t include this in an Authorization header. It is typically attached either in the URL with ?key=api_key_goes_here although it can be done with a header as well. (I think the x-goog-api-key header, but that is off the top of my head.)
  • The format for requestBody is… completely wrong.
    • Out of curiousity, where did you get that format?
    • If you use AI Studio and “Show code” and then select “cURL”, you can see how the JSON needs to be formatted.
    • You can also take a look at the API docs to see the format.
  • This would also suggest you’re not capturing errors from the server. Which you may want to do.

While I’m not a C# programmer, my undersanding is that there are several .NET libraries out there for Gemini. Is there a reason you’re not using one of them?

1 Like

Hi, thanks for your help, that code is the only thing that seems reasonable for me and it was the best i can do, as you may notice i dont know what a AQA model really is and i didnt really find useful libraries because as soon i put them together i make a mess, this is my first time using this nugget pack, all out from that i really appreciate your help and tips, thanks!

I’m not familiar with .NET, but I did find this library which seems to fit the bill and is actively maintained.