Spaces:
Runtime error
Runtime error
from langchain.base_language import BaseLanguageModel | |
from langchain.chains import LLMChain, SequentialChain | |
from langchain.chat_models import ChatAnthropic | |
from langchain.chat_models import ChatOpenAI | |
from langchain.llms import HuggingFaceHub | |
from langchain.prompts import ( | |
PromptTemplate, | |
ChatPromptTemplate, | |
SystemMessagePromptTemplate, | |
HumanMessagePromptTemplate, | |
) | |
class StyleScribble: | |
example: str | |
prompt: str | |
llm: BaseLanguageModel | |
def __init__(self, example=None, prompt=None, llm=None): | |
self.example = example | |
self.prompt = prompt | |
self.llm = llm | |
def set_imp_llm(self, model): | |
if model == 'GPT3': | |
self.llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k") | |
elif model == "GPT4": | |
self.llm = ChatOpenAI(model_name="gpt-4") | |
elif model == "Claude": | |
self.llm = ChatAnthropic() | |
else: | |
self.llm = HuggingFaceHub(repo_id=model) | |
def run(self): | |
return self.process() | |
def process(self): | |
seq_chain = SequentialChain( | |
chains=[self.get_extract_tone_chain(), self.get_generate_text_chain(self.prompt), | |
self.get_apply_style_chain()], | |
input_variables=["text"], verbose=True) | |
result = seq_chain({'text': self.example, "style": ""}) | |
return str(result.get('result')) | |
def create_chain(self, chat_prompt, output_key): | |
return LLMChain(llm=self.llm, | |
prompt=chat_prompt, output_key=output_key) | |
def get_extract_tone_chain(self): | |
template = """Building upon the nuances and distinctive traits in the sample text, establish | |
a comprehensive style guide that encapsulates the unique tone and writing style present in the sample. | |
This guide should focus on compelling tactics that foster a sense of connection between readers and | |
the content. Refrain from discussing the specific theme of the sample text or using it as a direct example. | |
Instead, formulate your analysis in such a way that it remains abstract, facilitating its application to any | |
other text that might be inspired by or originate from the sample. This abstract analysis will enable writers to adopt the essence of the style while allowing for versatility across various themes or topics.. | |
""" | |
system_message_prompt = SystemMessagePromptTemplate.from_template(template) | |
human_template = "{text}" | |
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) | |
return self.create_chain(chat_prompt, "style") | |
def get_generate_text_chain(self, prompt): | |
template = """Generate a text following the user_request(use same language of the request): | |
{user_request} | |
""".replace("{user_request}", prompt) | |
return self.create_chain(PromptTemplate.from_template(template), | |
"generated_text") | |
def get_apply_style_chain(self): | |
template = """STYLE: | |
{style} | |
REWRITE THE TEXT BELLOW APPLYING THE STYLE GUIDE ABOVE(use same language of the request): | |
{generated_text} | |
""" | |
prompt = PromptTemplate.from_template(template=template) | |
prompt.partial(style="") | |
return self.create_chain(prompt, "result") | |