@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.