zlmqi commited on
Commit
64da101
1 Parent(s): 1a65ea9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -28,10 +28,20 @@ class Chatbot:
28
 
29
  def create_bot(input):
30
  bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
31
-
 
32
  if input:
33
- response = bot.generate_response(input)
34
- output = response['content']
 
 
 
 
 
 
 
 
 
35
 
36
  return output
37
 
 
28
 
29
  def create_bot(input):
30
  bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
31
+
32
+ # use moderations endpoint to check input
33
  if input:
34
+ client = OpenAI()
35
+ response_mod = client.moderations.create(input=input)
36
+ response_dict = response_mod.model_dump()
37
+ flagged = response_dict['results'][0]['flagged']
38
+ print("Flagged:", flagged)
39
+
40
+ if not flagged:
41
+ response_bot = bot.generate_response(input)
42
+ output = response_bot['content']
43
+ else:
44
+ output = "Invalid request."
45
 
46
  return output
47