-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ollama): resolve model list loading issue and add Pytest for component testing #3575
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a225551
Commit to solve Model not loading issue
edwinjosechittilappilly 53d7b93
updated the component Ollama Component
edwinjosechittilappilly 0714a67
Merge branch 'main' into bug/ollama-component
edwinjosechittilappilly 5efa993
Merge branch 'main' into bug/ollama-component
edwinjosechittilappilly de4da2b
removed unwanted print statements
edwinjosechittilappilly 3204f9a
removed skipped tests
edwinjosechittilappilly e94911a
[autofix.ci] apply automated fixes
autofix-ci[bot] 9e1c265
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/backend/tests/unit/components/models/test_ChatOllama_component.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from langflow.components.models.OllamaModel import ChatOllamaComponent | ||
from langchain_community.chat_models.ollama import ChatOllama | ||
from urllib.parse import urljoin | ||
|
||
|
||
@pytest.fixture | ||
def component(): | ||
return ChatOllamaComponent() | ||
|
||
|
||
@patch("httpx.Client.get") | ||
def test_get_model_success(mock_get, component): | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = {"models": [{"name": "model1"}, {"name": "model2"}]} | ||
mock_response.raise_for_status.return_value = None | ||
mock_get.return_value = mock_response | ||
|
||
base_url = "http://localhost:11434" | ||
|
||
model_names = component.get_model(base_url) | ||
|
||
expected_url = urljoin(base_url, "/api/tags") | ||
|
||
mock_get.assert_called_once_with(expected_url) | ||
|
||
assert model_names == ["model1", "model2"] | ||
|
||
|
||
@patch("httpx.Client.get") | ||
def test_get_model_failure(mock_get, component): | ||
# Mock the response for the HTTP GET request to raise an exception | ||
mock_get.side_effect = Exception("HTTP request failed") | ||
|
||
url = "http://localhost:11434/api/tags" | ||
|
||
# Assert that the ValueError is raised when an exception occurs | ||
with pytest.raises(ValueError, match="Could not retrieve models"): | ||
component.get_model(url) | ||
|
||
|
||
def test_update_build_config_mirostat_disabled(component): | ||
edwinjosechittilappilly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
build_config = { | ||
"mirostat_eta": {"advanced": False, "value": 0.1}, | ||
"mirostat_tau": {"advanced": False, "value": 5}, | ||
} | ||
field_value = "Disabled" | ||
field_name = "mirostat" | ||
|
||
updated_config = component.update_build_config(build_config, field_value, field_name) | ||
|
||
assert updated_config["mirostat_eta"]["advanced"] is True | ||
assert updated_config["mirostat_tau"]["advanced"] is True | ||
assert updated_config["mirostat_eta"]["value"] is None | ||
assert updated_config["mirostat_tau"]["value"] is None | ||
|
||
|
||
def test_update_build_config_mirostat_enabled(component): | ||
build_config = { | ||
"mirostat_eta": {"advanced": False, "value": None}, | ||
"mirostat_tau": {"advanced": False, "value": None}, | ||
} | ||
field_value = "Mirostat 2.0" | ||
field_name = "mirostat" | ||
|
||
updated_config = component.update_build_config(build_config, field_value, field_name) | ||
|
||
assert updated_config["mirostat_eta"]["advanced"] is False | ||
assert updated_config["mirostat_tau"]["advanced"] is False | ||
assert updated_config["mirostat_eta"]["value"] == 0.2 | ||
assert updated_config["mirostat_tau"]["value"] == 10 | ||
|
||
|
||
@patch("httpx.Client.get") | ||
def test_update_build_config_model_name(mock_get, component): | ||
# Mock the response for the HTTP GET request | ||
mock_response = MagicMock() | ||
mock_response.json.return_value = {"models": [{"name": "model1"}, {"name": "model2"}]} | ||
mock_response.raise_for_status.return_value = None | ||
mock_get.return_value = mock_response | ||
|
||
build_config = { | ||
"base_url": {"load_from_db": False, "value": None}, | ||
"model_name": {"options": []}, | ||
} | ||
field_value = None | ||
field_name = "model_name" | ||
|
||
updated_config = component.update_build_config(build_config, field_value, field_name) | ||
|
||
assert updated_config["model_name"]["options"] == ["model1", "model2"] | ||
|
||
|
||
def test_update_build_config_keep_alive(component): | ||
build_config = {"keep_alive": {"value": None, "advanced": False}} | ||
field_value = "Keep" | ||
field_name = "keep_alive_flag" | ||
|
||
updated_config = component.update_build_config(build_config, field_value, field_name) | ||
assert updated_config["keep_alive"]["value"] == "-1" | ||
assert updated_config["keep_alive"]["advanced"] is True | ||
|
||
field_value = "Immediately" | ||
updated_config = component.update_build_config(build_config, field_value, field_name) | ||
assert updated_config["keep_alive"]["value"] == "0" | ||
assert updated_config["keep_alive"]["advanced"] is True | ||
|
||
|
||
@patch( | ||
"langchain_community.chat_models.ChatOllama", | ||
return_value=ChatOllama(base_url="http://localhost:11434", model="llama3.1"), | ||
) | ||
def test_build_model(mock_chat_ollama, component): | ||
edwinjosechittilappilly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
component.base_url = "http://localhost:11434" | ||
component.model_name = "llama3.1" | ||
component.mirostat = "Mirostat 2.0" | ||
component.mirostat_eta = 0.2 # Ensure this is set as a float | ||
component.mirostat_tau = 10.0 # Ensure this is set as a float | ||
component.temperature = 0.2 | ||
component.verbose = True | ||
model = component.build_model() | ||
assert isinstance(model, ChatOllama) | ||
assert model.base_url == "http://localhost:11434" | ||
assert model.model == "llama3.1" | ||
|
||
|
||
@patch("langchain_community.chat_models.ChatOllama") | ||
def test_build_model_failure(mock_chat_ollama, component): | ||
# Mock the ChatOllama to raise an exception when initialized | ||
pass |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could just call this base_url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it such that the endpoint url of the models are formed inside the function.
Earlier the input to the function was the url to the tags/models, which is ok, but in this way it would be more clear that the model is loaded from the base url and the endpoint url is framed without error inside the get_model function. Should I revert back to the previous method and have the model url endpoint before calling the function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, that's fine