Skip to content

Commit

Permalink
allow AIMessage.content=None for tool calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mattf committed Jul 24, 2024
1 parent 3af8119 commit 24598b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ def _get_payload(
messages.append(dict(role="user", content=msg))
elif isinstance(msg, dict):
if msg.get("content", None) is None:
raise ValueError(f"Message {msg} has no content")
# content=None is valid for assistant messages (tool calling)
if not msg.get("role") == "assistant":
raise ValueError(f"Message {msg} has no content.")
messages.append(msg)
else:
raise ValueError(f"Unknown message received: {msg} of type {type(msg)}")
Expand Down
36 changes: 36 additions & 0 deletions libs/ai-endpoints/tests/unit_tests/test_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import requests_mock
from langchain_core.messages import AIMessage

from langchain_nvidia_ai_endpoints import ChatNVIDIA


def test_invoke_aimessage_content_none(requests_mock: requests_mock.Mocker) -> None:
requests_mock.post(
"https://integrate.api.nvidia.com/v1/chat/completions",
json={
"id": "mock-id",
"created": 1234567890,
"object": "chat.completion",
"model": "mock-model",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "WORKED"},
}
],
},
)

empty_aimessage = AIMessage(content="EMPTY")
empty_aimessage.content = None # type: ignore

llm = ChatNVIDIA()
response = llm.invoke([empty_aimessage])
request = requests_mock.request_history[0]
assert request.method == "POST"
assert request.url == "https://integrate.api.nvidia.com/v1/chat/completions"
message = request.json()["messages"][0]
assert "content" in message and message["content"] != "EMPTY"
assert "content" in message and message["content"] is None
assert isinstance(response, AIMessage)
assert response.content == "WORKED"

0 comments on commit 24598b3

Please sign in to comment.