-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix: make Ask output token budget configurable #1247
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
bferanmi806-sketch
wants to merge
3
commits into
lfnovo:main
Choose a base branch
from
bferanmi806-sketch:fix/1221-ask-output-token-budget
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.
+204
−4
Open
Changes from 1 commit
Commits
Show all changes
3 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
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
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,132 @@ | ||
| from types import SimpleNamespace | ||
| from typing import cast | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| import open_notebook.graphs.ask as ask | ||
|
|
||
|
|
||
| def test_ask_max_tokens_defaults_when_env_is_unset(monkeypatch): | ||
| monkeypatch.delenv(ask.ASK_MAX_TOKENS_ENV_VAR, raising=False) | ||
|
|
||
| assert ask.get_ask_max_tokens() == 8192 | ||
|
|
||
|
|
||
| def test_ask_max_tokens_reads_positive_override(monkeypatch): | ||
| monkeypatch.setenv(ask.ASK_MAX_TOKENS_ENV_VAR, "12000") | ||
|
|
||
| assert ask.get_ask_max_tokens() == 12000 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", ["not-a-number", "0", "-5"]) | ||
| def test_ask_max_tokens_invalid_values_fall_back_to_default(monkeypatch, value): | ||
| monkeypatch.setenv(ask.ASK_MAX_TOKENS_ENV_VAR, value) | ||
|
|
||
| assert ask.get_ask_max_tokens() == 8192 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_strategy_uses_fixed_token_budget(monkeypatch): | ||
| monkeypatch.setenv(ask.ASK_MAX_TOKENS_ENV_VAR, "12000") | ||
| model = SimpleNamespace( | ||
| ainvoke=AsyncMock( | ||
| return_value=SimpleNamespace( | ||
| content='{"reasoning":"Need one search","searches":[]}' | ||
| ) | ||
| ) | ||
| ) | ||
| provision = AsyncMock(return_value=model) | ||
| monkeypatch.setattr(ask, "provision_langchain_model", provision) | ||
| monkeypatch.setattr( | ||
| ask.Prompter, "render", lambda self, **kwargs: "strategy prompt" | ||
| ) | ||
|
|
||
| result = await ask.call_model_with_messages( | ||
| cast( | ||
| ask.ThreadState, | ||
| {"question": "What is this?", "answers": [], "final_answer": ""}, | ||
| ), | ||
| {"configurable": {"strategy_model": "strategy-model"}}, | ||
| ) | ||
|
|
||
| assert result == { | ||
| "strategy": ask.Strategy(reasoning="Need one search", searches=[]) | ||
| } | ||
| provision.assert_awaited_once_with( | ||
| "strategy prompt", | ||
| "strategy-model", | ||
| "tools", | ||
| max_tokens=2000, | ||
| structured={"type": "json"}, | ||
| ) | ||
| model.ainvoke.assert_awaited_once_with("strategy prompt") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_provide_answer_uses_configured_token_budget(monkeypatch): | ||
| monkeypatch.setenv(ask.ASK_MAX_TOKENS_ENV_VAR, "12000") | ||
| vector_search = AsyncMock(return_value=[{"id": "source:1"}]) | ||
| model = SimpleNamespace( | ||
| ainvoke=AsyncMock( | ||
| return_value=SimpleNamespace(content="<think>internal</think>Answer") | ||
| ) | ||
| ) | ||
| provision = AsyncMock(return_value=model) | ||
| monkeypatch.setattr(ask, "vector_search", vector_search) | ||
| monkeypatch.setattr(ask, "provision_langchain_model", provision) | ||
| monkeypatch.setattr(ask.Prompter, "render", lambda self, **kwargs: "answer prompt") | ||
|
|
||
| result = await ask.provide_answer( | ||
| cast( | ||
| ask.SubGraphState, | ||
| { | ||
| "question": "What is this?", | ||
| "term": "this", | ||
| "instructions": "Explain it", | ||
| }, | ||
| ), | ||
| {"configurable": {"answer_model": "answer-model"}}, | ||
| ) | ||
|
|
||
| assert result == {"answers": ["Answer"]} | ||
| vector_search.assert_awaited_once_with("this", 10, True, True) | ||
| provision.assert_awaited_once_with( | ||
| "answer prompt", | ||
| "answer-model", | ||
| "tools", | ||
| max_tokens=12000, | ||
| ) | ||
| model.ainvoke.assert_awaited_once_with("answer prompt") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_write_final_answer_uses_configured_token_budget(monkeypatch): | ||
| monkeypatch.setenv(ask.ASK_MAX_TOKENS_ENV_VAR, "12000") | ||
| model = SimpleNamespace( | ||
| ainvoke=AsyncMock( | ||
| return_value=SimpleNamespace(content="<think>internal</think>Final answer") | ||
| ) | ||
| ) | ||
| provision = AsyncMock(return_value=model) | ||
| monkeypatch.setattr(ask, "provision_langchain_model", provision) | ||
| monkeypatch.setattr(ask.Prompter, "render", lambda self, **kwargs: "final prompt") | ||
|
|
||
| result = await ask.write_final_answer( | ||
| { | ||
| "question": "What is this?", | ||
| "strategy": ask.Strategy(reasoning="", searches=[]), | ||
| "answers": ["Answer"], | ||
| "final_answer": "", | ||
| }, | ||
| {"configurable": {"final_answer_model": "final-model"}}, | ||
| ) | ||
|
|
||
| assert result == {"final_answer": "Final answer"} | ||
| provision.assert_awaited_once_with( | ||
| "final prompt", | ||
| "final-model", | ||
| "tools", | ||
| max_tokens=12000, | ||
| ) | ||
| model.ainvoke.assert_awaited_once_with("final prompt") |
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.