Last active
May 10, 2022 16:51
-
-
Save aliyeysides/f434831f82f22111e12fcdd406ac6de8 to your computer and use it in GitHub Desktop.
OpenAI Ruby content filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruby/openai' | |
CLIENT = OpenAI::Client.new | |
def content_filter(prompt) | |
toxic_threshold = -0.355 # make sure required threshold hasn't changed https://beta.openai.com/docs/engines/content-filter | |
wrapped_prompt = "<|endoftext>#{prompt}\n--\nLabel:" | |
response = CLIENT.completions(engine: 'content-filter-alpha', | |
parameters: { prompt: wrapped_prompt, | |
temperature: 0, max_tokens: 1, top_p: 0, logprobs: 10 }).parsed_response | |
output_label = response['choices'][0]['text'] | |
if output_label == '2' | |
logprobs = response['choices'][0]['logprobs']['top_logprobs'][0] | |
if logprobs['2'] < toxic_threshold | |
logprob_0 = logprobs['0'] | |
logprob_1 = logprobs['1'] | |
if logprob_0 && logprob_1 | |
output_label = if logprob_0 >= logprob_1 | |
'0' | |
else | |
'1' | |
end | |
elsif logprob_0 && !logprob_1 | |
output_label = '0' | |
elsif !logprob_0 && logprob_1 | |
output_label = '1' | |
end | |
end | |
end | |
output_label = '2' unless %w[0 1 2].include?(output_label) | |
output_label | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment