Spaces:
Runtime error
Runtime error
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/free-speech-prompts.ipynb. | |
# %% auto 0 | |
__all__ = ['DEAFULT_PROMPT_TEMPLATE_TEXT', 'query_retriever', 'generate_custom_prompt', 'get_chat_model_response', | |
'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) | |
return docs | |
# %% ../nbs/free-speech-prompts.ipynb 10 | |
def generate_custom_prompt(original_post, principles=None, writing_style=None, word_limit=None): | |
# Get database and query retriever | |
#### | |
background_db = DeepLake(dataset_path="hub://vanderbilt-dsi/hatespeech-background", embedding = OpenAIEmbeddings()) | |
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_info = query_retriever(background_db, retriever_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 11 | |
def get_chat_model_response(mdl, input_prompt): | |
messages = [HumanMessage(content=input_prompt)] | |
return mdl(messages) | |
# %% ../nbs/free-speech-prompts.ipynb 12 | |
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 13 | |
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 | |