---
title: "LLM 的 ReAct 模式 | 行开心的颠倒世界"
description: "大语言模型的 Prompt ，通过加入各类控制，让语言模型可以在碰到问题变相使用外部工具来解决"
canonical: "https://xingkaixin.me/posts/llm-react-prompt/"
language: "zh-CN"
---

# LLM 的 ReAct 模式

大语言模型的 Prompt ，通过加入各类控制，让语言模型可以在碰到问题变相使用外部工具来解决

2023年4月11日·69 字·1 分钟

[LLM](https://xingkaixin.me/tags/LLM/)Prompt

![llm-react-prompt](https://xingkaixin.me/cover/llm-react-prompt.webp)

参考论文：

[ReAct: Synergizing Reasoning and Acting in Language Models](https://arxiv.org/abs/2210.03629)

### 实现图

![react](https://xingkaixin.me/posts/images/llm-react-prompt/react.webp)

![output](https://xingkaixin.me/posts/images/llm-react-prompt/output.webp)

### Prompt

```
请尽力回答以下问题。您可以使用以下工具：
CalculatorTool: 运行计算并返回数字 - 使用Python.
GithubUserInfoTool: 返回Github用户信息，包括位置、个人简介、粉丝数、关注数、公共仓库数量、创建时间等，并以json格式呈现。操作输入为GitHub用户名（不带引号）。
请按照以下格式编写每个步骤。您可以采取多个步骤，但不要给它们编号。如果上面提供的工具无法回答问题，请随意发挥并以“Final Answer:”开头开始回复。
关于与上述工具无关的事情，您不需要思考和行动模式。
Question: 你必须回答的输入问题
Thought: 你应该时刻考虑要做什么。
Action: 需要执行的操作，应该是 [CalculatorTool, GithubUserInfoTool] 中的一个（如果需要,不要同时出现多个Action）。
Action Input: the input to the action
Observation: the result of the action
… (this Thought/Action/Action Input/Observation can repeat N times)
Thought: 我现在知道Final Answer。
Final Answer: the final answer to the original input question
```

### 核心

为了实现这个模式，在 API 请求时，需要使用 `stop` 让`gpt` 模型响应到指定词时中断，这样就可以在代码中实现 `Action` 的要求并提供 `Observation`，并再次请求。

## 版权声明

作者

XingKaiXin

标题

LLM 的 ReAct 模式

发布时间

2023年4月11日

文章链接

[https://xingkaixin.me/posts/llm-react-prompt/](https://xingkaixin.me/posts/llm-react-prompt/)

本作品采用[CC BY-NC-ND 4.0 DEED](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh-hans)许可。

导览

展开导览
