Skip to content

Commit c9d6092

Browse files
committed
feat: improve tool use robustness
1 parent 16c7dd3 commit c9d6092

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

llama_cpp/llama_chat_format.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3453,7 +3453,7 @@ def _stream_tool_calls(
34533453
completions: List[llama_types.CreateCompletionResponse] = []
34543454
completions_tool_name: List[str] = []
34553455
finish_reason_chat_chunk = None
3456-
while tool is not None:
3456+
while tool is not None and len(completions) <= 16:
34573457
# Generate the parameter values for the selected tool
34583458
prompt += f"functions.{tool_name}:\n"
34593459
try:
@@ -3628,11 +3628,11 @@ def chatml_function_calling(
36283628
# Collect the llama.create_completion keyword arguments so we don't have to repeat these with
36293629
# each completion call
36303630
stop = (
3631-
[stop, "<|im_end|>"]
3631+
[stop, "<|im_end|>", "|im_end|>"]
36323632
if isinstance(stop, str)
3633-
else [*stop, "<|im_end|>"]
3633+
else [*stop, "<|im_end|>", "|im_end|>"]
36343634
if stop
3635-
else ["<|im_end|>"]
3635+
else ["<|im_end|>", "|im_end|>"]
36363636
)
36373637
grammar = ( # It is assumed the grammar applies to messages only, not tool calls
36383638
grammar
@@ -3726,9 +3726,12 @@ def chatml_function_calling(
37263726

37273727
# Case 2 step 2A: Respond with a message
37283728
if tool_name is None:
3729+
prompt = template_renderer.render(
3730+
messages=messages, tools=[], tool_calls=None, add_generation_prompt=True
3731+
)
37293732
return _convert_completion_to_chat(
37303733
llama.create_completion(
3731-
prompt=prompt + "message:\n",
3734+
prompt=prompt,
37323735
**completion_kwargs, # type: ignore[arg-type]
37333736
logprobs=top_logprobs if logprobs else None,
37343737
),
@@ -3748,7 +3751,7 @@ def chatml_function_calling(
37483751
tool = next((tool for tool in tools if tool["function"]["name"] == tool_name), None)
37493752
completions: List[llama_types.CreateCompletionResponse] = []
37503753
completions_tool_name: List[str] = []
3751-
while tool is not None:
3754+
while tool is not None and len(completions) <= 16:
37523755
# Generate the parameter values for the selected tool
37533756
prompt += f"functions.{tool_name}:\n"
37543757
try:

0 commit comments

Comments
 (0)