Skip to content

Commit 598b8a3

Browse files
authored
🐛 a better way to distinguish between executable and generated code #832
2 parents a7cf50e + 8a8746a commit 598b8a3

File tree

10 files changed

+883
-186
lines changed

10 files changed

+883
-186
lines changed

backend/prompts/managed_system_prompt_template.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ system_prompt: |-
6161
- 用简单的Python编写代码
6262
- 遵循python代码规范和python语法
6363
- 根据格式规范正确调用工具
64+
- 考虑到代码执行与展示用户代码的区别,使用'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'表达运行代码,使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码
65+
- 注意运行的代码不会被用户看到,所以如果用户需要看到代码,你需要使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码。
6466
6567
3. 观察结果:
6668
- 查看代码执行结果
@@ -99,7 +101,7 @@ system_prompt: |-
99101
{{ constraint }}
100102
101103
### python代码规范
102-
1. 如果认为是需要执行的代码,代码内容以'代码:\n```py\n'开头,并以'```<end_code>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```code:语言类型\n'开头,并以'```<end_code>'标识符结尾,其中语言类型例如python、java、javascript等;
104+
1. 如果认为是需要执行的代码,代码内容以'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'标识符结尾,其中语言类型例如python、java、javascript等;
103105
2. 只使用已定义的变量,变量将在多次调用之间持续保持;
104106
3. 使用“print()”函数让下一次的模型调用看到对应变量信息;
105107
4. 正确使用工具的入参,使用关键字参数,不要用字典形式;

backend/prompts/managed_system_prompt_template_en.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ system_prompt: |-
6161
- Write code in simple Python
6262
- Follow Python coding standards and Python syntax
6363
- Call tools correctly according to format specifications
64+
- To distinguish between code execution and displaying user code, use 'Code: \n```<RUN>\n' to start executing code and '```<END_CODE>' to indicate its completion. Use 'Code: \n```<DISPLAY:language_type>\n' to start displaying code and '```<END_CODE>' to indicate its completion.
65+
- Note that executed code is not visible to users. If users need to see the code, use 'Code: \n```<DISPLAY:language_type>\n' as the start and '```<END_CODE>' to denote displayed code.
6466
6567
3. Observe Results:
6668
- View code execution results
@@ -99,7 +101,7 @@ system_prompt: |-
99101
{{ constraint }}
100102
101103
### Python Code Specifications
102-
1. If it is considered to be code that needs to be executed, the code content begins with 'code: \n```py\n' and ends with '```<end_code>'. If the code does not need to be executed for display only, the code content begins with 'code:\n```code:language_type\n', and ends with '```<end_code>', where language_type can be python, java, javascript, etc;
104+
1. If it is considered to be code that needs to be executed, the code content begins with 'code: \n```<RUN>\n' and ends with '```<END_CODE>'. If the code does not need to be executed for display only, the code content begins with 'code:\n```<DISPLAY:language_type>\n', and ends with '```<END_CODE>', where language_type can be python, java, javascript, etc;
103105
2. Only use defined variables, variables will persist between multiple calls;
104106
3. Use "print()" function to let the next model call see corresponding variable information;
105107
4. Use tool input parameters correctly, use keyword arguments, not dictionary format;

backend/prompts/manager_system_prompt_template.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ system_prompt: |-
6262
- 用简单的Python编写代码
6363
- 遵循python代码规范和python语法
6464
- 正确调用工具或助手解决问题
65+
- 考虑到代码执行与展示用户代码的区别,使用'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'表达运行代码,使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码
66+
- 注意运行的代码不会被用户看到,所以如果用户需要看到代码,你需要使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码。
6567
6668
3. 观察结果:
6769
- 查看代码执行结果
@@ -127,7 +129,7 @@ system_prompt: |-
127129
{{ constraint }}
128130
129131
### python代码规范
130-
1. 如果认为是需要执行的代码,代码内容以'代码:\n```py\n'开头,并以'```<end_code>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```code:语言类型\n'开头,并以'```<end_code>'标识符结尾,其中语言类型例如python、java、javascript等;
132+
1. 如果认为是需要执行的代码,代码内容以'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'标识符结尾,其中语言类型例如python、java、javascript等;
131133
2. 只使用已定义的变量,变量将在多次调用之间持续保持;
132134
3. 使用“print()”函数让下一次的模型调用看到对应变量信息;
133135
4. 正确使用工具/助手的入参,使用关键字参数,不要用字典形式;

