Using LangChain to build a database-based Q&A robot (3): ReAct

Hello everyone, I am Jambo. We have learned how to use some basic functions of LangChain, and we should combine these functions to do some complex things. But before that, in order to let students better understand what LangChain has done, I would like to introduce some knowledge about the use of GPT.

At the beginning of ChatGPT's opening, in addition to major companies competing in AI algorithms, many people are studying how to make GPT-3 make better answers only by modifying the prompt. This method is called "prompt engineering ( Prompt Engineering)". If the LLM is likened to a brain with common sense, then the hint project is teaching it how to think, so as to combine knowledge more effectively to get answers. Like AutoGPT, he can let GPT-4 complete various tasks by itself through a well-designed prompt. In order to let students understand the ideas involved, we start with the introduction of the "chain of thought".

Chain of Thought

Chain of Thought (Chain of Thought) was proposed shortly after the launch of ChatGPT. Specifically, it is to let GPT-3 generate the thinking process of the question by manually writing examples. In this way, the effect of GPT-3 answer will be There is a substantial increase. Just like we are writing more complicated calculation questions, the correct rate of writing the process step by step will be higher than writing the answer directly.

Alt text

Later, someone discovered that the same effect can be achieved by just adding the magic hint "Let's think step by step." No need to write examples. And on this basis, he also asked GPT to sum up a more concise answer based on the answer with the thinking process in front of it, which is equivalent to hiding the thinking process.

Alt text

What I want to emphasize here is that when we use LLM to build applications, we can use LLM to analyze, disassemble, and solve problems many times before outputting the final answer. Just like the hidden layer between the input and the output in the deep learning network, we can add more logic here, so that the LLM can better understand the problem and come up with a better answer.

ReAct

No matter how we tune the prompt, LLM still suffers from a major flaw: it always generates answers based on the dataset at training time and the information provided in the prompt. If questions are asked about the weather, news or internal documents, etc., LLM will not be able to answer or will produce false answers ("hallusion").

Of course, after the user asks a question, we can first use a search engine or an internal database to search for the question, and then use the search results as part of the prompt. But in practice, this approach doesn't always work well, and can even be misleading in some cases. For example, if we want LLM to help us revise an article, under this process, the program will first use a search engine to search for the article, but it is likely that no relevant results can be found. That would put the LLM on straight strike, as we generally ask the LLM to generate content based only on the information provided by the prompt, to ensure accuracy.

In order to solve this problem, we can let LLM decide whether to search and what to search, just like optimizing the search statement with LLM mentioned in the previous tutorial. So the ReAct system was proposed, telling LLM which tools can be used through examples in the prompt, and then letting LLM decide whether to use these tools and how to use them. There are three parts to ReAct:

  1. Thinking: Thinking about what action needs to be taken based on the current information.
  2. Action: Make corresponding actions according to the thinking results, such as calling tools. The program can analyze the string generated in this step to call corresponding tools, similar to Python evalfunctions.
  3. Observation: Store the results of an action, such as a search, for the next time you think about it.

These three steps will continue to circulate until the thinking step judges that the answer has been found, and the final answer will be given in the following actions.

Alt text

In fact, if you think about it carefully, this process is similar to our human thinking process. When we think, we judge whether we need to take some action based on the current information, and then judge whether we have found the answer according to the result of the action. If not, we will continue to think until we find the answer.

The behavior mode of AutoGPT is actually the same as ReAct, but the prompt of AutoGPT is more detailed than the steps in the paper that proposed ReAct, and provides more tools for LLM to use. But either way, you need to write a lot of examples to tell LLM how to think and act, and depending on the tools provided, the effect will be different, and ReAct's thinking process will have many steps. And LangChain helps us hide these steps, and encapsulates this series of actions into "agents", and this is what we will introduce in the next chapter.

Last article: Using LangChain to build a database-based Q&A robot (2): Extracting text information from data sources

Guess you like

Origin blog.csdn.net/chenjambo/article/details/131885624