执行多个chain

因为他是链式的,所以他也可以按顺序依次去执行多个 chain

  1. from langchain.llms import OpenAI
  2. from langchain.chains import LLMChain
  3. from langchain.prompts import PromptTemplate
  4. from langchain.chains import SimpleSequentialChain
  5. # location 链
  6. llm = OpenAI(temperature=1)
  7. template = """Your job is to come up with a classic dish from the area that the users suggests.
  8. % USER LOCATION
  9. {user_location}
  10. YOUR RESPONSE:
  11. """
  12. prompt_template = PromptTemplate(input_variables=["user_location"], template=template)
  13. location_chain = LLMChain(llm=llm, prompt=prompt_template)
  14. # meal 链
  15. template = """Given a meal, give a short and simple recipe on how to make that dish at home.
  16. % MEAL
  17. {user_meal}
  18. YOUR RESPONSE:
  19. """
  20. prompt_template = PromptTemplate(input_variables=["user_meal"], template=template)
  21. meal_chain = LLMChain(llm=llm, prompt=prompt_template)
  22. # 通过 SimpleSequentialChain 串联起来,第一个答案会被替换第二个中的user_meal,然后再进行询问
  23. overall_chain = SimpleSequentialChain(chains=[location_chain, meal_chain], verbose=True)
  24. review = overall_chain.run("Rome")

image-20230406000133339