backend/prompts/manager_system_prompt_template_en.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ system_prompt: |-
6262
- Write code in simple Python
6363
- Follow Python coding standards and Python syntax
6464
- Correctly call tools or agents to solve problems
65+
- To distinguish between code execution and displaying user code, use 'Code: \n```<RUN>\n' to start executing code and '```<END_CODE>' to indicate its completion. Use 'Code: \n```<DISPLAY:language_type>\n' to start displaying code and '```<END_CODE>' to indicate its completion.
66+
- Note that executed code is not visible to users. If users need to see the code, use 'Code: \n```<DISPLAY:language_type>\n' as the start and '```<END_CODE>' to denote displayed code.
6567
6668
3. Observe Results:
6769
- View code execution results
@@ -127,7 +129,7 @@ system_prompt: |-
127129
{{ constraint }}
128130
129131
### Python Code Specifications
130-
1. If it is considered to be code that needs to be executed, the code content begins with 'code: \n```py\n' and ends with '```<end_code>'. If the code does not need to be executed for display only, the code content begins with 'code: \n```code:language_type\n', and ends with '```<end_code>', where language_type can be python, java, javascript, etc;
132+
1. If it is considered to be code that needs to be executed, the code content begins with 'code: \n```<RUN>\n' and ends with '```<END_CODE>'. If the code does not need to be executed for display only, the code content begins with 'code: \n```<DISPLAY:language_type>\n', and ends with '```<END_CODE>', where language_type can be python, java, javascript, etc;
131133
2. Only use defined variables, variables will persist between multiple calls;
132134
3. Use "print()" function to let the next model call see corresponding variable information;
133135
4. Use tool/agent input parameters correctly, use keyword arguments, not dictionary format;

