mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-09-26 20:31:14 +08:00
Refactor tool_calls handling in iter_response and async_iter_response functions for improved readability and logic consistency
This commit is contained in:
@@ -143,7 +143,9 @@ def iter_response(
|
||||
content = filter_json(content)
|
||||
chat_completion = ChatCompletion.model_construct(
|
||||
content, finish_reason, completion_id, int(time.time()), usage=usage,
|
||||
**filter_none(tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]) if tool_calls is not None else {},
|
||||
**(filter_none(
|
||||
tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
|
||||
) if tool_calls is not None else {}),
|
||||
conversation=None if conversation is None else conversation.get_dict(),
|
||||
reasoning=reasoning if reasoning else None
|
||||
)
|
||||
@@ -246,9 +248,9 @@ async def async_iter_response(
|
||||
content = filter_json(content)
|
||||
chat_completion = ChatCompletion.model_construct(
|
||||
content, finish_reason, completion_id, int(time.time()), usage=usage,
|
||||
**filter_none(
|
||||
**(filter_none(
|
||||
tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
|
||||
) if tool_calls is not None else {},
|
||||
) if tool_calls else {}),
|
||||
conversation=conversation,
|
||||
reasoning=reasoning if reasoning else None
|
||||
)
|
||||
|
@@ -279,7 +279,7 @@ class ChatCompletionDelta(BaseModel):
|
||||
def model_construct(cls, content: Optional[str]):
|
||||
if isinstance(content, Reasoning):
|
||||
return super().model_construct(role="assistant", content=None, reasoning=str(content))
|
||||
elif isinstance(content, ToolCalls):
|
||||
elif isinstance(content, ToolCalls) and content.get_list():
|
||||
return super().model_construct(role="assistant", content=None, tool_calls=[
|
||||
ToolCallModel.model_construct(**tool_call) for tool_call in content.get_list()
|
||||
])
|
||||
|
Reference in New Issue
Block a user