Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/memos/llms/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ def generate(self, messages: MessageList, **kwargs) -> Any:
if isinstance(tool_calls, list) and len(tool_calls) > 0:
return self.tool_call_parser(tool_calls)

str_thinking = (
f"<think>{response.message.thinking}</think>"
if hasattr(response.message, "thinking")
else ""
)
# ollama>=0.5 always defines Message.thinking (Optional, default None).
# hasattr is therefore always true and would stringify None into the reply.
thinking = response.message.thinking if hasattr(response.message, "thinking") else None
str_thinking = f"<think>{thinking}</think>" if thinking else ""
str_response = response.message.content
if self.config.remove_think_prefix:
return remove_thinking_tags(str_response or "")
Expand Down
28 changes: 28 additions & 0 deletions tests/llms/test_ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from types import SimpleNamespace
from unittest.mock import MagicMock

from ollama import Message

from memos.configs.llm import LLMConfigFactory, OllamaLLMConfig
from memos.llms.factory import LLMFactory
from memos.llms.ollama import OllamaLLM
Expand Down Expand Up @@ -81,3 +83,29 @@ def test_ollama_llm_with_mocked_backend(self):
response,
"<think>Analyzing your request...</think>Hello! How are you? I'm here to help and smile!",
)

def test_generate_does_not_stringify_none_thinking(self):
"""ollama>=0.5 Message.thinking defaults to None, so hasattr is always true."""
message = Message(role="assistant", content="Hello")
self.assertTrue(hasattr(message, "thinking"))
self.assertIsNone(message.thinking)

mock_chat = MagicMock()
mock_response = MagicMock()
mock_response.model_dump_json.return_value = "{}"
mock_response.message = message
mock_chat.return_value = mock_response

config = OllamaLLMConfig(
model_name_or_path="qwen3:0.6b",
temperature=0.8,
max_tokens=1024,
top_p=0.9,
top_k=50,
)
ollama = OllamaLLM(config)
ollama.client.chat = mock_chat

response = ollama.generate([{"role": "user", "content": "Hi"}])

self.assertEqual(response, "Hello")
Loading