mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 23:25:54 +08:00
fix: minimax streaming function_call message (#4271)
This commit is contained in:
parent
a80fe20456
commit
8cc492721b
@ -23,7 +23,7 @@ class MinimaxChatCompletionPro:
|
|||||||
def generate(self, model: str, api_key: str, group_id: str,
|
def generate(self, model: str, api_key: str, group_id: str,
|
||||||
prompt_messages: list[MinimaxMessage], model_parameters: dict,
|
prompt_messages: list[MinimaxMessage], model_parameters: dict,
|
||||||
tools: list[dict[str, Any]], stop: list[str] | None, stream: bool, user: str) \
|
tools: list[dict[str, Any]], stop: list[str] | None, stream: bool, user: str) \
|
||||||
-> Union[MinimaxMessage, Generator[MinimaxMessage, None, None]]:
|
-> Union[MinimaxMessage, Generator[MinimaxMessage, None, None]]:
|
||||||
"""
|
"""
|
||||||
generate chat completion
|
generate chat completion
|
||||||
"""
|
"""
|
||||||
@ -89,7 +89,7 @@ class MinimaxChatCompletionPro:
|
|||||||
|
|
||||||
if tools:
|
if tools:
|
||||||
body['functions'] = tools
|
body['functions'] = tools
|
||||||
body['function_call'] = { 'type': 'auto' }
|
body['function_call'] = {'type': 'auto'}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = post(
|
response = post(
|
||||||
@ -144,7 +144,6 @@ class MinimaxChatCompletionPro:
|
|||||||
"""
|
"""
|
||||||
handle stream chat generate response
|
handle stream chat generate response
|
||||||
"""
|
"""
|
||||||
function_call_storage = None
|
|
||||||
for line in response.iter_lines():
|
for line in response.iter_lines():
|
||||||
if not line:
|
if not line:
|
||||||
continue
|
continue
|
||||||
@ -158,54 +157,41 @@ class MinimaxChatCompletionPro:
|
|||||||
msg = data['base_resp']['status_msg']
|
msg = data['base_resp']['status_msg']
|
||||||
self._handle_error(code, msg)
|
self._handle_error(code, msg)
|
||||||
|
|
||||||
|
# final chunk
|
||||||
if data['reply'] or 'usage' in data and data['usage']:
|
if data['reply'] or 'usage' in data and data['usage']:
|
||||||
total_tokens = data['usage']['total_tokens']
|
total_tokens = data['usage']['total_tokens']
|
||||||
message = MinimaxMessage(
|
minimax_message = MinimaxMessage(
|
||||||
role=MinimaxMessage.Role.ASSISTANT.value,
|
role=MinimaxMessage.Role.ASSISTANT.value,
|
||||||
content=''
|
content=''
|
||||||
)
|
)
|
||||||
message.usage = {
|
minimax_message.usage = {
|
||||||
'prompt_tokens': 0,
|
'prompt_tokens': 0,
|
||||||
'completion_tokens': total_tokens,
|
'completion_tokens': total_tokens,
|
||||||
'total_tokens': total_tokens
|
'total_tokens': total_tokens
|
||||||
}
|
}
|
||||||
message.stop_reason = data['choices'][0]['finish_reason']
|
minimax_message.stop_reason = data['choices'][0]['finish_reason']
|
||||||
|
|
||||||
if function_call_storage:
|
choices = data.get('choices', [])
|
||||||
function_call_message = MinimaxMessage(content='', role=MinimaxMessage.Role.ASSISTANT.value)
|
if len(choices) > 0:
|
||||||
function_call_message.function_call = function_call_storage
|
for choice in choices:
|
||||||
yield function_call_message
|
message = choice['messages'][0]
|
||||||
|
# append function_call message
|
||||||
|
if 'function_call' in message:
|
||||||
|
function_call_message = MinimaxMessage(content='', role=MinimaxMessage.Role.ASSISTANT.value)
|
||||||
|
function_call_message.function_call = message['function_call']
|
||||||
|
yield function_call_message
|
||||||
|
|
||||||
yield message
|
yield minimax_message
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# partial chunk
|
||||||
choices = data.get('choices', [])
|
choices = data.get('choices', [])
|
||||||
if len(choices) == 0:
|
if len(choices) == 0:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for choice in choices:
|
for choice in choices:
|
||||||
message = choice['messages'][0]
|
message = choice['messages'][0]
|
||||||
|
# append text message
|
||||||
if 'function_call' in message:
|
|
||||||
if not function_call_storage:
|
|
||||||
function_call_storage = message['function_call']
|
|
||||||
if 'arguments' not in function_call_storage or not function_call_storage['arguments']:
|
|
||||||
function_call_storage['arguments'] = ''
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
function_call_storage['arguments'] += message['function_call']['arguments']
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if function_call_storage:
|
|
||||||
message['function_call'] = function_call_storage
|
|
||||||
function_call_storage = None
|
|
||||||
|
|
||||||
minimax_message = MinimaxMessage(content='', role=MinimaxMessage.Role.ASSISTANT.value)
|
|
||||||
|
|
||||||
if 'function_call' in message:
|
|
||||||
minimax_message.function_call = message['function_call']
|
|
||||||
|
|
||||||
if 'text' in message:
|
if 'text' in message:
|
||||||
minimax_message.content = message['text']
|
minimax_message = MinimaxMessage(content=message['text'], role=MinimaxMessage.Role.ASSISTANT.value)
|
||||||
|
yield minimax_message
|
||||||
yield minimax_message
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user