backend/prompts/utils/prompt_generate.yaml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
5252
- 用简单的Python编写代码
5353
- 遵循python代码规范和python语法
5454
- 根据格式规范正确调用工具/助手
55+
- 考虑到代码执行与展示用户代码的区别,使用'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'表达运行代码,使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码
56+
- 注意运行的代码不会被用户看到,所以如果用户需要看到代码,你需要使用'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'表达展示代码。
5557
5658
3. 观察结果:
5759
- 查看代码执行结果
5860
5961
在思考结束后,当Agent认为可以回答用户问题,那么可以不生成代码,直接生成最终回答给到用户并停止循环。
6062
6163
### python代码规范
62-
1. 如果认为是需要执行的代码,代码内容以'代码:\n```py\n'开头,并以'```<end_code>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```code:语言类型\n'开头,并以'```<end_code>'标识符结尾,其中语言类型例如python、java、javascript等;
64+
1. 如果认为是需要执行的代码,代码内容以'代码:\n```<RUN>\n'开头,并以'```<END_CODE>'标识符结尾。如果是不需要执行仅用于展示的代码,代码内容以'代码:\n```<DISPLAY:语言类型>\n'开头,并以'```<END_CODE>'标识符结尾,其中语言类型例如python、java、javascript等;
6365
2. 只使用已定义的变量,变量将在多次调用之间持续保持;
6466
3. 使用“print()”函数让下一次的模型调用看到对应变量信息;
6567
4. 正确使用工具/助手的入参,使用关键字参数,不要用字典形式;
@@ -76,18 +78,18 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
7678
7779
思考:我先使用knowledge_base_search工具查找本地知识库是否有相关信息。
7880
代码:
79-
```py
81+
```<RUN>
8082
knowledge_info = knowledge_base_search(query="东方明珠 介绍", index_names=["本地知识库1", "本地知识库2"])
8183
print(knowledge_info)
82-
```<end_code>
84+
```<END_CODE>
8385
观察结果:未找到查询"东方明珠 介绍"的结果。检索结果难以支撑回答。
8486
8587
思考:从本地知识库中没有找到相关信息,我需要使用web_search工具查询网络信息。
8688
代码:
87-
```py
89+
```<RUN>
8890
web_info = web_search(query="东方明珠 介绍")
8991
print(web_info)
90-
```<end_code>
92+
```<END_CODE>
9193
观察结果:东方明珠广播电视塔位于中国上海市浦东新区陆家嘴...
9294
9395
思考:我已经获得了有关信息,现在我将生成最终回答。
@@ -99,10 +101,10 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
99101
100102
思考:我需要调用旅程规划助手来规划出行。
101103
代码:
102-
```py
104+
```<RUN>
103105
itinerary_result = travel_planning_assistant(task="帮我规划明天从上海出发去北京的行程")
104106
print(itinerary_result)
105-
```<end_code>
107+
```<END_CODE>
106108
观察结果:明天从上海出发去北京的行程规划,包括交通、住宿、景点等。
107109
108110
思考:我已经获得了出行规划,现在我将生成最终回答。
@@ -114,18 +116,18 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
114116
115117
思考:我需要先获取天气数据,然后让分析助手帮我分析。
116118
代码:
117-
```py
119+
```<RUN>
118120
weather_data = weather_api(city="北京")
119121
print(weather_data)
120-
```<end_code>
122+
```<END_CODE>
121123
观察结果:{"temperature": 25, "humidity": 60%, "condition": "晴天"}
122124
123125
思考:现在我有天气数据了,让分析助手帮我分析这些数据。
124126
代码:
125-
```py
127+
```<RUN>
126128
analysis_result = data_analysis_assistant(task="分析今天的天气数据:温度25度,湿度60%,晴天")
127129
print(analysis_result)
128-
```<end_code>
130+
```<END_CODE>
129131
观察结果:今天天气适宜,温度适中,湿度正常,适合户外活动。
130132
131133
思考:我已经获得了天气数据和分析结果,现在我将生成最终回答。
@@ -147,9 +149,9 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
147149
148150
任务5:"帮我用Python写一个快速排序的代码"
149151
150-
思考:我需要直接写一个python代码,此代码仅用于展示,因此我以'代码:\n```code:python\n'开头。
152+
思考:我需要直接写一个python代码,此代码仅用于展示,因此我以'代码:\n```<DISPLAY:python>\n'开头。
151153
代码:
152-
```code:python
154+
```<DISPLAY:python>
153155
def quick_sort(arr):
154156
if len(arr) <= 1:
155157
return arr
@@ -158,12 +160,12 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
158160
middle = [x for x in arr if x == pivot]
159161
right = [x for x in arr if x > pivot]
160162
return quick_sort(left) + middle + quick_sort(right)
161-
```<end_code>
163+
```<END_CODE>
162164
观察结果:快速排序的python代码。
163165
164166
思考:我已经获得了快速排序的python代码,现在我将生成最终回答。
165167
快速排序的python代码如下:
166-
```code:python
168+
```<DISPLAY:python>
167169
def quick_sort(arr):
168170
if len(arr) <= 1:
169171
return arr
@@ -172,7 +174,7 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
172174
middle = [x for x in arr if x == pivot]
173175
right = [x for x in arr if x > pivot]
174176
return quick_sort(left) + middle + quick_sort(right)
175-
```<end_code>
177+
```<END_CODE>
176178
177179
---
178180

