log moderation results

This commit is contained in:
Fraser
2023-08-22 02:09:52 -04:00
parent 413fec99a6
commit 7df037fbd5
+12 -7
View File
@@ -156,16 +156,21 @@ def talk_to_robot_internal(index, query: str, mode: str, history: List[Dict[str,
# 3. Run both the standalone query and the full prompt through
# moderation to see if it will be accepted by OpenAI's api
mod_res = openai.Moderation.create(
input = [
query,
'\n\n'.join([message["content"] for message in prompt]),
]
)
prompt_string = '\n\n'.join([message["content"] for message in prompt])
mod_res = openai.Moderation.create( input = [ query, prompt_string ])
if any(map(lambda x: x["flagged"], mod_res["results"])):
raise ValueError("This conversation was rejected by OpenAI's moderation filter. Sorry.")
# this is a biiig ask of a discord webhook - put most important
# info at start such that it's more likely to not be cut off
log('-' * 80)
log("MODERATION REJECTED")
log("MODERATION RESPONSE:\n\n" + json.dumps(mod_res["results"], indent=2))
log("REJECTED QUERY: " + query)
log("REJECTED PROMPT:\n\n" + prompt_string)
log('-' * 80)
raise ValueError("This conversation was rejected by OpenAI's moderation filter. Sorry.")
# 4. Count number of tokens left for completion (-50 for a buffer)
max_tokens_completion = NUM_TOKENS - sum([len(ENCODER.encode(message["content"]) + ENCODER.encode(message["role"])) for message in prompt]) - 50