Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions tinker_cookbook/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,20 @@ def _render_message(self, idx: int, message: Message) -> tuple[list[int], list[i
# <think> in the assistant messages, we so don't need to re-add it in those cases.
ob_str += "<think>\n"
# Observation (prompt) part
ac_str = f"{ac_content}<|im_end|>"
if "tool_calls" in message:
ac_content += "\n".join(
[
f"<tool_call>\n{json.dumps(tool_call)}\n</tool_call>"
for tool_call in message["tool_calls"]
]
)
ac_content += "<|im_end|>"
# Action part
ac_tail_str = "" # No action tail needed for Qwen format
# Action part that's only included in the last message in SFT
return (
self.tokenizer.encode(ob_str, add_special_tokens=False),
self.tokenizer.encode(ac_str, add_special_tokens=False),
self.tokenizer.encode(ac_content, add_special_tokens=False),
self.tokenizer.encode(ac_tail_str, add_special_tokens=False),
)

Expand Down Expand Up @@ -405,11 +412,10 @@ def parse_response(self, response: list[int]) -> tuple[Message, bool]:
if not parse_success:
return assistant_message, False

# NOTE:
# we use the <function_call>...</function_call> tag to wrap the tool call.
match = re.search(
r"<function_call>(.*?)</function_call>", assistant_message["content"], re.DOTALL
)
# Follow Qwen docs and Qwen-Agent's tool calling prompt to use <tool_call>...</tool_call> tags to wrap the tool call.
# - https://qwen.readthedocs.io/en/latest/getting_started/concepts.html#tool-calling
# - https://github.com/QwenLM/Qwen-Agent/blob/main/qwen_agent/llm/fncall_prompts/nous_fncall_prompt.py#L279-L282
match = re.search(r"<tool_call>(.*?)</tool_call>", assistant_message["content"], re.DOTALL)
if match:
tool_calls = self._parse_tool_call(match.group(1))
if tool_calls is None:
Expand Down