-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathnous_fncall_prompt.py
273 lines (239 loc) · 11.5 KB
/
nous_fncall_prompt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import copy
import json
import os
from typing import List, Literal, Union
import json5
from qwen_agent.llm.fncall_prompts.base_fncall_prompt import BaseFnCallPrompt
from qwen_agent.llm.schema import ASSISTANT, FUNCTION, SYSTEM, USER, ContentItem, FunctionCall, Message
class NousFnCallPrompt(BaseFnCallPrompt):
def preprocess_fncall_messages(self,
messages: List[Message],
functions: List[dict],
lang: Literal['en', 'zh'],
parallel_function_calls: bool = True,
function_choice: Union[Literal['auto'], str] = 'auto',
**kwargs) -> List[Message]:
del lang # ignored
del parallel_function_calls # ignored
if function_choice != 'auto':
raise NotImplementedError
ori_messages = messages
# Change function_call responses to plaintext responses:
messages = []
for msg in copy.deepcopy(ori_messages):
role, content, reasoning_content = msg.role, msg.content, msg.reasoning_content
if role in (SYSTEM, USER):
messages.append(msg)
elif role == ASSISTANT:
content = (content or [])
fn_call = msg.function_call
if fn_call:
if (not SPECIAL_CODE_MODE) or (CODE_TOOL_PATTERN not in fn_call.name):
fc = {'name': fn_call.name, 'arguments': json5.loads(fn_call.arguments)}
fc = json.dumps(fc, ensure_ascii=False)
fc = f'<tool_call>\n{fc}\n</tool_call>'
else:
para = json5.loads(fn_call.arguments)
code = para['code']
para['code'] = ''
fc = {'name': fn_call.name, 'arguments': para}
fc = json.dumps(fc, ensure_ascii=False)
fc = f'<tool_call>\n{fc}\n<code>\n{code}\n</code>\n</tool_call>'
content.append(ContentItem(text=fc))
if messages and messages[-1].role == ASSISTANT:
if messages[-1].content and messages[-1].content[-1].text and (
not messages[-1].content[-1].text.endswith('\n')):
messages[-1].content.append(ContentItem(text='\n'))
messages[-1].content.extend(content)
else:
# TODO: Assuming there will only be one continuous reasoning_content here
messages.append(Message(role=role, content=content, reasoning_content=reasoning_content))
elif role == FUNCTION:
assert isinstance(content, list)
assert len(content) == 1
assert content[0].text
fc = f'<tool_response>\n{content[0].text}\n</tool_response>'
content = [ContentItem(text=fc)]
if messages[-1].role == USER:
messages[-1].content.append(ContentItem(text='\n'))
messages[-1].content.extend(content)
else:
messages.append(Message(role=USER, content=content))
else:
raise TypeError
tool_descs = [{'type': 'function', 'function': f} for f in functions]
tool_names = [function.get('name_for_model', function.get('name', '')) for function in functions]
tool_descs = '\n'.join([json.dumps(f, ensure_ascii=False) for f in tool_descs])
if SPECIAL_CODE_MODE and any([CODE_TOOL_PATTERN in x for x in tool_names]):
tool_system = FN_CALL_TEMPLATE_WITH_CI.format(tool_descs=tool_descs)
else:
tool_system = FN_CALL_TEMPLATE.format(tool_descs=tool_descs)
if messages and messages[0].role == SYSTEM:
messages[0].content.append(ContentItem(text='\n\n' + tool_system))
else:
messages = [Message(role=SYSTEM, content=[ContentItem(text=tool_system)])] + messages
return messages
def postprocess_fncall_messages(
self,
messages: List[Message],
parallel_function_calls: bool = True,
function_choice: Union[Literal['auto'], str] = 'auto',
thought_in_content: bool = False,
) -> List[Message]:
if function_choice != 'auto':
raise NotImplementedError
# Convert plaintext responses to function_call responses:
new_messages = []
for msg in messages:
role, content, reasoning_content, extra = msg.role, msg.content, msg.reasoning_content, msg.extra
assert isinstance(content, list)
if role in (SYSTEM, USER):
new_messages.append(
Message(role=role, content=content, reasoning_content=reasoning_content, extra=extra))
continue
# Reasoning content is placed in a separate message
if reasoning_content:
new_messages.append(Message(role=role, content='', reasoning_content=reasoning_content, extra=extra))
new_content = []
for item in content:
item_type, item_text = item.get_type_and_value()
if item_type != 'text': # multimodal
new_content.append(item)
continue
if thought_in_content:
if '</think>' not in item_text:
new_content.append(ContentItem(text=item_text))
continue
_item_text = item_text.split('</think>')
# assert len(_item_text) == 2
new_content.append(ContentItem(text='</think>'.join(_item_text[:-1]) + '</think>'))
item_text = _item_text[-1]
i = item_text.find('<tool_call>')
# If no function call:
if i < 0:
show_text = item_text
if show_text:
new_content.append(ContentItem(text=show_text))
continue
# split tool-call to separate assistant msg
tool_call_list = item_text.split('<tool_call>')
pre_thought = tool_call_list[0]
if pre_thought.strip():
new_content.append(ContentItem(text=pre_thought))
for txt in tool_call_list[1:]:
if not txt.strip():
continue
if '</tool_call>' not in txt:
# incomplete </tool_call>: This is to better represent incomplete tool calls in streaming output
fn_name, fn_args = extract_fn(txt)
if fn_name: # need to call function
if new_content:
new_messages.append(Message(
role=role,
content=new_content,
extra=extra,
)) # split thought and function call
new_content = []
# TODO: process incomplete tool-call messages
new_messages.append(
Message(
role=ASSISTANT,
content=[],
function_call=FunctionCall(
name=fn_name,
arguments=fn_args,
),
extra=extra,
))
continue
one_tool_call_txt = txt.split('</tool_call>')
# The complete tool-call response
if new_content:
new_messages.append(Message(
role=role,
content=new_content,
extra=extra,
)) # split thought and function call
new_content = []
if SPECIAL_CODE_MODE and '<code>' in one_tool_call_txt[0] and '</code>' in one_tool_call_txt[0]:
_snips = one_tool_call_txt[0].split('<code>')
fn = None
for i, _s in enumerate(_snips):
if i == 0:
fn = json5.loads(_s)
else:
# TODO: support more flexible params
code = _s.replace('</code>', '')
fn['arguments']['code'] = code
else:
fn = json5.loads(one_tool_call_txt[0].strip())
new_messages.append(
Message(
role=ASSISTANT,
content=[],
function_call=FunctionCall(
name=fn['name'],
arguments=json.dumps(fn['arguments'], ensure_ascii=False),
),
extra=extra,
))
# Expected not to output extra tails
# if one_tool_call_txt[1].strip():
# new_content.append(ContentItem(text=one_tool_call_txt[1]))
if new_content:
new_messages.append(Message(role=role, content=new_content, extra=extra))
return new_messages
FN_CALL_TEMPLATE = """# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{tool_descs}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{{"name": <function-name>, "arguments": <args-json-object>}}
</tool_call>"""
SPECIAL_CODE_MODE = os.getenv('SPECIAL_CODE_MODE', 'false').lower() == 'true'
CODE_TOOL_PATTERN = 'code_interpreter'
FN_CALL_TEMPLATE_WITH_CI = """# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{tool_descs}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{{"name": <function-name>, "arguments": <args-json-object>}}
</tool_call>
For code parameters, use placeholders first, and then put the code within <code></code> XML tags, such as:
<tool_call>
{{"name": <function-name>, "arguments": {{"code": ""}}}}
<code>
Here is the code.
</code>
</tool_call>"""
# Mainly for removing incomplete special tokens when streaming the output
# This assumes that '<tool_call>\n{"name": "' is the special token for the NousFnCallPrompt
def remove_incomplete_special_tokens(text: str) -> str:
if text in '<tool_call>\n{"name": "':
text = ''
return text
def extract_fn(text: str):
fn_name, fn_args = '', ''
fn_name_s = '"name": "'
fn_name_e = '", "'
fn_args_s = '"arguments": '
i = text.find(fn_name_s)
k = text.find(fn_args_s)
if i > 0:
_text = text[i + len(fn_name_s):]
j = _text.find(fn_name_e)
if j > -1:
fn_name = _text[:j]
if k > 0:
fn_args = text[k + len(fn_args_s):]
if len(fn_args) > 5:
fn_args = fn_args[:-5]
else:
fn_args = ''
return fn_name, fn_args