fix(autogpt/llm): Omit `AssistantChatMessage.tool_calls` if no tool calls are present

OpenAI likes neither `tool_calls=[]` nor `tool_calls=None`. If no `tool_calls` are in the message, the key must be omitted.

This partially reverts commit 67bafa6302.

---

Co-authored-by: kcze <kpczerwinski@gmail.com>
pull/6879/head
Reinier van der Leer 2024-02-20 13:04:55 +01:00
parent c8a40727d1
commit 7689a51f53
No known key found for this signature in database
GPG Key ID: CDC1180FDAE06193
2 changed files with 6 additions and 3 deletions

View File

@ -423,7 +423,7 @@ class OpenAIProvider(
tool_calls=(
[AssistantToolCall(**tc.dict()) for tc in _assistant_msg.tool_calls]
if _assistant_msg.tool_calls
else list()
else None
),
)
response = ChatModelResponse(
@ -570,7 +570,10 @@ class OpenAIProvider(
messages: list[ChatMessage], *_, **kwargs
) -> ChatCompletion:
raw_messages = [
message.dict(include={"role", "content", "tool_calls", "name"})
message.dict(
include={"role", "content", "tool_calls", "name"},
exclude_none=True,
)
for message in messages
]
return await self._client.chat.completions.create(

View File

@ -91,7 +91,7 @@ class AssistantToolCallDict(TypedDict):
class AssistantChatMessage(ChatMessage):
role: Literal["assistant"] = "assistant"
content: Optional[str]
tool_calls: list[AssistantToolCall] = Field(default_factory=list)
tool_calls: Optional[list[AssistantToolCall]] = None
class AssistantChatMessageDict(TypedDict, total=False):