zhiminy commited on
Commit
a5c143f
·
1 Parent(s): 3b4c3ae

add guardrail for SE-related tasks

Browse files
Files changed (2) hide show
  1. app.py +83 -3
  2. requirements.txt +1 -2
app.py CHANGED
@@ -188,7 +188,7 @@ def fetch_url_content(url):
188
  return fetch_huggingface_content(url)
189
  except Exception as e:
190
  print(f"Error fetching URL content: {e}")
191
- return None
192
 
193
 
194
  # Truncate prompt
@@ -536,6 +536,8 @@ with gr.Blocks() as app:
536
  "Sign in with Hugging Face", elem_id="oauth-button"
537
  )
538
 
 
 
539
  # NEW: Add a textbox for the repository URL above the user prompt
540
  repo_url = gr.Textbox(
541
  show_label=False,
@@ -642,13 +644,90 @@ with gr.Blocks() as app:
642
  ],
643
  )
644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645
  # Function to update model titles and responses
646
  def update_model_titles_and_responses(
647
- repo_info, user_input, models_state, conversation_state
648
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  # Combine repo-related information (if any) and user query into one prompt.
650
  combined_user_input = (
651
- f"Repo-related Information: {fetch_url_content(repo_info)}\n\n{user_input}"
652
  if repo_info
653
  else user_input
654
  )
@@ -810,6 +889,7 @@ with gr.Blocks() as app:
810
  fn=update_model_titles_and_responses,
811
  inputs=[repo_url, shared_input, models_state, conversation_state],
812
  outputs=[
 
813
  shared_input,
814
  repo_url,
815
  user_prompt_md,
 
188
  return fetch_huggingface_content(url)
189
  except Exception as e:
190
  print(f"Error fetching URL content: {e}")
191
+ return ''
192
 
193
 
194
  # Truncate prompt
 
536
  "Sign in with Hugging Face", elem_id="oauth-button"
537
  )
538
 
539
+ guardrail_message = gr.Markdown("", visible=False, elem_id="guardrail-message")
540
+
541
  # NEW: Add a textbox for the repository URL above the user prompt
542
  repo_url = gr.Textbox(
543
  show_label=False,
 
644
  ],
645
  )
646
 
647
+ def guardrail_check_se_relevance(user_prompt):
648
+ """
649
+ Use GPT-3.5-turbo to check if the user_prompt is SE-related.
650
+ Return True if it is SE-related, otherwise False.
651
+ """
652
+ # Example instructions for classification — adjust to your needs
653
+ system_message = {
654
+ "role": "system",
655
+ "content": (
656
+ "You are a classifier that decides if a user's question is relevant to software engineering. "
657
+ "If the question is about software engineering concepts, tools, processes, or code, respond with 'Yes'. "
658
+ "Otherwise, respond with 'No'."
659
+ ),
660
+ }
661
+ user_message = {"role": "user", "content": user_prompt}
662
+
663
+ try:
664
+ # Make the chat completion call
665
+ response = openai_client.chat.completions.create(
666
+ model="gpt-3.5-turbo", messages=[system_message, user_message]
667
+ )
668
+ classification = response.choices[0].message.content.strip().lower()
669
+ # Check if GPT-3.5-turbo responded with 'Yes'
670
+ return classification.lower().startswith("yes")
671
+ except Exception as e:
672
+ print(f"Guardrail check failed: {e}")
673
+ # If there's an error, you might decide to fail open (allow) or fail closed (block).
674
+ # Here we default to fail open, but you can change as needed.
675
+ return True
676
+
677
  # Function to update model titles and responses
678
  def update_model_titles_and_responses(
679
+ repo_url, user_input, models_state, conversation_state
680
  ):
681
+ # Guardrail check first
682
+ if not guardrail_check_se_relevance(user_input):
683
+ # Return updates to show the guardrail message,
684
+ # hide everything else or revert to original state
685
+ return (
686
+ # guardrail_message
687
+ gr.update(
688
+ value="### Oops! Try asking something about software engineering. Thanks!",
689
+ visible=True,
690
+ ),
691
+ # shared_input
692
+ gr.update(value="", visible=True),
693
+ # repo_url
694
+ gr.update(value="", visible=True),
695
+ # user_prompt_md
696
+ gr.update(value="", visible=False),
697
+ # response_a_title
698
+ gr.update(value="", visible=False),
699
+ # response_b_title
700
+ gr.update(value="", visible=False),
701
+ # response_a
702
+ gr.update(value=""),
703
+ # response_b
704
+ gr.update(value=""),
705
+ # multi_round_inputs
706
+ gr.update(visible=False),
707
+ # vote_panel
708
+ gr.update(visible=False),
709
+ # send_first
710
+ gr.update(visible=True, interactive=True),
711
+ # feedback
712
+ gr.update(interactive=False),
713
+ # models_state
714
+ models_state,
715
+ # conversation_state
716
+ conversation_state,
717
+ # timeout_popup
718
+ gr.update(visible=False),
719
+ # model_a_send
720
+ gr.update(interactive=False),
721
+ # model_b_send
722
+ gr.update(interactive=False),
723
+ # thanks_message
724
+ gr.update(visible=False),
725
+ )
726
+
727
+ repo_info = fetch_url_content(repo_url)
728
  # Combine repo-related information (if any) and user query into one prompt.
729
  combined_user_input = (
730
+ f"Repo-related Information: {repo_info}\n\n{user_input}"
731
  if repo_info
732
  else user_input
733
  )
 
889
  fn=update_model_titles_and_responses,
890
  inputs=[repo_url, shared_input, models_state, conversation_state],
891
  outputs=[
892
+ guardrail_message,
893
  shared_input,
894
  repo_url,
895
  user_prompt_md,
requirements.txt CHANGED
@@ -5,5 +5,4 @@ huggingface_hub
5
  openai
6
  PyGithub
7
  python-dotenv
8
- python-gitlab
9
- vertexai
 
5
  openai
6
  PyGithub
7
  python-dotenv
8
+ python-gitlab