当前位置:   article > 正文

【LangChain系列 7】Prompt模版——少样本prompt模版(一)_oepnai 少样本prompt代码怎么写

oepnai 少样本prompt代码怎么写

原文地址:【LangChain系列 7】Prompt模版——少样本prompt模版(一)

本文速读:

  • prompt样本集合

  • prompt样本选择器

少样本模版的意思是:在prompt中包含一些样本,这样LLM就可以根据这些样本,更好的理解prompt,从而给出更加符合我们需要的回答。

下面我们将介绍两种方式实现 少样本prompt模版:

  • prompt样本集合

  • prompt样本选择器

01 Prompt样本集合


Prompt样本集合是指:将所有给定的样本集合作为prompt的一部分,输入给LLM。比如说样本如下:

  1. from langchain.prompts.few_shot import FewShotPromptTemplate
  2. from langchain.prompts.prompt import PromptTemplate
  3. examples = [
  4. {
  5. "question": "Who lived longer, Muhammad Ali or Alan Turing?",
  6. "answer":
  7. """
  8. Are follow up questions needed here: Yes.
  9. Follow up: How old was Muhammad Ali when he died?
  10. Intermediate answer: Muhammad Ali was 74 years old when he died.
  11. Follow up: How old was Alan Turing when he died?
  12. Intermediate answer: Alan Turing was 41 years old when he died.
  13. So the final answer is: Muhammad Ali
  14. """
  15. },
  16. {
  17. "question": "When was the founder of craigslist born?",
  18. "answer":
  19. """
  20. Are follow up questions needed here: Yes.
  21. Follow up: Who was the founder of craigslist?
  22. Intermediate answer: Craigslist was founded by Craig Newmark.
  23. Follow up: When was Craig Newmark born?
  24. Intermediate answer: Craig Newmark was born on December 6, 1952.
  25. So the final answer is: December 6, 1952
  26. """
  27. },
  28. {
  29. "question": "Who was the maternal grandfather of George Washington?",
  30. "answer":
  31. """
  32. Are follow up questions needed here: Yes.
  33. Follow up: Who was the mother of George Washington?
  34. Intermediate answer: The mother of George Washington was Mary Ball Washington.
  35. Follow up: Who was the father of Mary Ball Washington?
  36. Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
  37. So the final answer is: Joseph Ball
  38. """
  39. },
  40. {
  41. "question": "Are both the directors of Jaws and Casino Royale from the same country?",
  42. "answer":
  43. """
  44. Are follow up questions needed here: Yes.
  45. Follow up: Who is the director of Jaws?
  46. Intermediate Answer: The director of Jaws is Steven Spielberg.
  47. Follow up: Where is Steven Spielberg from?
  48. Intermediate Answer: The United States.
  49. Follow up: Who is the director of Casino Royale?
  50. Intermediate Answer: The director of Casino Royale is Martin Campbell.
  51. Follow up: Where is Martin Campbell from?
  52. Intermediate Answer: New Zealand.
  53. So the final answer is: No
  54. """
  55. }
  56. ]
  57. example_prompt = PromptTemplate(input_variables=["question", "answer"], template="Question: {question}\n{answer}")
  58. print(example_prompt.format(**examples[0]))

输出结果:

  1. Question: Who lived longer, Muhammad Ali or Alan Turing?
  2. Are follow up questions needed here: Yes.
  3. Follow up: How old was Muhammad Ali when he died?
  4. Intermediate answer: Muhammad Ali was 74 years old when he died.
  5. Follow up: How old was Alan Turing when he died?
  6. Intermediate answer: Alan Turing was 41 years old when he died.
  7. So the final answer is: Muhammad Ali

下面我们创建FewShotPromptTemplate实例,并将所有样本通过参数examples传入给模版:

  1. prompt = FewShotPromptTemplate(
  2. examples=examples,
  3. example_prompt=example_prompt,
  4. suffix="Question: {input}",
  5. input_variables=["input"]
  6. )
  7. print(prompt.format(input="Who was the father of Mary Ball Washington?"))

