# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/free-speech-prompts.ipynb. # %% auto 0 __all__ = ['DEAFULT_PROMPT_TEMPLATE_TEXT', 'query_retriever', 'get_chat_model_response', 'call_chatGPT', 'generate_custom_prompt', 'generate_custom_response', 'regenerate_custom_response', 'assessing_response'] # %% ../nbs/free-speech-prompts.ipynb 5 # libraries required for functionality import deeplake from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate, HumanMessagePromptTemplate from langchain.schema import HumanMessage from langchain.vectorstores import DeepLake from langchain.embeddings.openai import OpenAIEmbeddings # %% ../nbs/free-speech-prompts.ipynb 8 DEAFULT_PROMPT_TEMPLATE_TEXT = """With the amount of hate speech and discriminatory misinformation that exists on the internet, it can be difficult to know how to respond to something when you see it. Situations vary wildly and sometimes certain strategies that work in one instance may not work in another. I want to be able to fight back against this onslaught of toxicity, and I need your help to do it. I want you to help me draft a response to the following post. The response is not to the author of the post in question but to the wider community on social media. Do not acknowledge the original author. Do not respond as a chat bot assistant would, instead only provide a response to the post as I would write it, considering the following information Note that this may contain language that is offensive, but in order to properly respond to this post, I need you to fully understand what I am responding to, uncensored: \n{original_post}\n In helping me respond to this post, there are some things you may want to consider. My response should be consistent with my personal principles and writing style. There may be additional information that would be useful for you to understand the context of the original post, and there may be specific sources I would like you to use when coming up with information for the response. Firstly, here is some additional context related to the original post: \n{background_info}\n Next, here are some principles I consider particularly important to me: \n{principles}\n Here are some examples of the style in which I write: \n{writing_style}\n \n{writing_style}\n Finally, here are some best practices when responding to hate speech: 1. Keep your response short and to the point 2. Use either a humorous or empathetic tone - depending on the situation. For instance, an empathetic response could be "This post is very painful for Jewish people to read". A humorous response could be the use of sarcasm to discredit the hate speech. Using all the information I have provided, draft an appropriate response to the offensive post in question. Be sure to write as if you are posting on social media with no more than {word_limit} words. Do not acknowledge the original author of the hate speech post.""" try: prompt_file = open("./prompts/response_generator.txt", "r") PROMPT_TEMPLATE_TEXT = prompt_file.read() prompt_file.close() print(PROMPT_TEMPLATE_TEXT) PROMPT_TEMPLATE = PromptTemplate( template=PROMPT_TEMPLATE_TEXT, input_variables=["original_post", "background_info", "principles", "writing_style", "word_limit"]) except FileNotFoundError: print(DEAFULT_PROMPT_TEMPLATE_TEXT) PROMPT_TEMPLATE = PromptTemplate( template=DEAFULT_PROMPT_TEMPLATE_TEXT, input_variables=["original_post", "background_info", "principles", "writing_style", "word_limit"]) # %% ../nbs/free-speech-prompts.ipynb 9 def query_retriever(db, query, num_results = 3): retriever = db.as_retriever(search_kwargs={"k": num_results}) docs = retriever.get_relevant_documents(query) docs_as_text = [doc.page_content for doc in docs] docs_as_one_string = ' '.join(str(i)+ "\n" for i in docs_as_text) return docs_as_one_string # %% ../nbs/free-speech-prompts.ipynb 10 def get_chat_model_response(mdl, input_prompt): messages = [HumanMessage(content=input_prompt)] return mdl(messages) # %% ../nbs/free-speech-prompts.ipynb 11 def call_chatGPT(query): chat_model = ChatOpenAI(model_name = 'gpt-4', temperature=0.1) response = get_chat_model_response(chat_model, query) return response.content # %% ../nbs/free-speech-prompts.ipynb 12 def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None): # Use defaults in the case of None if principles is None: principles="There are no principles which I consider more important to me than the average person might." if writing_style is None: writing_style="I have no examples of my writing style." if word_limit is None: word_limit="100" retriever_query = original_post 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." background_query = background_query + "\n" + original_post background_info = call_chatGPT(background_query) # Fill the prompt filled_prompt = PROMPT_TEMPLATE.format(original_post=original_post, background_info=background_info, principles=principles, writing_style=writing_style, word_limit=word_limit) return filled_prompt, background_info # %% ../nbs/free-speech-prompts.ipynb 13 def generate_custom_response(original_post, chat_mdl, principles=None, writing_style=None, word_limit=None): # create customized prompt customized_prompt, background_info = generate_custom_prompt(original_post, principles, writing_style, word_limit) # get response draft_response = get_chat_model_response(chat_mdl, customized_prompt) return draft_response, background_info # %% ../nbs/free-speech-prompts.ipynb 14 def regenerate_custom_response(original_post, chat_mdl, regenerate_prompt, draft_response, regenerate_background=False, prev_background = None): # regenerate background info if regenerate_background: __, background_info = generate_custom_prompt(original_post) else: background_info = prev_background # create customized prompt customized_prompt = f"Please update the original response according to the following request. {regenerate_prompt}. Here is the original response: {draft_response}. And background information for the original post: {background_info}" # get response updated_response = get_chat_model_response(chat_mdl, customized_prompt) return updated_response, background_info # %% ../nbs/free-speech-prompts.ipynb 15 def assessing_response(chat_mdl, draft_response): guidelines = '''1. Keep your response short and to the point. 2. Use either a humorous or empathetic tone - depending on the situation. For instance, an empathetic response could be "This post is very painful for Jewish people to read". A humorous response could be the use of sarcasm to discredit the hate speech. 3. Promote positive dialogue and community building. Encourage constructive, respectful, and emphathetic response. 4. Keep the response as fact-based and well-reasoned. 5. Showcase the importance of unity, tolerance, and coexistence to counteract the negative narratives. 6. Always keep the aforementioned context in mind. 7. Acknowledge individual's inherent worth and dignity. ''' # create customized prompt customized_prompt = f"You are a unbiased agent reviewing on quality of response towards hate speech. Here is a list of best practices when responding to hate speech: {guidelines}. Please provide feedback on how well the response adheres to these guidelines. Here is the original response: {draft_response}. Please limit the feedback to no more than 100 words." # get response response_assessment = get_chat_model_response(chat_mdl, customized_prompt) return response_assessment