private async Task<bool> CheckGroundedness(string text, string questionText)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(endpoint);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var requestContent = new JObject
{
["domain"] = "Generic", // Update the domain to suit your use case
["task"] = "QnA", // Task updated for Q&A
["text"] = text, // The student's answer
["groundingSources"] = new JArray
{
"One hot day, a thirsty crow flew all over the fields looking for water. For a long time, he could not find any. He felt very weak, almost lost all hope. Suddenly, he saw a water jug below the tree. He flew straight down to see if there was any water inside. Yes, he could see some water inside the jug!\r\n\r\nThe crow tried to push his head into the jug. Sadly, he found that the neck of the jug was too narrow. Then he tried to push the jug to tilt for the water to flow out, but the jug was too heavy.The crow thought hard for a while. Then, looking around it, he saw some pebbles. He suddenly had a good idea. He started picking up the pebbles one by one, dropping each into the jug. As more and more pebbles filled the jug, the water level kept rising. Soon it was high enough for the crow to drink. His plan had worked!" // Grounding reference
},
["QnA"] = new JObject
{
["Query"] = questionText
},
["reasoning"] = false // Enable or disable reasoning as needed
};
HttpContent content = new StringContent(requestContent.ToString(), Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync("/contentsafety/text:detectGroundedness?api-version=2024-09-15-preview", content);
string jsonResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"API Response: {jsonResponse}");
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Check The Status Code: {response.StatusCode}");
//string jsonResponse = await response.Content.ReadAsStringAsync();
//Console.WriteLine($"API Response: {jsonResponse}");
var responseObject = JObject.Parse(jsonResponse);
// Check for the 'isGrounded' field explicitly
if (responseObject["ungroundedDetected"] != null && responseObject["ungroundedPercentage"] != null)
{
bool ungroundedDetected = (bool)responseObject["ungroundedDetected"];
double ungroundedPercentage = (double)responseObject["ungroundedPercentage"];
// If no ungrounded content is detected and the percentage is 0, return true
if (!ungroundedDetected && ungroundedPercentage == 0)
{
return true;
}
}
return false;
//string jsonResponse = await response.Content.ReadAsStringAsync();
// //return jsonResponse;
// return true;
}
else
{
string errorResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error: {response.StatusCode}");
Console.WriteLine($"Details: {errorResponse}");
//return $"Error: {response.StatusCode}";
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception occurred: {ex.Message}");
}
// Call Azure Content Safety API or another groundedness detection service
// Example pseudo-code:
// var response = await _contentSafetyService.EvaluateTextAsync(text);
// return response.IsGrounded;
//return true; // Placeholder logic
return false;
}