Skip to content

Commit

Permalink
log moderation results
Browse files Browse the repository at this point in the history
  • Loading branch information
FraserLee committed Aug 22, 2023
1 parent 413fec9 commit 7df037f
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7df037f

Please sign in to comment.