Search code examples
asp.netazureazure-openai

Token usage of Content Filtered messages in Azure OpenAI Services


When sending a message to a chat via GetChatCompletions as a response, I get a RequestFailedException. In the exception, I get an answer for which category content filter triggered, but nothing for how many tokens were used.   

So my questions is: Do messages or responses that triggered the Content Filter not use tokens? If they do use tokens, how many are they using, and where can I get this information? Is the Azure OpenAI Service using Content Safety?

I tried to parse the message from the exception, but it just doesn't have that kind of information.


Solution

  • To send a request to the OpenAI API to check whether a given message is likely to trigger the Content Filter.

    Here is the code:

        using System;
        using System.Net.Http;
        using System.Net.Http.Headers;
        using System.Threading.Tasks;
        
        class Program
        {
            static async Task Main()
            {
                string apiKey = "YOUR_OPENAI_API_KEY";  // Replace with your OpenAI API key
                string message = "message that is likely to trigger the Content Filter.";
        
                int? tokenUsage = await GetContentFilteredMessageTokenUsage(apiKey, message);
        
                if (tokenUsage != null)
                {
                    Console.WriteLine($"The message used {tokenUsage} tokens.");
                }
                else
                {
                    Console.WriteLine("The message did not trigger the Content Filter.");
                }
            }
        
            static async Task<int?> GetContentFilteredMessageTokenUsage(string apiKey, string message)
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
        
                    var payload = new
                    {
                        messages = new[]
                        {
                            new
                            {
                                role = "system",
                                content = "You are a helpful assistant."
                            },
                            new
                            {
                                role = "user",
                                content = message
                            }
                        }
                    };
        
                    var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(payload));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        
                    var response = await httpClient.PostAsync("https://api.openai.com/v1/chat/completions", content);
                    var responseContent = await response.Content.ReadAsStringAsync();
        
                    if (response.IsSuccessStatusCode)
                    {
                        dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
        
                        if (jsonResponse.message.role == "assistant" &&
                            jsonResponse.content_filtering.status == "failed")
                        {
                            return ((string)jsonResponse.message.content).Split(' ').Length;
                        }
                    }
        
                    return null;
                }
            }
        }
    

    Output enter image description here

    Message: "The message did not trigger the Content Filter," it means that the content filter did not detect any issues with the provided message, and the message is considered safe.

    When you use a message like Senstive Information, the content filter is likely to trigger, and the code will then report the number of tokens used.

    Answer you questions

    Do messages or responses that triggered the Content Filter not use tokens?

    Yes, messages or responses that triggered the Content Filter will still use tokens. For example, a simple message like "This is a message that is likely to trigger the Content Filter." will use fewer tokens than a more complex message like "I am writing a blog post about the OpenAI Content Filter and I am wondering if you can provide me with more information about how it works."

    If they do use tokens, how many are they using, and where can I get this information?

    To get the number of tokens used in a message, you can use the GetContentFilteredMessageTokenUsage. This Method will calculate the number of tokens used in the message by splitting the message content on whitespace.

    Is the Azure OpenAI Service using Content Safety?

    Yes, the Azure OpenAI Service is using Content Safety. Please refer to this Azure AI Content Safety MSDoc