i am using the contententsafety api for groundedness detection to check the answer of a question wheather it is correct or not. but it always return status true

Intikhab Hussain Bhat 20 Reputation points
2025-01-21T13:56:41.5733333+00:00

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;

}     
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,094 questions
{count} votes

Accepted answer
  1. SriLakshmi C 2,490 Reputation points Microsoft Vendor
    2025-01-22T20:41:14.5533333+00:00

    Hello Intikhab Hussain Bhat,

    Greetings and Welcome to Microsoft Q&A! Thanks for posting the question.

    I understand that you're consistently receiving a status of "true" from the Azure Content Safety API for groundedness detection, here is few things to check and consider,

    • Grounding Reference Content in the groundingSources field should have sufficient variation and relevance to the text being evaluated; closely matching content may always result in a positive grounding detection.
    • Response Handling, ensure the API response is being parsed correctly, and log the complete response structure to confirm fields like ungroundedDetected and ungroundedPercentage are present and correctly interpreted.
    • Review the Grounding Sources to include diverse and specific examples to accurately simulate grounded and ungrounded scenarios.
    • Verify the Correct API Endpoint and Parameters to ensure that the configuration aligns with the API's intended behavior, such as using the latest API version and appropriate domain/task values.
    • Optimize API Configuration by testing alternative domains, enabling the reasoning parameter, and validating the request's overall structure to ensure accurate results.

    Also kindly refer this Use Groundedness detection.

    I Hope you understand. Do let me know if you have any further queries.

    Thank you!

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.