-
Notifications
You must be signed in to change notification settings - Fork 666
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
Add callback support in settings and tools #590
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0bc8b7b
Add callback support in settings and tools
nadolskit 6a2c724
re-add flaky
nadolskit f1873d0
Merge branch 'main' into settings-callbacks
nadolskit 20484a0
fix type_checking import
nadolskit 7e353fe
Merge branch 'settings-callbacks' of https://github.com/Future-House/…
nadolskit d359950
Merge branch 'main' into settings-callbacks
nadolskit 4f3dddf
remove syncronous callbacks
nadolskit 4ec586a
Address comments
nadolskit 0fe7fc1
address comments
nadolskit 50a3503
Update paperqa/agents/tools.py
nadolskit 68320d8
Merge branch 'main' into settings-callbacks
nadolskit eaab9e7
parallelize the callback calling
nadolskit c281c28
move callbacks into agent settings
nadolskit 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
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
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import time | ||
from pathlib import Path | ||
from typing import Any, cast | ||
from unittest.mock import patch | ||
from unittest.mock import AsyncMock, patch | ||
from uuid import uuid4 | ||
|
||
import ldp.agent | ||
|
@@ -421,10 +421,11 @@ async def test_gather_evidence_rejects_empty_docs( | |
), "Agent should have hit its max timesteps" | ||
|
||
|
||
@pytest.mark.parametrize("callback_type", [None, "async"]) | ||
@pytest.mark.flaky(reruns=3, only_rerun=["AssertionError", "EmptyDocsError"]) | ||
@pytest.mark.asyncio | ||
async def test_agent_sharing_state( | ||
agent_test_settings: Settings, subtests: SubTests | ||
agent_test_settings: Settings, subtests: SubTests, callback_type: str | ||
) -> None: | ||
agent_test_settings.agent.search_count = 3 # Keep low for speed | ||
agent_test_settings.answer.evidence_k = 2 | ||
|
@@ -433,6 +434,22 @@ async def test_agent_sharing_state( | |
summary_llm_model = agent_test_settings.get_summary_llm() | ||
embedding_model = agent_test_settings.get_embedding_model() | ||
|
||
callbacks = {} | ||
if callback_type == "async": | ||
gen_answer_initialized_callback = AsyncMock() | ||
gen_answer_completed_callback = AsyncMock() | ||
gather_evidence_initialized_callback = AsyncMock() | ||
gather_evidence_completed_callback = AsyncMock() | ||
|
||
callbacks = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure it really matters, but it's more future proof to use |
||
"gen_answer_initialized": [gen_answer_initialized_callback], | ||
"gen_answer_completed": [gen_answer_completed_callback], | ||
"gather_evidence_initialized": [gather_evidence_initialized_callback], | ||
"gather_evidence_completed": [gather_evidence_completed_callback], | ||
} | ||
|
||
agent_test_settings.agent.callbacks = callbacks # type: ignore[assignment] | ||
|
||
answer = Answer(question="What is is a self-explanatory model?") | ||
query = QueryRequest(query=answer.question, settings=agent_test_settings) | ||
env_state = EnvironmentState(docs=Docs(), answer=answer) | ||
|
@@ -455,8 +472,7 @@ async def test_agent_sharing_state( | |
assert env_state.docs.docs, "Search did not add any papers" | ||
mock_save_index.assert_not_awaited(), "Search shouldn't try to update the index" | ||
assert all( | ||
(isinstance(d, Doc) or issubclass(d, Doc)) # type: ignore[unreachable] | ||
for d in env_state.docs.docs.values() | ||
isinstance(d, Doc) for d in env_state.docs.docs.values() | ||
), "Document type or DOI propagation failure" | ||
|
||
with subtests.test(msg=GatherEvidence.__name__): | ||
|
@@ -468,6 +484,11 @@ async def test_agent_sharing_state( | |
embedding_model=embedding_model, | ||
) | ||
await gather_evidence_tool.gather_evidence(answer.question, state=env_state) | ||
|
||
if callback_type == "async": | ||
gather_evidence_initialized_callback.assert_awaited_once_with(env_state) | ||
gather_evidence_completed_callback.assert_awaited_once_with(env_state) | ||
|
||
assert answer.contexts, "Evidence did not return any results" | ||
|
||
with subtests.test(msg=f"{GenerateAnswer.__name__} working"): | ||
|
@@ -478,6 +499,11 @@ async def test_agent_sharing_state( | |
embedding_model=embedding_model, | ||
) | ||
result = await generate_answer_tool.gen_answer(answer.question, state=env_state) | ||
|
||
if callback_type == "async": | ||
gen_answer_initialized_callback.assert_awaited_once_with(env_state) | ||
gen_answer_completed_callback.assert_awaited_once_with(env_state) | ||
|
||
assert re.search( | ||
pattern=EnvironmentState.STATUS_SEARCH_REGEX_PATTERN, string=result | ||
) | ||
|
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 should be
callback_type: str | None