File size: 6,065 Bytes
6dc8174
 
 
a12fe73
8e04782
6dc8174
 
 
8e04782
6dc8174
 
 
8e04782
 
6dc8174
 
8e04782
6dc8174
 
 
 
 
 
 
 
 
 
 
8e04782
6dc8174
8e04782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6dc8174
 
 
8e04782
 
 
 
b32f76b
 
 
 
8e04782
 
a12fe73
 
 
 
 
 
 
 
 
 
 
 
 
 
8e04782
 
 
a12fe73
8e04782
6dc8174
 
 
 
 
 
 
 
8e04782
 
 
 
 
a12fe73
 
 
8e04782
6dc8174
 
8e04782
6dc8174
8e04782
6dc8174
a12fe73
8e04782
6dc8174
 
8e04782
6dc8174
 
 
 
8e04782
 
a12fe73
8e04782
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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']

# %% ../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.
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
Here are the sources I would like you to use when getting information for my response:
\n{sources}\n
Using all the information I have provided, please draft an appropriate response to the offensive post in question that will hopefully make people more accepting of others.
Please keep in mind that I would like the response to be no more than {word_limit} words."""

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", "sources", "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", "sources", "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):

    # Get database and query retriever
    
    sources_db = DeepLake(dataset_path="hub://vanderbilt-dsi/counterspeech-resources", embedding = OpenAIEmbeddings())

    # 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="an infinite amount of"

    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)
    sources = query_retriever(sources_db, retriever_query)

    # Fill the prompt
    filled_prompt = PROMPT_TEMPLATE.format(original_post=original_post, background_info=background_info, principles=principles, writing_style=writing_style, sources=sources, word_limit=word_limit)

    return filled_prompt, background_info, sources

# %% ../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, sources = 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, sources

# %% ../nbs/free-speech-prompts.ipynb 14
def regenerate_custom_response(chat_mdl, regenerate_prompt, draft_response):

    # 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}"

    # get response
    updated_response = get_chat_model_response(chat_mdl, customized_prompt)

    return updated_response