-
Notifications
You must be signed in to change notification settings - Fork 120
Adapt to the Ollama reasoning platform and use the "reasoning" JSON k… #268
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
Open
zhujunling-nj
wants to merge
5
commits into
langgenius:main
Choose a base branch
from
zhujunling-nj:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
148 changes: 148 additions & 0 deletions
148
python/tests/interfaces/model/test_llm_ollama_adapter.py
This file contains hidden or 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,148 @@ | ||
| import unittest | ||
|
|
||
| from dify_plugin.entities.model import AIModelEntity, ModelPropertyKey, ModelType | ||
| from dify_plugin.entities.model.llm import LLMMode, LLMResult | ||
| from dify_plugin.interfaces.model.large_language_model import LargeLanguageModel | ||
|
|
||
|
|
||
| class MockLLM(LargeLanguageModel): | ||
| """ | ||
| Concrete Mock class for testing non-abstract methods of LargeLanguageModel. | ||
| """ | ||
|
|
||
| def _invoke( | ||
| self, | ||
| model: str, | ||
| credentials: dict, | ||
| prompt_messages: list, | ||
| model_parameters: dict, | ||
| tools: list, | ||
| stop: list, | ||
| stream: bool, | ||
| user: str, | ||
| ) -> LLMResult: | ||
| pass | ||
|
|
||
| def get_num_tokens(self, model: str, credentials: dict, prompt_messages: list, tools: list) -> int: | ||
| return 0 | ||
|
|
||
| def validate_credentials(self, model: str, credentials: dict) -> None: | ||
| pass | ||
|
|
||
| @property | ||
| def _invoke_error_mapping(self) -> dict: | ||
| return {} | ||
|
|
||
|
|
||
| class TestOllamaAdapter(unittest.TestCase): | ||
| def setUp(self): | ||
| # Create a dummy model schema to satisfy AIModel.__init__ | ||
| dummy_schema = AIModelEntity( | ||
| model="mock_model", | ||
| label={"en_US": "Mock Model"}, | ||
| model_type=ModelType.LLM, | ||
| features=[], | ||
| model_properties={ModelPropertyKey.MODE: LLMMode.CHAT.value, ModelPropertyKey.CONTEXT_SIZE: 4096}, | ||
| parameter_rules=[], | ||
| pricing=None, | ||
| deprecated=False, | ||
| ) | ||
| self.llm = MockLLM(model_schemas=[dummy_schema]) | ||
|
|
||
|
|
||
| def test_with_reasoning_content(self): | ||
| """ | ||
| The test includes reasoning_content, | ||
| and the output should contain the <think> tag. | ||
| """ | ||
|
|
||
| # Simulate simulated streaming data: | ||
| # 1. Has reasoning_content | ||
|
|
||
| chunks = [ | ||
| # Chunk 1: Thinking started | ||
| {"reasoning_content": "Thinking started.", "content": ""}, | ||
| # Chunk 2: Still thinking | ||
| {"reasoning_content": " Still thinking.", "content": ""}, | ||
| {"content": "Content started."}, | ||
| ] | ||
|
|
||
| # Assume we are testing the logic function itself: | ||
| is_reasoning = False | ||
| full_output = "" | ||
|
|
||
| for chunk in chunks: | ||
| # Directly call the implementation in SDK to verify real code logic | ||
| output, is_reasoning = self.llm._wrap_thinking_by_reasoning_content(chunk, is_reasoning) | ||
| full_output += output | ||
|
|
||
| # Verify results | ||
| print(f"DEBUG Output: {full_output!r}") | ||
|
|
||
| assert "<think>" in full_output | ||
| assert "Thinking started. Still thinking." in full_output | ||
|
|
||
|
|
||
| def test_with_reasoning(self): | ||
| """ | ||
| The test includes reasoning, | ||
| and the output should contain the <think> tag. | ||
| """ | ||
|
|
||
| # Simulate simulated streaming data: | ||
| # 1. Has reasoning | ||
|
|
||
| chunks = [ | ||
| # Chunk 1: Thinking started | ||
| {"reasoning": "Thinking started.", "content": ""}, | ||
| # Chunk 2: Still thinking | ||
| {"reasoning": " Still thinking.", "content": ""}, | ||
| {"content": "Content started."}, | ||
| ] | ||
|
|
||
| # Assume we are testing the logic function itself: | ||
| is_reasoning = False | ||
| full_output = "" | ||
|
|
||
| for chunk in chunks: | ||
| # Directly call the implementation in SDK to verify real code logic | ||
| output, is_reasoning = self.llm._wrap_thinking_by_reasoning_content(chunk, is_reasoning) | ||
| full_output += output | ||
|
|
||
| # Verify results | ||
| print(f"DEBUG Output: {full_output!r}") | ||
|
|
||
| assert "<think>" in full_output | ||
| assert "Thinking started. Still thinking." in full_output | ||
|
|
||
|
|
||
| def test_without_reasoning(self): | ||
| """ | ||
| The test does not include reasoning_content or reasoning. | ||
| The output should not contain the <think> tag. | ||
| """ | ||
|
|
||
| # Simulate simulated streaming data: | ||
| # 1. Has no reasoning_content and reasoning | ||
|
|
||
| chunks = [ | ||
| # Chunk 1: No Thinking | ||
| {"content": "Content started."}, | ||
| # Chunk 2: Still No thinking | ||
| {"content": " Still content."}, | ||
| ] | ||
|
|
||
| # Assume we are testing the logic function itself: | ||
| is_reasoning = False | ||
| full_output = "" | ||
|
|
||
| for chunk in chunks: | ||
| # Directly call the implementation in SDK to verify real code logic | ||
| output, is_reasoning = self.llm._wrap_thinking_by_reasoning_content(chunk, is_reasoning) | ||
| full_output += output | ||
|
|
||
| # Verify results | ||
| print(f"DEBUG Output: {full_output!r}") | ||
|
|
||
| assert "<think>" not in full_output | ||
| assert "Content started. Still content." in full_output |
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.
This change, combined with the existing logic, could lead to a
TypeErrorifdelta.get('reasoning_content')ordelta.get('reasoning')returns a truthy non-string value (e.g., a dictionary or a list). The string concatenation on line 542 would fail. To make the code more robust and prevent runtime errors, it's best to explicitly convert the result to a string. Usingstr()and providing an empty string as a final fallback will handleNoneand other falsy values correctly while safely converting any other types.