Advertisement
drinfernoo

Untitled

Aug 1st, 2023
1,402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. ```python
  2. from langchain.chat_models import ChatOpenAI
  3. from langchain.chains import LLMChain, SequentialChain
  4. from langchain.memory import ConversationBufferMemory
  5. from langchain.prompts import PromptTemplate
  6.  
  7. llm = ChatOpenAI(temperature=0.7,)
  8. memory = ConversationBufferMemory(memory_key="chat_history", input_key="human_input")
  9.  
  10. system_template = """You are a writer who is well-versed in the writing styles of children's book and story authors.
  11.  
  12. {chat_history}
  13. Human: {human_input}
  14. Writer:"""
  15. system_prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=system_template)
  16.  
  17. author_template = """Given an author's name, describe the writing style of the given author.
  18.  
  19. Author: {author}
  20.  
  21. Writer: This is a description of the style of the above author:"""
  22. author_prompt = PromptTemplate(input_variables=["author"], template=author_template)
  23. author_chain = LLMChain(llm=llm, prompt=author_prompt, output_key="author_style")
  24.  
  25. 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.
  26.  
  27. Age: {age}
  28. Author: {author}
  29. Author's Style: {author_style}
  30.  
  31. Writer: This is an outline for a story in the style of the above author:"""
  32. outline_prompt = PromptTemplate(
  33.     input_variables=["age", "author", "author_style"], template=outline_template
  34. )
  35. outline_chain = LLMChain(llm=llm, prompt=outline_prompt, output_key="outline")
  36.  
  37. story_template = """Given an outline for a children's bedtime story, write an age-appropriate story in the style of the given author.
  38.  
  39. Age: {age}
  40. Author: {author}
  41. Author's Style: {author_style}
  42. Story Outline: {outline}
  43.  
  44. Writer: This is an age-appropriate story (for {age} year-olds), based on the above outline, in the style of {author}:"""
  45. story_prompt = PromptTemplate(
  46.     input_variables=["age", "author", "author_style", "outline"],
  47.     template=story_template,
  48. )
  49. story_chain = LLMChain(llm=llm, prompt=story_prompt)
  50.  
  51. overall_chain = SequentialChain(
  52.     chains=[author_chain, outline_chain, story_chain],
  53.     input_variables=["age", "author"],
  54.     verbose=True,
  55.     memory=memory,
  56. )
  57. review = overall_chain({"age": 6, "author": "Dr. Seuss"})
  58.  
  59. print(review)
  60. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement