1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Ollama thinking content (#150393)

This commit is contained in:
Denis Shulyaka
2025-10-06 00:33:45 +03:00
committed by GitHub
parent 9a9fd44c62
commit f1b8e8a963
2 changed files with 69 additions and 1 deletions

View File

@@ -95,6 +95,7 @@ def _convert_content(
return ollama.Message(
role=MessageRole.ASSISTANT.value,
content=chat_content.content,
thinking=chat_content.thinking_content,
tool_calls=[
ollama.Message.ToolCall(
function=ollama.Message.ToolCall.Function(
@@ -103,7 +104,8 @@ def _convert_content(
)
)
for tool_call in chat_content.tool_calls or ()
],
]
or None,
)
if isinstance(chat_content, conversation.UserContent):
images: list[ollama.Image] = []
@@ -162,6 +164,8 @@ async def _transform_stream(
]
if (content := response_message.get("content")) is not None:
chunk["content"] = content
if (thinking := response_message.get("thinking")) is not None:
chunk["thinking_content"] = thinking
if response_message.get("done"):
new_msg = True
yield chunk

View File

@@ -145,6 +145,70 @@ async def test_chat_stream(
assert result.response.speech["plain"]["speech"] == "test response"
async def test_thinking_content(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_init_component,
) -> None:
"""Test that thinking content is retained in multi-turn conversation."""
entry = MockConfigEntry()
entry.add_to_hass(hass)
subentry = next(iter(mock_config_entry.subentries.values()))
hass.config_entries.async_update_subentry(
mock_config_entry,
subentry,
data={
**subentry.data,
ollama.CONF_THINK: True,
},
)
conversation_id = "conversation_id_1234"
with patch(
"ollama.AsyncClient.chat",
return_value=stream_generator(
{
"message": {
"role": "assistant",
"content": "test response",
"thinking": "test thinking",
},
"done": True,
"done_reason": "stop",
},
),
) as mock_chat:
await conversation.async_converse(
hass,
"test message",
conversation_id,
Context(),
agent_id=mock_config_entry.entry_id,
)
await conversation.async_converse(
hass,
"test message 2",
conversation_id,
Context(),
agent_id=mock_config_entry.entry_id,
)
assert mock_chat.call_count == 2
assert mock_chat.call_args.kwargs["messages"][1:] == [
Message(role="user", content="test message"),
Message(
role="assistant",
content="test response",
thinking="test thinking",
),
Message(role="user", content="test message 2"),
]
async def test_template_variables(
hass: HomeAssistant, mock_config_entry: MockConfigEntry
) -> None: