Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```python
- from langchain.chat_models import ChatOpenAI
- from langchain.chains import LLMChain, SequentialChain
- from langchain.memory import ConversationBufferMemory
- from langchain.prompts import PromptTemplate
- llm = ChatOpenAI(temperature=0.7,)
- memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
- system_template = """You are a writer who is well-versed in the writing styles of children's book and story authors.
- {chat_history}
- Human: {human_input}
- Writer:"""
- system_prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=system_template)
- author_template = """Given an author's name, describe the writing style of the given author.
- Author: {author}
- Writer: This is a description of the style of the above author:"""
- author_prompt = PromptTemplate(input_variables=["author"], template=author_template)
- author_chain = LLMChain(llm=llm, prompt=author_prompt, output_key="author_style")
- outline_template = """Given an author's name, a description of their writing style, and a child's age, write an outline for an age-appropriate bedtime story in the style of the given author.
- Age: {age}
- Author: {author}
- Author's Style: {author_style}
- Writer: This is an outline for a story in the style of the above author:"""
- outline_prompt = PromptTemplate(
- input_variables=["age", "author", "author_style"], template=outline_template
- )
- outline_chain = LLMChain(llm=llm, prompt=outline_prompt, output_key="outline")
- story_template = """Given an outline for a children's bedtime story, write an age-appropriate story in the style of the given author.
- Age: {age}
- Author: {author}
- Author's Style: {author_style}
- Story Outline: {outline}
- Writer: This is an age-appropriate story (for {age} year-olds), based on the above outline, in the style of {author}:"""
- story_prompt = PromptTemplate(
- input_variables=["age", "author", "author_style", "outline"],
- template=story_template,
- )
- story_chain = LLMChain(llm=llm, prompt=story_prompt)
- overall_chain = SequentialChain(
- chains=[author_chain, outline_chain, story_chain],
- input_variables=["age", "author"],
- verbose=True,
- memory=memory,
- )
- review = overall_chain({"age": 6, "author": "Dr. Seuss"})
- print(review)
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement