(
self,
*,
parsed: Dict[str, Any],
partial: bool = False,
)
| 7364 | return json.dumps(arguments, ensure_ascii=False, separators=(",", ":")) |
| 7365 | |
| 7366 | def _parsed_chat_message( |
| 7367 | self, |
| 7368 | *, |
| 7369 | parsed: Dict[str, Any], |
| 7370 | partial: bool = False, |
| 7371 | ) -> Dict[str, Any]: |
| 7372 | message: Dict[str, Any] = { |
| 7373 | "role": parsed.get("role", "assistant"), |
| 7374 | } |
| 7375 | for key, value in parsed.items(): |
| 7376 | if key in {"role", "content", "tool_calls"}: |
| 7377 | continue |
| 7378 | if value is None or value == "": |
| 7379 | continue |
| 7380 | message[key] = value |
| 7381 | content = parsed.get("content") |
| 7382 | tool_calls = parsed.get("tool_calls") |
| 7383 | if isinstance(tool_calls, list) and tool_calls: |
| 7384 | normalized_tool_calls = [] |
| 7385 | for tool_call_index, tool_call in enumerate(tool_calls): |
| 7386 | function = tool_call["function"] |
| 7387 | if self._tool_content_type(function["name"]) == "text": |
| 7388 | arguments = self._text_tool_arguments( |
| 7389 | function["name"], |
| 7390 | function["arguments"], |
| 7391 | partial=partial, |
| 7392 | ) |
| 7393 | if arguments is None: |
| 7394 | continue |
| 7395 | else: |
| 7396 | arguments = self._serialize_tool_arguments( |
| 7397 | function["arguments"], |
| 7398 | partial=partial, |
| 7399 | ) |
| 7400 | normalized_tool_calls.append( |
| 7401 | { |
| 7402 | "id": f"call_{self._choice_index}_{function['name']}_{self._completion_id}_{tool_call_index}", |
| 7403 | "type": tool_call.get("type", "function"), |
| 7404 | "function": { |
| 7405 | "name": function["name"], |
| 7406 | "arguments": arguments, |
| 7407 | }, |
| 7408 | } |
| 7409 | ) |
| 7410 | message["content"] = content if content not in {None, ""} else None |
| 7411 | message["tool_calls"] = normalized_tool_calls |
| 7412 | if len(normalized_tool_calls) == 1: |
| 7413 | message["function_call"] = dict(normalized_tool_calls[0]["function"]) |
| 7414 | return message |
| 7415 | message["content"] = content if content is not None else "" |
| 7416 | return message |
| 7417 | |
| 7418 | def _message_deltas( |
| 7419 | self, |
no test coverage detected