Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e6a7e0

Browse filesBrowse files
committed
fix streaming with tool_choice/function_call
1 parent e11d44d commit 8e6a7e0
Copy full SHA for 8e6a7e0

File tree

Expand file treeCollapse file tree

1 file changed

+44
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+44
-1
lines changed

‎llama_cpp/llama_chat_format.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_chat_format.py
+44-1Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,33 @@ def generate_streaming(tools, functions, function_call, prompt):
20112011
tool_id = "".join([random.choice(string.ascii_letters + string.digits) for _ in range(24)])
20122012
completion = create_completion(prompt=prompt, stop=stops, grammar=grammar)
20132013
completion_text = ""
2014+
first = True
20142015
for chunk in completion:
2016+
# Yield the tool/function name first
2017+
if first:
2018+
if tools is not None:
2019+
func_call_dict = {
2020+
"tool_calls": [
2021+
{
2022+
"index": 0,
2023+
"id": "call_" + tool_id,
2024+
"type": "function",
2025+
"function": {"name": function_call["name"], "arguments": ""},
2026+
}
2027+
]
2028+
}
2029+
else:
2030+
func_call_dict = {"function_call": {"name": function_call["name"], "arguments": ""}}
2031+
yield llama_types.CreateChatCompletionStreamResponse(
2032+
id="chat" + chunk["id"],
2033+
object="chat.completion.chunk",
2034+
created=chunk["created"],
2035+
model=chunk["model"],
2036+
choices=[
2037+
{"index": 0, "logprobs": None, "delta": {"role": None, "content": None, **func_call_dict}}
2038+
],
2039+
)
2040+
first = False
20152041
if tools is not None:
20162042
func_call_dict = {
20172043
"tool_calls": [
@@ -2046,6 +2072,23 @@ def generate_streaming(tools, functions, function_call, prompt):
20462072
}
20472073
],
20482074
)
2075+
# Yield tool_call/function_call stop message
2076+
yield {
2077+
"id": "chat" + chunk["id"],
2078+
"object": "chat.completion.chunk",
2079+
"created": chunk["created"],
2080+
"model": chunk["model"],
2081+
"choices": [
2082+
{
2083+
"index": 0,
2084+
"finish_reason": "tool_calls" if tools is not None else "function_call",
2085+
"logprobs": None,
2086+
"delta": {
2087+
"role": None, "content": None, "function_call": None, "tool_calls": None
2088+
},
2089+
}
2090+
],
2091+
}
20492092
# If "auto" or no tool_choice/function_call
20502093
elif isinstance(function_call, str) and function_call == "auto":
20512094
tool_index = 0
@@ -2240,7 +2283,7 @@ def generate_streaming(tools, functions, function_call, prompt):
22402283
prompt += "\n<|from|>assistant\n<|recipient|>"
22412284
tool_index += 1
22422285
else:
2243-
# Yield tool_call stop message
2286+
# Yield tool_call/function_call stop message
22442287
yield {
22452288
"id": "chat" + chunk_id,
22462289
"object": "chat.completion.chunk",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.