题意:在Azure OpenAI服务中,内容过滤消息的令牌使用
问题背景:
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.
在通过 GetChatCompletions 发送消息到聊天作为响应时,我遇到了 RequestFailedException。在异常中,我得到了触发内容过滤器的类别信息,但没有关于使用了多少令牌的任何信息。
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?
所以我的问题是:触发内容过滤器的消息或响应不使用令牌吗?如果它们确实使用令牌,那么它们使用了多少,我可以在哪里获取这些信息?Azure OpenAI服务是否使用内容安全功能?
I tried to parse the message from the exception, but it just doesn't have that kind of information.
我尝试从异常中解析消息,但它就是没有那种信息。
问题解决:
To send a request to the OpenAI API to check whether a given message is likely to trigger the Content Filter.
向OpenAI API发送请求,以检查给定的消息是否可能触发内容过滤器。
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;
}
}
}
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."
是的,触发内容过滤器的消息或响应仍然会使用令牌。例如,像“这是一个很可能触发内容过滤器的消息。”这样的简单消息将比像“我正在写一篇关于OpenAI内容过滤器的博客文章,我想知道你是否能为我提供更多关于它是如何工作的信息。”这样的更复杂消息使用更少的令牌。
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.
要获取消息中使用的令牌数量,您可以使用GetContentFilteredMessageTokenUsage
方法。此方法将通过按空白字符拆分消息内容来计算消息中使用的令牌数量。
Is the Azure OpenAI Service using Content Safety?
Yes, the Azure OpenAI Service is using Content Safety. Please refer to this
是的,Azure OpenAI服务正在使用内容安全。请参考这个
Azure AI Content Safety MSDoc