Umang Chaudhry commited on
Commit
a12fe73
1 Parent(s): 91bc884

Merge pull request #102 from vanderbilt-data-science/chatgpt-background-info

Browse files
free_speech_app/FreeSpeechPromptsResponses.py CHANGED
@@ -1,7 +1,7 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/free-speech-prompts.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['DEAFULT_PROMPT_TEMPLATE_TEXT', 'query_retriever', 'generate_custom_prompt', 'get_chat_model_response',
5
  'generate_custom_response', 'regenerate_custom_response']
6
 
7
  # %% ../nbs/free-speech-prompts.ipynb 5
@@ -61,11 +61,24 @@ def query_retriever(db, query, num_results = 3):
61
  return docs_as_one_string
62
 
63
  # %% ../nbs/free-speech-prompts.ipynb 10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None):
65
 
66
  # Get database and query retriever
67
- ####
68
- background_db = DeepLake(dataset_path="hub://vanderbilt-dsi/hatespeech-background", embedding = OpenAIEmbeddings())
69
  sources_db = DeepLake(dataset_path="hub://vanderbilt-dsi/counterspeech-resources", embedding = OpenAIEmbeddings())
70
 
71
  # Use defaults in the case of None
@@ -80,7 +93,9 @@ def generate_custom_prompt(original_post, principles=None, writing_style=None, w
80
  word_limit="an infinite amount of"
81
 
82
  retriever_query = original_post
83
- background_info = query_retriever(background_db, retriever_query)
 
 
84
  sources = query_retriever(sources_db, retriever_query)
85
 
86
  # Fill the prompt
@@ -88,14 +103,7 @@ def generate_custom_prompt(original_post, principles=None, writing_style=None, w
88
 
89
  return filled_prompt, background_info, sources
90
 
91
- # %% ../nbs/free-speech-prompts.ipynb 11
92
- def get_chat_model_response(mdl, input_prompt):
93
-
94
- messages = [HumanMessage(content=input_prompt)]
95
-
96
- return mdl(messages)
97
-
98
- # %% ../nbs/free-speech-prompts.ipynb 12
99
  def generate_custom_response(original_post, chat_mdl, principles=None, writing_style=None, word_limit=None):
100
 
101
  # create customized prompt
@@ -106,7 +114,7 @@ def generate_custom_response(original_post, chat_mdl, principles=None, writing_s
106
 
107
  return draft_response, background_info, sources
108
 
109
- # %% ../nbs/free-speech-prompts.ipynb 13
110
  def regenerate_custom_response(chat_mdl, regenerate_prompt, draft_response):
111
 
112
  # create customized prompt
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/free-speech-prompts.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['DEAFULT_PROMPT_TEMPLATE_TEXT', 'query_retriever', 'get_chat_model_response', 'call_chatGPT', 'generate_custom_prompt',
5
  'generate_custom_response', 'regenerate_custom_response']
6
 
7
  # %% ../nbs/free-speech-prompts.ipynb 5
 
61
  return docs_as_one_string
62
 
63
  # %% ../nbs/free-speech-prompts.ipynb 10
64
+ def get_chat_model_response(mdl, input_prompt):
65
+
66
+ messages = [HumanMessage(content=input_prompt)]
67
+
68
+ return mdl(messages)
69
+
70
+ # %% ../nbs/free-speech-prompts.ipynb 11
71
+ def call_chatGPT(query):
72
+ chat_model = ChatOpenAI(model_name = 'gpt-4', temperature=0.1)
73
+ response = get_chat_model_response(chat_model, query)
74
+
75
+ return response.content
76
+
77
+ # %% ../nbs/free-speech-prompts.ipynb 12
78
  def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None):
79
 
80
  # Get database and query retriever
81
+
 
82
  sources_db = DeepLake(dataset_path="hub://vanderbilt-dsi/counterspeech-resources", embedding = OpenAIEmbeddings())
83
 
84
  # Use defaults in the case of None
 
93
  word_limit="an infinite amount of"
94
 
95
  retriever_query = original_post
96
+ background_query = "I found this post which I suspect to be hate speech. Can you please explain the background information I need to understand this post? Keep in mind terms/codes commonly used by extremist/hate groups such as Nazis, TERFS, alt-right, etc."
97
+ background_query = background_query + "\n" + original_post
98
+ background_info = call_chatGPT(background_query)
99
  sources = query_retriever(sources_db, retriever_query)
100
 
101
  # Fill the prompt
 
103
 
104
  return filled_prompt, background_info, sources
105
 
106
+ # %% ../nbs/free-speech-prompts.ipynb 13
 
 
 
 
 
 
 
107
  def generate_custom_response(original_post, chat_mdl, principles=None, writing_style=None, word_limit=None):
108
 
109
  # create customized prompt
 
114
 
115
  return draft_response, background_info, sources
116
 
117
+ # %% ../nbs/free-speech-prompts.ipynb 14
118
  def regenerate_custom_response(chat_mdl, regenerate_prompt, draft_response):
119
 
120
  # create customized prompt