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?