-
Notifications
You must be signed in to change notification settings - Fork 4
/
chatgpt_moderation.py
79 lines (64 loc) · 3.25 KB
/
chatgpt_moderation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
This is a test script that provides access to OpenAI's ChatGPT model.
It creates a function chatgpt_moderation to check whether any user input violates OpenAI's content policy. Where a
violation is identified, the user input should not be processed for the text completion or image generation functions.
The reason for the violation will be output on the screen.
A full response can also output as a JSON file that contains additional metadata.
Functions:
chatgpt_moderation(prompt, model, output_to_file) -> tuple
"""
# Import os, dotenv, openai, json, datetime
import os
from dotenv import load_dotenv
import openai
import json
from datetime import datetime
# Load the API key for OpenAI
load_dotenv('openai_credentials.env')
openai.api_key = os.getenv("OPENAI_API_KEY")
# Create a function to input text and set the moderation model
def chatgpt_moderation(prompt, model="text-moderation-stable", output_to_file=False):
"""
This function checks whether any user input violates OpenAI's content policy
:param prompt: the input to be checked
:param model: the content moderation model being used
:param output_to_file: output a JSON file with the results
:return: (bool, list)
"""
response_raw = openai.Moderation.create(input=prompt, model=model)
# Convert the Completion response from an OpenAIObject to a dictionary type so can convert to JSON later
response_raw_dict_all = response_raw.__dict__
response_previous = response_raw_dict_all['_previous']
response_ms = {key: response_raw_dict_all[key] for key in response_raw_dict_all.keys() & {'_response_ms'}}
response_raw_dict = response_previous | response_ms
# Create the UNIX EPOCH time as it is not generated in the moderation output like in completions or images
unix_epoch_time_gmt = int(datetime.now().timestamp())
# Create a dictionary for the UNIX EPOCH time so can convert to JSON later
created_dict = {"created": unix_epoch_time_gmt}
# Add the model runtime settings to a nested dictionary type so can convert to JSON later
request_settings = {
"parameters": [
{
"input": prompt,
"model": model
}
]
}
# Merge the two dictionaries together to form the amended response output
response_dict = response_raw_dict | request_settings | created_dict
# Convert response dictionary to valid JSON format with some PrettyPrint indent
# response = json.dumps(response_dict, indent=4)
# Output to JSON file using the ID as the filename
if output_to_file is True:
with open("output_chatgpt_moderation/" + response_raw['id'] + ".json", "w") as outfile:
json.dump(response_dict, outfile, indent=4)
# Get the violation status - does the input breach the OpenAI T&Cs - True means a breach.
flagged_status = response_raw['results'][0]['flagged']
if flagged_status is True:
reason_categories_dict = response_raw['results'][0]['categories']
breach_categories_list = [key for key, value in reason_categories_dict.items() if value is True]
response_tuple = (True, breach_categories_list)
else:
response_tuple = (False, [])
# Return the amended ChatGPT moderation details in JSON format
return response_tuple