@@ -2078,11 +2078,21 @@ def create_completion(stop):
2078
2078
completion_text = completion ["choices" ][0 ]["text" ]
2079
2079
completion_tokens += completion ["usage" ]["completion_tokens" ]
2080
2080
if function_name == "all" :
2081
- content += completion_text .removesuffix ("\n <|from|>assistant\n " ).removesuffix ("\n <|from|> assistant\n " )
2081
+ if completion_text .endswith ("\n <|from|>assistant\n " ):
2082
+ content += completion_text [:- len ("\n <|from|>assistant\n " )]
2083
+ if completion_text .endswith ("\n <|from|> assistant\n " ):
2084
+ content += completion_text [- len ("\n <|from|> assistant\n " )]
2085
+ else :
2086
+ content += completion_text
2082
2087
content = content .lstrip ()
2083
2088
# Check whether the model wants to generate another turn
2084
2089
if "<|from|> assistant" in completion_text or "<|from|>assistant" in completion_text :
2085
- cleaned_completion_text = completion_text .removesuffix ("\n <|from|>assistant\n " ).removesuffix ("\n <|from|> assistant\n " ).strip ()
2090
+ if completion_text .endswith ("\n <|from|>assistant\n " ):
2091
+ cleaned_completion_text = completion_text [:- len ("\n <|from|>assistant\n " )].strip ()
2092
+ elif completion_text .endswith ("\n <|from|> assistant\n " ):
2093
+ cleaned_completion_text = completion_text [- len ("\n <|from|> assistant\n " )].strip ()
2094
+ else :
2095
+ cleaned_completion_text = completion_text .strip ()
2086
2096
prompt += f"{ cleaned_completion_text } \n <|from|>assistant\n <|recipient|>"
2087
2097
else :
2088
2098
break
@@ -2125,8 +2135,9 @@ def create_completion(stop):
2125
2135
"function_call" : {
2126
2136
"name" : tool_calls [0 ]["function" ]["name" ],
2127
2137
"arguments" : tool_calls [0 ]["function" ]["arguments" ],
2128
- }
2129
- } if len (tool_calls ) == 1 else {}
2138
+ } if len (tool_calls ) > 0 and tools is None else None ,
2139
+ "tool_calls" : tool_calls if len (tool_calls ) > 0 and tools is not None else None ,
2140
+ }
2130
2141
completion ["usage" ]["completion_tokens" ] = completion_tokens
2131
2142
return llama_types .CreateChatCompletionResponse (
2132
2143
id = "chat" + completion ["id" ],
@@ -2140,7 +2151,7 @@ def create_completion(stop):
2140
2151
"message" : {
2141
2152
"role" : "assistant" ,
2142
2153
"content" : None if content == "" else content ,
2143
- "tool_calls" : tool_calls ,
2154
+ "tool_calls" : tool_calls if tools is not None else None ,
2144
2155
** function_call_dict ,
2145
2156
},
2146
2157
"finish_reason" : "tool_calls" if len (tool_calls ) > 0 else "stop" ,
0 commit comments