mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
ollama: add validate_model_on_init, catch more errors (#31784)
* Ensure access to local model during `ChatOllama` instantiation (#27720). This adds a new param `validate_model_on_init` (default: `true`) * Catch a few more errors from the Ollama client to assist users
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,8 +1,13 @@
|
||||
"""Test chat model integration using standard integration tests."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from httpx import ConnectError
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
from langchain_tests.integration_tests import ChatModelIntegrationTests
|
||||
from ollama import ResponseError
|
||||
from pydantic import ValidationError
|
||||
|
||||
from langchain_ollama.chat_models import ChatOllama
|
||||
|
||||
@@ -47,3 +52,29 @@ class TestChatOllama(ChatModelIntegrationTests):
|
||||
)
|
||||
async def test_tool_calling_async(self, model: BaseChatModel) -> None:
|
||||
await super().test_tool_calling_async(model)
|
||||
|
||||
@patch("langchain_ollama.chat_models.Client.list")
|
||||
def test_init_model_not_found(self, mock_list: MagicMock) -> None:
|
||||
"""Test that a ValueError is raised when the model is not found."""
|
||||
mock_list.side_effect = ValueError("Test model not found")
|
||||
with pytest.raises(ValueError) as excinfo:
|
||||
ChatOllama(model="non-existent-model", validate_model_on_init=True)
|
||||
assert "Test model not found" in str(excinfo.value)
|
||||
|
||||
@patch("langchain_ollama.chat_models.Client.list")
|
||||
def test_init_connection_error(self, mock_list: MagicMock) -> None:
|
||||
"""Test that a ValidationError is raised on connect failure during init."""
|
||||
mock_list.side_effect = ConnectError("Test connection error")
|
||||
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
ChatOllama(model="any-model", validate_model_on_init=True)
|
||||
assert "not found in Ollama" in str(excinfo.value)
|
||||
|
||||
@patch("langchain_ollama.chat_models.Client.list")
|
||||
def test_init_response_error(self, mock_list: MagicMock) -> None:
|
||||
"""Test that a ResponseError is raised."""
|
||||
mock_list.side_effect = ResponseError("Test response error")
|
||||
|
||||
with pytest.raises(ValidationError) as excinfo:
|
||||
ChatOllama(model="any-model", validate_model_on_init=True)
|
||||
assert "Received an error from the Ollama API" in str(excinfo.value)
|
||||
|
||||
Reference in New Issue
Block a user