XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Refactor tool_calls handling in iter_response and async_iter_response functions for improved readability and logic consistency

24ebab64
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +6 -4
Modified g4f/client/__init__.py +5 -3
@@ -143,7 +143,9 @@ def iter_response(
143 143 content = filter_json(content)
144 144 chat_completion = ChatCompletion.model_construct(
145 145 content, finish_reason, completion_id, int(time.time()), usage=usage,
146 **filter_none(tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]) if tool_calls is not None else {},
146 **(filter_none(
147 tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
148 ) if tool_calls is not None else {}),
147 149 conversation=None if conversation is None else conversation.get_dict(),
148 150 reasoning=reasoning if reasoning else None
149 151 )
@@ -246,9 +248,9 @@ async def async_iter_response(
246 248 content = filter_json(content)
247 249 chat_completion = ChatCompletion.model_construct(
248 250 content, finish_reason, completion_id, int(time.time()), usage=usage,
249 **filter_none(
251 **(filter_none(
250 252 tool_calls=[ToolCallModel.model_construct(**tool_call) for tool_call in tool_calls]
251 ) if tool_calls is not None else {},
253 ) if tool_calls else {}),
252 254 conversation=conversation,
253 255 reasoning=reasoning if reasoning else None
254 256 )
Modified g4f/client/stubs.py +1 -1
@@ -279,7 +279,7 @@ class ChatCompletionDelta(BaseModel):
279 279 def model_construct(cls, content: Optional[str]):
280 280 if isinstance(content, Reasoning):
281 281 return super().model_construct(role="assistant", content=None, reasoning=str(content))
282 elif isinstance(content, ToolCalls):
282 elif isinstance(content, ToolCalls) and content.get_list():
283 283 return super().model_construct(role="assistant", content=None, tool_calls=[
284 284 ToolCallModel.model_construct(**tool_call) for tool_call in content.get_list()
285 285 ])