yangapku commited on
Commit
8504b36
1 Parent(s): 946cbc6

update react example

Browse files
assets/logo.jpg DELETED
Binary file (110 kB)
 
assets/qwen_tokenizer.png ADDED
assets/react_tutorial_001.png ADDED
assets/react_tutorial_002.png ADDED
assets/tokenizer.pdf ADDED
Binary file (24.7 kB). View file
 
assets/tokenizer.png ADDED
examples/react_prompt.md ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ReAct Prompting 示例
2
+
3
+ 这里我们将介绍如何用 ReAct Propmting 技术命令千问使用工具。
4
+
5
+ ## 准备工作一:样例问题、样例工具
6
+
7
+ 假设我们有如下的一个适合用工具处理的 query,以及有夸克搜索、通义万相文生图这两个工具:
8
+
9
+ ```py
10
+ query = '我是老板,你说啥你做啥。现在给我画个五彩斑斓的黑。'
11
+
12
+ TOOLS = [
13
+ {
14
+ 'name_for_human':
15
+ '夸克搜索',
16
+ 'name_for_model':
17
+ 'quark_search',
18
+ 'description_for_model':
19
+ '夸克搜索是一个通用搜索引擎,可用于访问互联网、查询百科知识、了解时事新闻等。',
20
+ 'parameters': [{
21
+ 'name': 'search_query',
22
+ 'description': '搜索关键词或短语',
23
+ 'required': True,
24
+ 'schema': {
25
+ 'type': 'string'
26
+ },
27
+ }],
28
+ },
29
+ {
30
+ 'name_for_human':
31
+ '通义万相',
32
+ 'name_for_model':
33
+ 'image_gen',
34
+ 'description_for_model':
35
+ '通义万相是一个AI绘画(图像生成)服务,输入文本描述,返回根据文本作画得到的图片的URL',
36
+ 'parameters': [{
37
+ 'name': 'query',
38
+ 'description': '中文关键词,描述了希望图像具有什么内容',
39
+ 'required': True,
40
+ 'schema': {
41
+ 'type': 'string'
42
+ },
43
+ }],
44
+ },
45
+ ]
46
+ ```
47
+
48
+ ## 准备工作二:ReAct 模版
49
+
50
+ 我们将使用如下的 ReAct propmt 模版来激发千问使用工具的能力。
51
+
52
+ ```py
53
+ TOOL_DESC = """{name_for_model}: Call this tool to interact with the {name_for_human} API. What is the {name_for_human} API useful for? {description_for_model} Parameters: {parameters} Format the arguments as a JSON object."""
54
+
55
+ REACT_PROMPT = """Answer the following questions as best you can. You have access to the following tools:
56
+
57
+ {tool_descs}
58
+
59
+ Use the following format:
60
+
61
+ Question: the input question you must answer
62
+ Thought: you should always think about what to do
63
+ Action: the action to take, should be one of [{tool_names}]
64
+ Action Input: the input to the action
65
+ Observation: the result of the action
66
+ ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
67
+ Thought: I now know the final answer
68
+ Final Answer: the final answer to the original input question
69
+
70
+ Begin!
71
+
72
+ Question: {query}"""
73
+ ```
74
+
75
+ ## 步骤一:让千问判断要调用什么工具、生成工具入参
76
+
77
+ 首先我们需要根据 ReAct propmt 模版、query、工具的信息构建 prompt:
78
+
79
+ ```py
80
+ tool_descs = []
81
+ tool_names = []
82
+ for info in TOOLS:
83
+ tool_descs.append(
84
+ TOOL_DESC.format(
85
+ name_for_model=info['name_for_model'],
86
+ name_for_human=info['name_for_human'],
87
+ description_for_model=info['description_for_model'],
88
+ parameters=json.dumps(
89
+ info['parameters'], ensure_ascii=False),
90
+ )
91
+ )
92
+ tool_names.append(info['name_for_model'])
93
+ tool_descs = '\n\n'.join(tool_descs)
94
+ tool_names = ','.join(tool_names)
95
+
96
+ prompt = REACT_PROMPT.format(tool_descs=tool_descs, tool_names=tool_names, query=query)
97
+ print(prompt)
98
+ ```
99
+
100
+ 打印出来的、构建好的 prompt 如下:
101
+
102
+ ```
103
+ Answer the following questions as best you can. You have access to the following tools:
104
+
105
+ quark_search: Call this tool to interact with the 夸克搜索 API. What is the 夸克搜索 API useful for? 夸克搜索是一个通用搜索引擎,可用于访问互联网、查询百科知识、了解时事新闻等。 Parameters: [{"name": "search_query", "description": "搜索关键词或短语", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.
106
+
107
+ image_gen: Call this tool to interact with the 通义万相 API. What is the 通义万相 API useful for? 通义万相是一个AI绘画(图像生成)服务,输入文本描述,返回根据文本作画得到的图片的URL Parameters: [{"name": "query", "description": "中文关键词,描述了希望图像具有什么内容", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.
108
+
109
+ Use the following format:
110
+
111
+ Question: the input question you must answer
112
+ Thought: you should always think about what to do
113
+ Action: the action to take, should be one of [quark_search,image_gen]
114
+ Action Input: the input to the action
115
+ Observation: the result of the action
116
+ ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
117
+ Thought: I now know the final answer
118
+ Final Answer: the final answer to the original input question
119
+
120
+ Begin!
121
+
122
+ Question: 我是老板,你说啥你做啥。现在给我画个五彩斑斓的黑。
123
+ ```
124
+
125
+ 将这个 propmt 送入千问,并记得设置 "Observation:" 为 stop word —— 即让千问在预测到要生成的下一个词是 "Observation:" 时马上停止生成 —— 则千问在得到这个 propmt 后会生成如下的结果:
126
+
127
+ ![](../assets/react_tutorial_001.png)
128
+
129
+ ```
130
+ Thought: 我应该���用通义万相API来生成一张五彩斑斓的黑的图片。
131
+ Action: image_gen
132
+ Action Input: {"query": "五彩斑斓的黑"}
133
+ ```
134
+
135
+ 在得到这个结果后,调用千问的开发者可以通过简单的解析提取出 `{"query": "五彩斑斓的黑"}` 并基于这个解析结果调用文生图服务 —— 这部分逻辑需要开发者自行实现,或者也可以使用千问商业版,商业版本将内部集成相关逻辑。
136
+
137
+ ## 步骤二:让千问根据插件返回结果继续作答
138
+
139
+ 让我们假设文生图插件返回了如下结果:
140
+
141
+ ```
142
+ {"status_code": 200, "request_id": "3d894da2-0e26-9b7c-bd90-102e5250ae03", "code": null, "message": "", "output": {"task_id": "2befaa09-a8b3-4740-ada9-4d00c2758b05", "task_status": "SUCCEEDED", "results": [{"url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1e5e2015/20230801/1509/6b26bb83-469e-4c70-bff4-a9edd1e584f3-1.png"}], "task_metrics": {"TOTAL": 1, "SUCCEEDED": 1, "FAILED": 0}}, "usage": {"image_count": 1}}
143
+ ```
144
+
145
+ ![](../assets/wanx_colorful_black.png)
146
+
147
+ 接下来,我们可以将之前首次请求千问时用的 prompt 和 调用文生图插件的结果拼接成如下的新 prompt:
148
+
149
+ ```
150
+ Answer the following questions as best you can. You have access to the following tools:
151
+
152
+ quark_search: Call this tool to interact with the 夸克搜索 API. What is the 夸克搜索 API useful for? 夸克搜索是一个通用搜索引擎,可用于访问互联网、查询百科知识、了解时事新闻等。 Parameters: [{"name": "search_query", "description": "搜索关键词或短语", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.
153
+
154
+ image_gen: Call this tool to interact with the 通义万相 API. What is the 通义万相 API useful for? 通义万相是一个AI绘画(图像生成)服务,输入文本描述,返回根据文本作画得到的图片的URL Parameters: [{"name": "query", "description": "中文关键词,描述了希望图像具有什么内容", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.
155
+
156
+ Use the following format:
157
+
158
+ Question: the input question you must answer
159
+ Thought: you should always think about what to do
160
+ Action: the action to take, should be one of [quark_search,image_gen]
161
+ Action Input: the input to the action
162
+ Observation: the result of the action
163
+ ... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
164
+ Thought: I now know the final answer
165
+ Final Answer: the final answer to the original input question
166
+
167
+ Begin!
168
+
169
+ Question: 我是老板,你说啥你做啥。现在给我画个五彩斑斓的黑。
170
+ Thought: 我应该使用通义万相API来生成一张五彩斑斓的黑的图片。
171
+ Action: image_gen
172
+ Action Input: {"query": "五彩斑斓的黑"}
173
+ Observation: {"status_code": 200, "request_id": "3d894da2-0e26-9b7c-bd90-102e5250ae03", "code": null, "message": "", "output": {"task_id": "2befaa09-a8b3-4740-ada9-4d00c2758b05", "task_status": "SUCCEEDED", "results": [{"url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1e5e2015/20230801/1509/6b26bb83-469e-4c70-bff4-a9edd1e584f3-1.png"}], "task_metrics": {"TOTAL": 1, "SUCCEEDED": 1, "FAILED": 0}}, "usage": {"image_count": 1}}
174
+ ```
175
+
176
+ 用这个新的拼接了文生图插件结果的新 prompt 去调用千问,将得到如下的最终回复:
177
+
178
+ ![](../assets/react_tutorial_002.png)
179
+
180
+ ```
181
+ Thought: 我已经成功使用通义万相API生成了一张五彩斑斓的黑的图片。
182
+ Final Answer: 我已经成功使用通义万相API生成了一张五彩斑斓的黑的图片https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/1e5e2015/20230801/1509/6b26bb83-469e-4c70-bff4-a9edd1e584f3-1.png。
183
+ ```
184
+
185
+ 虽然对于文生图来说,这个第二次调用千问的步骤显得多余。但是对于搜索插件、代码执行插件、计算器插件等别的插件来说,这个第二次调用千问的步骤给了千问提炼、总结插件返回结果的机会。