Large language model 8 - Prompt engineer

The good effect of large language models is largely due to the computing power and data sets. The computing power allows the model to be large enough, so that the model's understanding, memory, reasoning and generation, and generalization capabilities can be all at the same time, while the data set is It is the source of model learning.

The prompt method in LLM is mainly used in the fine-tune stage, that is, based on pre-training, templates or prompts for specific tasks. These prompts usually contain key information of the question, such as question type, answer type, and restrictions. etc., through a small amount of annotated data (prompts) to fine-tune the model to better adapt to specific tasks and application scenarios. In this process, prompt engineering can help the model better understand tasks and inputs, and generate more accurate output, thereby improving the model's effect and performance in practical applications.

During inference, the prompt method is not used to modify the user's input. Instead, it is mainly used to provide additional contextual information to the model to better understand the user's input and questions. For example, in a question and answer task, the prompt method can generate a prompt related to the question, helping the model better understand the question and user input, and generate more accurate answers.

Specifically, the prompt project can help the model in the following aspects:

Provide task-related prompt information: The prompt project can generate prompt information for specific tasks, such as question type, answer type, restrictions, etc. These hints can help the model better understand the task and input, and generate more accurate output.

Generate task-specific templates: The prompt project can generate task-specific templates to help the model better generate output that meets requirements. For example, in question and answer tasks, templates can be used to guide the model to generate answers that meet the requirements of the question.

Provide additional contextual information: prompt projects can provide additional contextual information to help the model better understand the input and problem. For example, in text generation tasks, the context information provided by the prompt project can be used to help the model generate text that meets the requirements.

Prompt engineering in the field of text generation creates input requests for large language models (LLM) to produce expected outputs. To create good question-answer pairs for the model, a good prompt engineer often has some understanding of large models. , and be able to clearly summarize and state problems. The prompt project can be traced back to the text-text model of ChatGPT. After that, textual graphics and non-text prompts have been developed.

Model hallucination limitations pose real challenges when developers wish to build systems with models, since the entire system must meet quality, security, and trustworthiness requirements. For example, can we trust that the automated code reviews provided by LLM are correct? Or are the answers returned to questions about how to handle insurance-related tasks reliable?

This article introduces several commonly used literary methods. These methods can be used in marketing, advertising creativity, mathematics, law, business cases, etc. For convenience, this article uses the mathematical chicken and rabbit problem in the same cage ("Chicken and Rabbit"). In a cage, there are 35 heads and 94 legs. How many chickens are there and how many rabbits are there?") For example.

1.1 Model Illusion

Illusions in large language models arise from data compression and inconsistencies. Quality assurance is challenging as many data sets may be outdated or unreliable. To reduce hallucinations:

The temperature of a model is a scalar value used to adjust the probability distribution predicted by the model. In the case of LLM, it strikes a balance between sticking to what the model learned from the training data and generating more diverse or creative responses. Often, these creative answers are more prone to hallucinations.

1. Temperature parameters that constrain model creativity;
2. Pay attention to prompt engineering, guide the model to think step by step, and provide facts and source references in the response.
3. Incorporate external knowledge sources to improve answer verification.

A combination of these methods can achieve the best results.

The paper calls generating content that is false or not faithful to the source of information an illusion.
Illusions can be classified into the following categories:
1. Logical fallacy: The model makes errors in its reasoning and provides wrong answers.
2. Fabricating facts: Instead of answering “I don’t know”, the model confidently asserts facts that don’t exist.
3. Bias caused by data: Due to the prevalence of certain data, the output of the model may be biased, resulting in incorrect results.

One explanation for why LLM produces hallucinations is that the training process of the model is a data compression process. As shown in the figure below, a large amount of data in the training set is eventually compressed into a small number of parameters and the relationship between parameters. The key to this compression is to generate A model stores a mathematical representation of the relationships (probabilities) between inputs (text or pixels), rather than the inputs themselves, and more importantly, this representation lets us extract knowledge (either through sampling or running queries/hints).

This compression reduces fidelity, making full recovery of the original knowledge difficult, and thus the model tends to imperfectly "fill in the gaps" or produce hallucinations, which is a trade-off for this compressed but useful knowledge representation.