backend/prompts/utils/prompt_generate_en.yaml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
5353
- Write code in simple Python
5454
- Follow Python coding standards and Python syntax
5555
- Call tools/assistants correctly according to format specifications
56+
- To distinguish between code execution and displaying user code, use 'Code: \n```<RUN>\n' to start executing code and '```<END_CODE>' to indicate its completion. Use 'Code: \n```<DISPLAY:language_type>\n' to start displaying code and '```<END_CODE>' to indicate its completion.
57+
- Note that executed code is not visible to users. If users need to see the code, use 'Code: \n```<DISPLAY:language_type>\n' as the start and '```<END_CODE>' to denote displayed code.
5658
5759
3. Observe Results:
5860
- View code execution results
5961
6062
After thinking, when you believe you can answer the user's question, you can generate a final answer directly to the user without generating code and stop the loop.
6163
6264
### Python Code Specifications
63-
1. If it is considered to be code that needs to be executed, the code content begins with 'Code:\n```py\n' and ends with '```<end_code>'. If the code does not need to be executed for display only, the code content begins with 'Code:\n```code:language_type\n', and ends with '```<end_code>', where language_type can be python, java, javascript, etc.;
65+
1. If it is considered to be code that needs to be executed, the code content begins with 'Code:\n```<RUN>\n' and ends with '```<END_CODE>'. If the code does not need to be executed for display only, the code content begins with 'Code:\n```<DISPLAY:language_type>\n', and ends with '```<END_CODE>', where language_type can be python, java, javascript, etc.;
6466
2. Only use defined variables, variables will persist between multiple calls;
6567
3. Use "print()" function to let the next model call see corresponding variable information;
6668
4. Use tool/assistant input parameters correctly, use keyword arguments, not dictionary format;
@@ -77,18 +79,18 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
7779
7880
Think: I will first use the knowledge_base_search tool to find if there is relevant information in the local knowledge base.
7981
Code:
80-
```py
82+
```<RUN>
8183
knowledge_info = knowledge_base_search(query="Oriental Pearl Tower introduction", index_names=["local_knowledge_base1", "local_knowledge_base2"])
8284
print(knowledge_info)
83-
```<end_code>
85+
```<END_CODE>
8486
Observe Results: No results found for query "Oriental Pearl Tower introduction". The search results are insufficient to support an answer.
8587
8688
Think: Since no relevant information was found in the local knowledge base, I need to use the web_search tool to query network information.
8789
Code:
88-
```py
90+
```<RUN>
8991
web_info = web_search(query="Oriental Pearl Tower introduction")
9092
print(web_info)
91-
```<end_code>
93+
```<END_CODE>
9294
Observe Results: The Oriental Pearl TV Tower is located in Lujiazui, Pudong New Area, Shanghai, China...
9395
9496
Think: I have obtained the relevant information, now I will generate the final answer.
@@ -100,10 +102,10 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
100102
101103
Think: I need to call the travel planning assistant to plan the trip.
102104
Code:
103-
```py
105+
```<RUN>
104106
itinerary_result = travel_planning_assistant(task="Help me plan tomorrow's trip from Shanghai to Beijing")
105107
print(itinerary_result)
106-
```<end_code>
108+
```<END_CODE>
107109
Observe Results: Tomorrow's trip planning from Shanghai to Beijing, including transportation, accommodation, attractions, etc.
108110
109111
Think: I have obtained the travel planning, now I will generate the final answer.
@@ -115,18 +117,18 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
115117
116118
Think: I need to first get weather data, then let the analysis assistant help me analyze.
117119
Code:
118-
```py
120+
```<RUN>
119121
weather_data = weather_api(city="Beijing")
120122
print(weather_data)
121-
```<end_code>
123+
```<END_CODE>
122124
Observe Results: {"temperature": 25, "humidity": "60%", "condition": "sunny"}
123125
124126
Think: Now I have weather data, let the analysis assistant help me analyze this data.
125127
Code:
126-
```py
128+
```<RUN>
127129
analysis_result = data_analysis_assistant(task="Analyze today's weather data: temperature 25 degrees, humidity 60%, sunny")
128130
print(analysis_result)
129-
```<end_code>
131+
```<END_CODE>
130132
Observe Results: Today's weather is suitable, temperature is moderate, humidity is normal, suitable for outdoor activities.
131133
132134
Think: I have obtained weather data and analysis results, now I will generate the final answer.
@@ -143,10 +145,10 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
143145
144146
Task 5: "Write a Python quick sort code"
145147
146-
Think: I need to write a Python code directly, this code is only for display, so I start with 'Code:\n```code:python\n'.
148+
Think: I need to write a Python code directly, this code is only for display, so I start with 'Code:\n```<DISPLAY:python>\n'.
147149
148150
Code:
149-
```code:python
151+
```<DISPLAY:python>
150152
def quick_sort(arr):
151153
if len(arr) <= 1:
152154
return arr
@@ -156,12 +158,12 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
156158
middle = [x for x in arr if x == pivot]
157159
right = [x for x in arr if x > pivot]
158160
return quick_sort(left) + middle + quick_sort(right)
159-
```<end_code>
161+
```<END_CODE>
160162
Observe Results: The Python quick sort code.
161163
162164
Think: I have obtained the Python quick sort code, now I will generate the final answer.
163165
The Python quick sort code is as follows:
164-
```code:python
166+
```<DISPLAY:python>
165167
def quick_sort(arr):
166168
if len(arr) <= 1:
167169
return arr
@@ -170,7 +172,7 @@ FEW_SHOTS_SYSTEM_PROMPT: |-
170172
middle = [x for x in arr if x == pivot]
171173
right = [x for x in arr if x > pivot]
172174
return quick_sort(left) + middle + quick_sort(right)
173-
```<end_code>
175+
```<END_CODE>
174176
175177
---
176178

0 commit comments

Comments
 (0)