输出结果:

  1. Question: Who lived longer, Muhammad Ali or Alan Turing?
  2. Are follow up questions needed here: Yes.
  3. Follow up: How old was Muhammad Ali when he died?
  4. Intermediate answer: Muhammad Ali was 74 years old when he died.
  5. Follow up: How old was Alan Turing when he died?
  6. Intermediate answer: Alan Turing was 41 years old when he died.
  7. So the final answer is: Muhammad Ali
  8. Question: When was the founder of craigslist born?
  9. Are follow up questions needed here: Yes.
  10. Follow up: Who was the founder of craigslist?
  11. Intermediate answer: Craigslist was founded by Craig Newmark.
  12. Follow up: When was Craig Newmark born?
  13. Intermediate answer: Craig Newmark was born on December 6, 1952.
  14. So the final answer is: December 6, 1952
  15. Question: Who was the maternal grandfather of George Washington?
  16. Are follow up questions needed here: Yes.
  17. Follow up: Who was the mother of George Washington?
  18. Intermediate answer: The mother of George Washington was Mary Ball Washington.
  19. Follow up: Who was the father of Mary Ball Washington?
  20. Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
  21. So the final answer is: Joseph Ball
  22. Question: Are both the directors of Jaws and Casino Royale from the same country?
  23. Are follow up questions needed here: Yes.
  24. Follow up: Who is the director of Jaws?
  25. Intermediate Answer: The director of Jaws is Steven Spielberg.
  26. Follow up: Where is Steven Spielberg from?
  27. Intermediate Answer: The United States.
  28. Follow up: Who is the director of Casino Royale?
  29. Intermediate Answer: The director of Casino Royale is Martin Campbell.
  30. Follow up: Where is Martin Campbell from?
  31. Intermediate Answer: New Zealand.
  32. So the final answer is: No
  33. Question: Who was the father of Mary Ball Washington?

以上就是通过 prompt样本集合 的方式创建了少样本prompt模版。

02 Prompt样本选择器


prompt样本集合 将所有的样本作为prompt的一部分,这种方式有两个问题:

1. 有些样本可能和我们的提问的相关性不大

2. 所有的样本太多,超过了LLM的token限制

为了解决这个问题,LangChain提供了ExampleSelector,这样我们就可以选择部分更加合适的样本输入给LLM。

下面我们用SemanticSimilarityExampleSelector重写上面的代码。

1. 定义example_selector​​​​​​​

  1. from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
  2. from langchain.vectorstores import Chroma
  3. from langchain.embeddings import OpenAIEmbeddings
  4. example_selector = SemanticSimilarityExampleSelector.from_examples(
  5. # This is the list of examples available to select from.
  6. examples,
  7. # This is the embedding class used to produce embeddings which are used to measure semantic similarity.
  8. OpenAIEmbeddings(),
  9. # This is the VectorStore class that is used to store the embeddings and do a similarity search over.
  10. Chroma,
  11. # This is the number of examples to produce.
  12. k=1
  13. )
  14. # Select the most similar example to the input.
  15. question = "Who was the father of Mary Ball Washington?"
  16. selected_examples = example_selector.select_examples({"question": question})
  17. print(f"Examples most similar to the input: {question}")
  18. for example in selected_examples:
  19. print("\n")
  20. for k, v in example.items():
  21. print(f"{k}: {v}")

2. 创建FewShotPromptTemplate实例​​​​​​​

  1. prompt = FewShotPromptTemplate(
  2. example_selector=example_selector,
  3. example_prompt=example_prompt,
  4. suffix="Question: {input}",
  5. input_variables=["input"]
  6. )
  7. print(prompt.format(input="Who was the father of Mary Ball Washington?"))

输出结果:​​​​​​​

  1. Question: Who was the maternal grandfather of George Washington?
  2. Are follow up questions needed here: Yes.
  3. Follow up: Who was the mother of George Washington?
  4. Intermediate answer: The mother of George Washington was Mary Ball Washington.
  5. Follow up: Who was the father of Mary Ball Washington?
  6. Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
  7. So the final answer is: Joseph Ball
  8. Question: Who was the father of Mary Ball Washington?

通过ExampleSelector的方式选择合适的部分样本作为prompt的一部分,这种方式称为 prompt样本选择器,一方面解决样本过多的问题,另一方面可以提高prompt的质量,从而得到更加符合我们要求的回答。

本文小结

通过介绍 prompt样本集合 和 prompt样本选择器 两种创建少样本prompt模版的方式,我们对 少样本prompt模版 有了基本的认识,可以根据实际的业务需求创建自己的 少样本prompt模版 了。

更多最新文章,请关注公众号:大白爱爬山

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/336639
推荐阅读
相关标签
  

闽ICP备14008679号