LLMs can also produce hallucinations when their training data sets contain limited, outdated, or contradictory information about the questions posed to them.
Insert image description here
The prompt method is as follows:

1.2 Direct answer

Insert image description here

1.3 Chain of Thought (CoT)

Chain-of-thought (CoT) chain-of-thought prompts that the core idea of ​​chain-of-thought is to divide the final answer acquisition into multiple intermediate processes. The process from this intermediate to the final answer is to induce the model to use reasoning steps that imitate ideas to improve reasoning capabilities. , which will improve the model's ability in mathematics and common sense reasoning. For example, in the common chicken and rabbit cage problem in mathematics, in the early days of CoT, a small number of examples were provided for Q&A. This is called a few-shot prompting technology. Of course, you can also add "Please deduce step by step" and "Please give" to the Q. Detailed Thought Process," which has been shown to be equally effective, is known as the zero-shot prompting technique. Many models show that CoT technology can improve the model's reasoning capabilities.
Insert image description here

1.4 Least-to-most prompting

The core idea of ​​this method is that since humans draw conclusions through facts and logical thinking, LLM should also be able to learn this skill through some examples of logical thinking reasoning.
CoT serves as a few-shot prompt engineering approach, and LLM can be taught how to break down challenges or problems into reasoning patterns. The Self-Ask method not only guides the LLM to follow the CoT method when reasoning, but also allows the LLM to ask its own questions to obtain the final answer. But what if the problem to be solved by LLM is more difficult than the Self-Ask example? Least-to-most is the prompt method used to deal with this type of problem. This method is based on the following strategies:
1. Decompose the complex problem into a series of simpler sub-problems.
2. Then solve each sub-problem in turn.
The answer to the sub-problem solved first benefits the unsolved sub-problem. Least-to-most hints are a technique that uses a sequence of step-by-step hints to arrive at a final conclusion.

Least-to-most prompting can be combined with thought chains and self-consistent methods. For some questions, the two stages of least-to-most prompting can be combined into a single prompt.
Insert image description here

1.5 Self-consistency decoding

Thought chain reasoning produces a thought path which is then followed in turn. Instead, self-consistency taps into the intuition that a complex reasoning problem often allows for many different ways of thinking, leading to its single correct answer.
The self-consistent method consists of three steps:

Prompt LLM to generate the Chain of Thought (CoT) reasoning section.
Generate a diverse set of reasoning paths.
Choose the most consistent output for the final answer.
Insert image description here

1.6 Self-Ask Prompting

The core idea of ​​this method is that even if the LLM does not have a clear and direct answer to the problem, the LLM has sufficient knowledge and skills. These knowledge and skills can solve some sub-problems, and the final problem can be solved through these sub-problems. Source.
Self-question prompts are a progression from direct and thought chain prompts, LLM reasoning is clearly demonstrated, LLM also breaks down questions into smaller follow-up questions, LLM knows when the final answer is reached, and can move from subsequent intermediate answers to final Answer.
Insert image description here

from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

self_ask_with_search = initialize_agent(
    tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
)
self_ask_with_search.run(
    "鸡和兔在一个笼子里,共有35个头,94只脚,那么鸡有多少只,兔有多少只?”
)

1.7 Meta-Prompting

The Meta-Prompting method refers to guiding users to think about the problem by asking questions, that is, they can ask and think about the problem themselves and gradually solve the original problem. The following are the steps to use the Meta-Prompting method to answer this question in detail:

Q: 鸡和兔在一个笼子里,共有35个头,94只脚,那么鸡有多少只,兔有多少只?

A: 我们可以通过以下问题来引导思考:

鸡和兔的数量分别是多少?设鸡的数量为x,兔的数量为y。

题目中给出了哪些条件?我们需要将这些条件转化为方程式。

如何将问题转化为方程组的形式?可以根据头和脚的数量来列出方程组。

如何求解方程组?可以使用代数方法或图形方法求解。

求解出来的结果是否符合题目要求?我们需要检验求解出来的结果是否满足题目中给出的条件。

