How to use Azure Open AI's content Filtering in Typescript?

Ritvij Sharma 60 Reputation points
2023-10-30T07:03:59.3366667+00:00

Based on the documentation here: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter

If the input from the user or the output from the model is at a specific level of danger, it will be filtered based on the content filter set in azure open ai studio.

The documentation also mentions getting an error and a 400 response if this happens and content is filtered. But in the Typescript package, the ChatCompletions datatype does not have an error field. So how do I exactly detect an error when filtering is done? How do I let the user know that they need to rephrase their prompt to more appropriate language?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,915 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,782 questions
0 comments No comments
{count} votes

Accepted answer
  1. navba-MSFT 23,620 Reputation points Microsoft Employee
    2023-10-30T10:25:14.3933333+00:00

    @Ritvij Sharma Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    You want to know how to detect an error when filtering is done using Azure Open AI's content filtering in Typescript. Also you want to know how to let the user know that they need to rephrase their prompt to more appropriate language.

    To detect an error when filtering is done using Azure Open AI's content filtering in Typescript, you can check the response status code. If the response status code is 400, it means that the content has been filtered, and you can let the user know that they need to rephrase their prompt to more appropriate language.

    Approach 1:

    Regarding the ChatCompletions datatype not having an error field, In such cases, you can handle errors using a try...catch statement. The error details can be found in either error.response or error.message

    try {
        // Call to Azure OpenAI API
    } catch (error) {
        if (error.response && error.response.status === 400) {
            console.error(`Error: ${error.message}`);
            // Inform the user to rephrase their prompt
        }
    }
    

    In this code snippet, error.response.status is used to check if the status code of the error is 400. If it is, then an error message is logged and the user is informed to rephrase their prompt.

    See Error Handling in TypeScript here: https://platform.openai.com/docs/guides/images/language-specific-tips?context=node

    Approach 2:

    Alternatively, you can check the finish_reason field in the response from the Azure OpenAI API. If the finish_reason is set to content_filter , it signifies that some of the completion was filtered due to potentially harmful content. In such cases, you can send a prompt to the user asking them to rephrase their input in more appropriate language. More info here.

    try {
        // Call to Azure OpenAI API
        let response = /* your API call here */;
        if (response.finish_reason === 'content_filter') {
            // Inform the user to rephrase their prompt
        }
    } catch (error) {
        console.error(`Error: ${error.message}`);
    }
    

    Hope this helps.

    If you have any queries or concerns, please let me know. I would be happy to help.

    **
    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    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.