Allen Porter 57ab567d08
Update ollama to use the ChatLog/ChatSession APIs (#138167)
* Update ollama to use the ChatLog/ChatSession APIs

* Add documentation about history trimming.

* Revert changes to chat_log.py

* Explicitly check for SystemContent when converting system messages

* Remove half of a comment
2025-02-09 16:52:01 -05:00

29 lines
641 B
Python

"""Models for Ollama integration."""
from dataclasses import dataclass
from enum import StrEnum
import ollama
class MessageRole(StrEnum):
"""Role of a chat message."""
SYSTEM = "system" # prompt
USER = "user"
ASSISTANT = "assistant"
TOOL = "tool"
@dataclass
class MessageHistory:
"""Chat message history."""
messages: list[ollama.Message]
"""List of message history, including system prompt and assistant responses."""
@property
def num_user_messages(self) -> int:
"""Return a count of user messages."""
return sum(m["role"] == MessageRole.USER.value for m in self.messages)