Home Assistant Unofficial Reference 2024.12.1
models.py
Go to the documentation of this file.
1 """Models for Ollama integration."""
2 
3 from dataclasses import dataclass
4 from enum import StrEnum
5 
6 import ollama
7 
8 
9 class MessageRole(StrEnum):
10  """Role of a chat message."""
11 
12  SYSTEM = "system" # prompt
13  USER = "user"
14  ASSISTANT = "assistant"
15  TOOL = "tool"
16 
17 
18 @dataclass
20  """Chat message history."""
21 
22  timestamp: float
23  """Timestamp of last use in seconds."""
24 
25  messages: list[ollama.Message]
26  """List of message history, including system prompt and assistant responses."""
27 
28  @property
29  def num_user_messages(self) -> int:
30  """Return a count of user messages."""
31  return sum(m["role"] == MessageRole.USER.value for m in self.messages)