LLM提示词效果提升

Reading time ~12 minutes

本文介绍了如何通过任务拆分、少样本学习和思维链(CoT)等方法来提高人工智能模型的推理和响应能力。特别强调了明确定义任务、提供背景知识和术语、以及通过分步解释来增加透明度和准确性的重要性。文中还探讨了零样本CoT和少样本CoT,展示了如何通过设置输出格式和提供关键词来改善推理结果的抽取。最后,文中通过JSON格式输出的例子,说明了如何将推理过程和结论以结构化数据的形式呈现,以便于分析和理解。

任务拆分

## Instructions
Using the included text below, perform the following steps:

1. Read through the entire text carefully
2. Extract the most important paragraph
3. From the paragraph extracted in step 2, extract the most important sentence
4. Summarize the sentence extracted in step 3 and make it between 30 and 50 words long.
5. Only return the result of step 4 in your response.

提示续写补全

如果输出是 HTML 代码并且必须是格式正确的 HTML 文档非常重要,则可以向模型展示完成应该如何开始,并且它往往会效仿。

...
Please generate the response in a well-formed HTML document. The completion should begin as
follows:

<!DOCTYPE html>
<html>

定义任务

与其简单地直接提出问题,不如明确定义任务,同时提供简洁明了的说明(提供背景知识或特定领域的术语)。该模型通常可以通过包含相关的背景知识、特定领域的术语和相关示例来构建更扎实的响应。

[prompt]
Hearsay is an out-of-court statement introduced to prove the truth of the matter
asserted. Answer the following question regarding hearsay with either Yes or No.

On the issue of Albert's wellbeing after the accident, Angela testified that he
gave a thumbs up when asked how he was feeling. Is there hearsay?

[completion]
Yes.

少样本学习

思维链CoT

思维链(有时缩写为 CoT)提示鼓励其LLM推理提供分步解释,以提高透明度,允许更好的错误分析,并帮助引导模型得出正确的答案。

零样本CoT

[prompt]
Hearsay is an out-of-court statement introduced to prove the truth of the matter asserted.

Q: On the issue of Albert's wellbeing after the accident, Angela testified that he gave a
thumbs up when asked how he was feeling. Is there hearsay?
A: Work through the problem step by step first and then answer the question.

[completion]
Yes, let's work through this scenario step by step to identify if there's hearsay involved.

1. Angela's Testimony: According to Angela's testimony, Albert gave a "thumbs up" when asked
about his well-being. This statement was made by Angela in court, which means it's in-court testimony.

2. Nature of the Statement: Angela's statement about Albert's "thumbs up" gesture is being
used to imply that Albert is doing well or feeling good after the accident.

3. Out-of-Court Statement: The actual statement being discussed here - "I'm feeling good" - was
allegedly made by Albert out of court. This is a classic example of an out-of-court statement.

4. Purpose of the Statement: Angela's testimony, including the reference to Albert's gesture,
  is being used to prove Albert's subjective state of well-being.

Therefore, yes, there is hearsay in Angela's testimony. The statement made by Albert, describing
his feelings, is hearsay because it's an out-of-court assertion being used to prove the truth
of the matter - in this case, that Albert was feeling well after the accident.

few shot COT

zero shot COT推理结果不容易抽取,使用few shot COT可设置输出格式。

Reasoning:关键词

[prompt]
Hearsay is an out-of-court statement introduced to prove the truth of the matter asserted.

Q: On the issue of whether David is fast, the fact that David set a high school track record.
Is there hearsay?
Reasoning: The statement is non-assertive conduct.
A: No

Q: On the issue of whether Rebecca was ill, the fact that Rebecca told Ronald that she was
unwell. Is there hearsay?
Reasoning: The statement is standard hearsay.
A: Yes

Q: To prove that Tim was a soccer fan, the fact that Tim told Jimmy that "Real Madrid was
the best soccer team in the world." Is there hearsay?
Reasoning: The statement is not introduced to prove truth.
A: No

Q: When asked by the attorney on cross-examination, Alice testified that she had "never seen
the plaintiff before, and had no idea who she was." Is there hearsay?
Reasoning: The statement was made in-court.
A: No

Q: On the issue of whether Martin punched James, the fact that Martin smiled and nodded when
asked if he did so by an officer on the scene. Is there hearsay?
Reasoning: The statement is non-verbal hearsay.
A: Yes

Q: On the issue of Albert's wellbeing after the accident, Angela testified that he gave a
thumbs up when asked how he was feeling. Is there hearsay?

[completion]
Yes. Angela's testimony about Albert's non-verbal conduct, i.e., giving a "thumbs up," falls
under the definition of hearsay, as it is an out-of-court statement (Albert's gesture) used
to prove the truth of the matter asserted (that Albert was doing well after the accident). 

Hearsay is typically defined as an out-of-court statement offered in court for the truth of
the matter asserted. Non-verbal conduct, such as gestures or nods, can indeed constitute
hearsay if they are intended to assert a fact. In this scenario, Albert's thumbs up is
interpreted as asserting the fact that he is feeling well, hence it is considered hearsay.

few shot COT以JSON格式输出

[prompt]
Hearsay is an out-of-court statement introduced to prove the truth of the matter asserted.
Given a statement, respond with a JSON object that has two keys: `reasoning` and `answer`.
The first key's value should contain the reasoning used to arrive at the answer.
The second key's value should contain either "Yes" or "No".

Q: On the issue of whether David is fast, the fact that David set a high school track record.
Is there hearsay?
{
  "reasoning": "The statement is non-assertive conduct."
  "answer": "No"
}

Q: On the issue of whether Rebecca was ill, the fact that Rebecca told Ronald that she was
unwell. Is there hearsay?
{
  "reasoning": "The statement is standard hearsay."
  "answer": "Yes"
}

Q: To prove that Tim was a soccer fan, the fact that Tim told Jimmy that "Real Madrid was
the best soccer team in the world." Is there hearsay?
{
  "reasoning": "The statement is not introduced to prove truth."
  "answer": "No"
}

Q: When asked by the attorney on cross-examination, Alice testified that she had "never seen
the plaintiff before, and had no idea who she was." Is there hearsay?
{
  "reasoning": "The statement was made in-court."
  "answer": "No"
}

Q: On the issue of whether Martin punched James, the fact that Martin smiled and nodded
when asked if he did so by an officer on the scene. Is there hearsay?
{
  "reasoning": "The statement is non-verbal hearsay."
  "answer": "Yes"
}

Q: On the issue of Albert's wellbeing after the accident, Angela testified that he gave
a thumbs up when asked how he was feeling. Is there hearsay?
{
  "reasoning": "<the reasoning used to arrive at the above answer>",
  "answer": "<Yes if there was hearsay, No if there was not>"
}

[completion]
{
  "reasoning": "The statement is a non-verbal assertion, which also counts as hearsay.",
  "answer": "Yes"
}

prompt chain

提示链接可以显式强制模型放慢速度并将任务分解为组成部分。如前面所述,任务拆分可以成为提高完成质量的有效技术。但是,有时会LLM尝试立即输出答案。此外,在信息过载场景中,更复杂的指令也不会被丢弃,能得到更好的指令遵循效果。

与其要求模型在回答之前“逐步解决问题”(在某些情况下LLMs可能会忘记这样做),我们可以先要求对情况进行分析,然后要求简单的“是”或“否”答案

[prompt 1]
Hearsay is an out-of-court statement introduced to prove the truth of the matter asserted.

In determining whether a piece of evidence meets the definition of hearsay, lawyers ask three
questions:

1. Was there a statement?
2. Was it made outside of court?
3. Is it being introduced to prove the truth of the matter asserted?

Apply this reasoning to the following question.

Q: On the issue of Albert's wellbeing after the accident, Angela testified that he gave a
thumbs up when asked how he was feeling.

[completion]
Yes, this is hearsay, based on the definition you provided.

1. There was a statement: Angela testified that Albert gave a thumbs up. This would constitute
a gesture as a form of statement, indicating Albert's state of being. 

2. It was made outside of court: According to the scenario, Angela's observation of Albert's
gesture occurred outside the courtroom, making it an out-of-court statement. 

3. It's being introduced to prove the truth of the matter asserted: Angela's testimony,
including the thumbs-up gesture, is being used as evidence of Albert's well-being after the
accident. The gesture is being used literally for its assertional value, suggesting that Albert
is doing well, or at least, that he is feeling fine. 

Therefore, Angela's testimony regarding Albert's gesture fits the definition of hearsay and
would be treated as such in a legal setting.

在上述完成过程中,该问题已正确分析,但我们正在寻找一个明确的“是”或“否”答案,以便下游任务可以轻松引入。因此,我们将第一个提示的完成与第二个提示联系起来:

[prompt 2]
Given the question below and the accompanying analysis, answer with only "Yes" or "No".

## question
{question}

## analysis
{completion_from_prompt_1}

[completion]
Yes

第一个提示来专注于分析,第二个提示在单个词的响应中正确提取信息。

Query Rewrite重写技术

Query rewriting 是将 queries 和系统中存储的文档的语义空间进行对齐(aligning the semantics of queries and documents)的关键技术。 Continue reading

GraphRAG介绍

Published on July 02, 2024

RAG系统优化

Published on June 26, 2024