最终的结果是什么?我们需要将求解出来的鸡和兔的数量反馈给用户。

根据以上问题,我们可以得到以下详细回答:

鸡和兔的数量分别是多少?设鸡的数量为x,兔的数量为y。

题目中给出了哪些条件?我们需要将这些条件转化为方程式。题目中给出了35个头和94只脚,因此可以列出以下两个方程式:

x + y = 35 (鸡和兔的总数为35)
2x + 4y = 94 (鸡的脚有2只,兔的脚有4只)

如何将问题转化为方程组的形式?可以根据头和脚的数量来列出方程组。我们可以通过以下问题来引导思考:

鸡和兔的总数是多少?
鸡和兔的脚的总数是多少?
鸡和兔的脚的总数如何与鸡和兔的数量相关联?
根据以上问题,我们可以得到以下方程组:

x + y = 35 (鸡和兔的总数为35)
2x + 4y = 94 (鸡的脚有2只,兔的脚有4只)

如何求解方程组?可以使用代数方法或图形方法求解。我们可以通过以下问题来引导思考:

如何消去其中一个未知量?
如何求解另一个未知量?
根据以上问题,我们可以得到以下过程:

将第一个方程式乘以2,得到2x + 2y = 70
将第二个方程式减去上面的式子,得到2y = 24
解得y = 12
将y = 12代入第一个方程式,得到x = 23

求解出来的结果是否符合题目要求?我们需要检验求解出来的结果是否满足题目中给出的条件。我们可以通过以下问题来引导思考:

求解出来的鸡和兔的数量是否满足题目中给出的总数?
求解出来的鸡和兔的脚的数量是否满足题目中给出的总数?
根据以上问题,我们可以得到以下答案:

鸡和兔的总数为35,求解出来的鸡和兔数量之和为35,符合题目要求。
鸡和兔的脚的总数为94,求解出来的鸡和兔脚的数量之和为94,符合题目要求。
最终的结果是什么?我们需要将求解出来的鸡和兔的数量反馈给用户。因此,最终结果是:鸡有23只,兔有12只。

1.8 ReAct

For humans, the tight synergy between reasoning and action enables humans to quickly learn new tasks and make robust reasoning and decisions. We can do this even in the face of unforeseen circumstances, information or uncertainty. The core idea of ​​ReAct is to combine reasoning and taking action.
Insert image description hereAccording to research, ReAct overcomes the illusion and error cascading problems of CoT reasoning by interacting with knowledge sources such as Wikipedia. Human-like task-solving trajectories are generated.

As shown in the figure below, in the event sequence of the ReAct-based Agent, inference tracking makes the final result of LLM more interpretable and uses various references in the thinking process.

from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

react = initialize_agent(tools, llm, agent=AgentType.REACT_DOCSTORE, verbose=True)

1.9 Symbolic Reasoning & PAL

The Symbolic Reasoning & PAL method is an intelligent question and answer technology based on symbolic reasoning and feasibility logic, which can use logical reasoning and knowledge representation to solve complex problems.
Insert image description here
Chicken and rabbit in the same cage itself is a mathematical problem, and it is easy to think of using symbolic methods to solve this kind of problem. The following is a vegetable statistics problem, which also shows the symbolic method process.
Insert image description here

1.10 Iterative Prompting

LLM fine-tuning and enhanced hint engineering can improve model performance. Make sure prompts are contextual and contain small training examples and conversation history. Iterative prompts should establish a chain of contextual thoughts that negates the creation of irrelevant facts and hallucinations. Interactive context awareness & context prompts.

1.11 Sequential Prompting

Typically recommender systems are developed in a pipeline architecture, consisting of a multi-stage candidate set generation (retrieving more relevant items) and ranking (ranking relevant items in a higher position) process. Sequential Prompting focuses on the ranking stage of recommendation systems because LLM is more expensive to run on large-scale candidate sets.
The ranking performance is sensitive to the retrieved top-ranked candidate items and is more suitable for examining subtle differences in LLM recommendation capabilities.
Insert image description here

Guess you like

Origin blog.csdn.net/shichaog/article/details/132773249