Spaces:
Sleeping
Sleeping
Commit ·
82e5fad
1
Parent(s): 5800c6c
fix: Xử lý an toàn tool_calls trong hàm respond
Browse files- Thêm kiểm tra tool_calls là list hay dict
- Thêm kiểm tra các thuộc tính tồn tại trước khi truy cập
- Xử lý an toàn hơn khi truy cập các thuộc tính của tool_call
- Tránh lỗi KeyError khi xử lý tool_calls từ API
app.py
CHANGED
|
@@ -121,14 +121,19 @@ def respond(
|
|
| 121 |
delta = chunk.choices[0].delta
|
| 122 |
|
| 123 |
if hasattr(delta, 'tool_calls') and delta.tool_calls:
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Xử lý function name
|
| 127 |
-
if hasattr(tool_call.function, 'name'):
|
| 128 |
current_tool_call["function"] = tool_call.function.name
|
| 129 |
|
| 130 |
# Xử lý arguments
|
| 131 |
-
if hasattr(tool_call.function, 'arguments'):
|
| 132 |
current_tool_call["arguments"] += tool_call.function.arguments
|
| 133 |
|
| 134 |
# Thực thi function khi nhận đủ arguments
|
|
|
|
| 121 |
delta = chunk.choices[0].delta
|
| 122 |
|
| 123 |
if hasattr(delta, 'tool_calls') and delta.tool_calls:
|
| 124 |
+
# Kiểm tra tool_calls là list và có phần tử
|
| 125 |
+
if isinstance(delta.tool_calls, list) and len(delta.tool_calls) > 0:
|
| 126 |
+
tool_call = delta.tool_calls[0]
|
| 127 |
+
else:
|
| 128 |
+
# Xử lý tool_calls là dict
|
| 129 |
+
tool_call = delta.tool_calls
|
| 130 |
|
| 131 |
# Xử lý function name
|
| 132 |
+
if hasattr(tool_call, 'function') and hasattr(tool_call.function, 'name'):
|
| 133 |
current_tool_call["function"] = tool_call.function.name
|
| 134 |
|
| 135 |
# Xử lý arguments
|
| 136 |
+
if hasattr(tool_call, 'function') and hasattr(tool_call.function, 'arguments'):
|
| 137 |
current_tool_call["arguments"] += tool_call.function.arguments
|
| 138 |
|
| 139 |
# Thực thi function khi nhận đủ arguments
|