-
Notifications
You must be signed in to change notification settings - Fork 116
fix: handle direct model answers in ReACT loop #763
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
Closed
+299
−2
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3772479
fix: handle direct model answers in ReACT loop
markstur c2e004e
fix: before failing react loop ask LLM if it has the answer
markstur c077f16
feat: add callback to react() to allow checking for final answer
markstur ecb6e0f
test: move test
markstur 4abfb96
doc: improve docstrings
markstur b4072ff
test: update deprecated llm test markers
markstur d47e1ec
test: add test coverage for answer_check
markstur 07791bd
fix: remove unused TrueOrFalse
markstur c0b01bb
test: add qualitative markers to tests
markstur 0988b5b
docs: fix example to use achat
markstur 7d3be72
feat: answer_check callable protocol
markstur 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
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
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,107 @@ | ||
| """Test ReACT framework handling of direct answers without tool calls.""" | ||
|
|
||
| import pydantic | ||
| import pytest | ||
|
|
||
| from mellea.backends.tools import tool | ||
| from mellea.stdlib import functional as mfuncs | ||
| from mellea.stdlib.context import ChatContext | ||
| from mellea.stdlib.frameworks.react import react | ||
| from mellea.stdlib.session import start_session | ||
|
|
||
|
|
||
| class TrueOrFalse(pydantic.BaseModel): | ||
| """Response indicating whether the ReACT agent has completed its task.""" | ||
|
|
||
| answer: bool = pydantic.Field( | ||
| description="True if you have enough information to answer the user's question, False if you need more tool calls" | ||
| ) | ||
|
|
||
|
|
||
| async def last_loop_completion_check( | ||
| goal, context, backend, step, model_options, turn_num, loop_budget | ||
| ): | ||
| """Completion check that asks the model if it has the answer on the last iteration. | ||
|
|
||
| Note: step.value is guaranteed to exist when this is called. | ||
| """ | ||
| # Only check on last iteration (and not for unlimited budget) | ||
| if loop_budget == -1 or turn_num < loop_budget: | ||
| return False | ||
|
|
||
| content = mfuncs.chat( | ||
| content=f"Do you know the answer to the user's original query ({goal})? If so, respond with True. If you need to take more actions, then respond False.", | ||
| context=context, | ||
| backend=backend, | ||
| format=TrueOrFalse, | ||
| )[0].content | ||
| have_answer = TrueOrFalse.model_validate_json(content).answer | ||
| return have_answer | ||
|
|
||
|
|
||
| @pytest.mark.ollama | ||
| @pytest.mark.e2e | ||
| @pytest.mark.qualitative | ||
| async def test_react_direct_answer_without_tools(): | ||
|
markstur marked this conversation as resolved.
|
||
| """Test that ReACT handles direct answers when model doesn't call tools. | ||
|
|
||
| This tests the case where the model provides a direct answer in step.value | ||
| without making any tool calls. The fix ensures the loop terminates properly | ||
| instead of continuing until loop_budget is exhausted. | ||
| """ | ||
| m = start_session() | ||
|
|
||
| # Ask a simple question that doesn't require tools | ||
| # The model should provide a direct answer without calling any tools | ||
| out, _ = await react( | ||
| goal="What is 2 + 2?", | ||
| context=ChatContext(), | ||
| backend=m.backend, | ||
| tools=[], # No tools provided | ||
| loop_budget=3, # Should complete in 1 iteration, not exhaust budget | ||
| answer_check=last_loop_completion_check, | ||
| ) | ||
|
|
||
| # Verify we got an answer | ||
| assert out.value is not None | ||
| assert len(out.value) > 0 | ||
|
|
||
| # The answer should contain "4" or "four" | ||
| answer_lower = out.value.lower() | ||
| assert "4" in answer_lower or "four" in answer_lower | ||
|
|
||
|
|
||
| @pytest.mark.ollama | ||
| @pytest.mark.e2e | ||
| @pytest.mark.qualitative | ||
| async def test_react_direct_answer_with_unused_tools(): | ||
| """Test that ReACT handles direct answers even when tools are available. | ||
|
|
||
| This tests the case where tools are provided but the model chooses to | ||
| answer directly without using them. | ||
| """ | ||
| m = start_session() | ||
|
|
||
| # Create a dummy tool that won't be needed | ||
| @tool | ||
| def search_web(query: str) -> str: | ||
| """Search the web for information.""" | ||
| return "Search results" | ||
|
|
||
| # Ask a question that doesn't need the tool | ||
| out, _ = await react( | ||
| goal="What is the capital of France?", | ||
| context=ChatContext(), | ||
| backend=m.backend, | ||
| tools=[search_web], | ||
| loop_budget=3, | ||
| answer_check=last_loop_completion_check, | ||
| ) | ||
|
|
||
| # Verify we got an answer | ||
| assert out.value is not None | ||
| assert len(out.value) > 0 | ||
|
|
||
| # The answer should mention Paris | ||
| answer_lower = out.value.lower() | ||
| assert "paris" in answer_lower | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.