From 7df037fbd5ec26886abd942318dc2bcbad6fb1e0 Mon Sep 17 00:00:00 2001 From: Fraser Date: Tue, 22 Aug 2023 02:09:52 -0400 Subject: [PATCH] log moderation results --- api/chat.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/api/chat.py b/api/chat.py index 789a2b2..e8be035 100644 --- a/api/chat.py +++ b/api/chat.py @@ -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