-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agents-api): Adaptive context (via recursive summarization) (#306)
* research: Recursive summarization experiments Signed-off-by: Diwank Singh Tomer <[email protected]> * fix: Minor fix to rec-sum notebook Signed-off-by: Diwank Singh Tomer <[email protected]> * wip: Rec-sum notebook Signed-off-by: Diwank Singh Tomer <[email protected]> * entity and trim step prompt + notebook. summarise setup. * summarization step added. * wip Signed-off-by: Diwank Singh Tomer <[email protected]> * chore: Move rec_sum subpackage * feat: Summarize messages recursively * feat: Use custom model for summarization * chore: Remove commented out code * fix: Serialize pandas series objects * fix: Choose correct way to generate based on model name * fix: Add old entities message as a child for the new one instead of deleting it * chore: Add tenacity dependency * fix: Strip closing ct:trimmed tag * fix: Strip closing ct:summarized-messages tag * fix: Add a list of entries instead of only one * fix: Convert strings to UUID * fix: Stringify message content * fix: Convert new entry content to a list of JSON * fix: Strip ct:entities tag * fix: Do not add new entry explicitly * fix: Update assertion * feat: Truncate context window based on session settings (#381) * feat: Calculate tokens for image content * feat: Update SDKs to support adaptive context options * fix: Truncate entries * fix: Make truncation a background task * fix: Add truncation workflow to registry * fix: Fix deleting query * fix: Remove truncated entries * fix: Convert role to string only if needed * fix: Replace delete by rm * fix: Fix entries deleting logic * fix: Set name to None if absent * fix: Make deleting query accept UUID as a string * fix: Convert UUIDs to strings * fix: Fix query parameter name * fix: Convert input to array of arrays * fix: Make entries indices zro-based * fix: Customize summarization model via environment variable * chore: Re-arrange operations on dialog entries * deps: poetry lock on agents-api and python sdk Signed-off-by: Diwank Tomer <[email protected]> --------- Signed-off-by: Diwank Singh Tomer <[email protected]> Signed-off-by: Diwank Tomer <[email protected]> Co-authored-by: Siddharth Balyan <[email protected]> Co-authored-by: Dmitry Paramonov <[email protected]> Co-authored-by: Diwank Tomer <[email protected]>
- Loading branch information
1 parent
c6e6079
commit 5b06179
Showing
57 changed files
with
6,706 additions
and
228 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
h = logging.StreamHandler() | ||
fmt = logging.Formatter("[%(asctime)s/%(levelname)s] - %(message)s") | ||
h.setFormatter(fmt) | ||
logger.addHandler(h) |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from uuid import UUID | ||
from temporalio import activity | ||
from agents_api.models.entry.entries_summarization import get_toplevel_entries_query | ||
from agents_api.models.entry.delete_entries import delete_entries | ||
from agents_api.autogen.openapi_model import Role | ||
from agents_api.common.protocol.entries import Entry | ||
|
||
|
||
def get_extra_entries(messages: list[Entry], token_count_threshold: int) -> list[UUID]: | ||
if not len(messages): | ||
return messages | ||
|
||
result: list[UUID] = [] | ||
token_cnt, offset = 0, 0 | ||
if messages[0].role == Role.system: | ||
token_cnt, offset = messages[0].token_count, 1 | ||
|
||
for m in reversed(messages[offset:]): | ||
token_cnt += m.token_count | ||
if token_cnt < token_count_threshold: | ||
continue | ||
else: | ||
result.append(m.id) | ||
|
||
return result | ||
|
||
|
||
@activity.defn | ||
async def truncation(session_id: str, token_count_threshold: int) -> None: | ||
session_id = UUID(session_id) | ||
|
||
delete_entries( | ||
get_extra_entries( | ||
[ | ||
Entry( | ||
entry_id=row["entry_id"], | ||
session_id=session_id, | ||
source=row["source"], | ||
role=Role(row["role"]), | ||
name=row["name"], | ||
content=row["content"], | ||
created_at=row["created_at"], | ||
timestamp=row["timestamp"], | ||
) | ||
for _, row in get_toplevel_entries_query( | ||
session_id=session_id | ||
).iterrows() | ||
], | ||
token_count_threshold, | ||
), | ||
) |
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
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
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
Oops, something went wrong.