-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(agents-api): Move gather_messages to its own model
Signed-off-by: Diwank Tomer <[email protected]>
- Loading branch information
Diwank Tomer
committed
Aug 14, 2024
1 parent
b1fc2a4
commit 3ab7758
Showing
12 changed files
with
137 additions
and
86 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,22 @@ | ||
""" | ||
Module: agents_api/models/docs | ||
This module is responsible for managing document-related operations within the application, particularly for agents and possibly other entities. It serves as a core component of the document management system, enabling features such as document creation, listing, deletion, and embedding of snippets for enhanced search and retrieval capabilities. | ||
Main functionalities include: | ||
- Creating new documents and associating them with agents or users. | ||
- Listing documents based on various criteria, including ownership and metadata filters. | ||
- Deleting documents by their unique identifiers. | ||
- Embedding document snippets for retrieval purposes. | ||
The module interacts with other parts of the application, such as the agents and users modules, to provide a comprehensive document management system. Its role is crucial in enabling document search, retrieval, and management features within the context of agents and users. | ||
This documentation aims to provide clear, concise, and sufficient context for new developers or contributors to understand the module's role without needing to dive deep into the code immediately. | ||
""" | ||
|
||
# ruff: noqa: F401, F403, F405 | ||
|
||
from .gather_messages import gather_messages | ||
from .get_cached_response import get_cached_response | ||
from .prepare_chat_context import prepare_chat_context | ||
from .set_cached_response import set_cached_response |
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,82 @@ | ||
from uuid import UUID | ||
|
||
from beartype import beartype | ||
from fastapi import HTTPException | ||
from pycozo.client import QueryException | ||
from pydantic import ValidationError | ||
|
||
from agents_api.autogen.Chat import ChatInput | ||
|
||
from ...autogen.openapi_model import DocReference, History | ||
from ...clients import embed | ||
from ...common.protocol.developers import Developer | ||
from ...common.protocol.sessions import ChatContext | ||
from ..docs.search_docs_hybrid import search_docs_hybrid | ||
from ..entry.get_history import get_history | ||
from ..utils import ( | ||
partialclass, | ||
rewrap_exceptions, | ||
) | ||
|
||
|
||
@rewrap_exceptions( | ||
{ | ||
QueryException: partialclass(HTTPException, status_code=400), | ||
ValidationError: partialclass(HTTPException, status_code=400), | ||
TypeError: partialclass(HTTPException, status_code=400), | ||
} | ||
) | ||
@beartype | ||
async def gather_messages( | ||
*, | ||
developer: Developer, | ||
session_id: UUID, | ||
chat_context: ChatContext, | ||
chat_input: ChatInput, | ||
): | ||
new_raw_messages = [msg.model_dump() for msg in chat_input.messages] | ||
recall = chat_input.recall | ||
|
||
assert len(new_raw_messages) > 0 | ||
|
||
# Get the session history | ||
history: History = get_history( | ||
developer_id=developer.id, | ||
session_id=session_id, | ||
allowed_sources=["api_request", "api_response", "tool_response", "summarizer"], | ||
) | ||
|
||
# Keep leaf nodes only | ||
relations = history.relations | ||
past_messages = [ | ||
entry.model_dump() | ||
for entry in history.entries | ||
if entry.id not in {r.head for r in relations} | ||
] | ||
|
||
if not recall: | ||
return past_messages, [] | ||
|
||
# Search matching docs | ||
[query_embedding, *_] = await embed.embed( | ||
inputs=[ | ||
f"{msg.get('name') or msg['role']}: {msg['content']}" | ||
for msg in new_raw_messages | ||
], | ||
join_inputs=True, | ||
) | ||
query_text = new_raw_messages[-1]["content"] | ||
|
||
# List all the applicable owners to search docs from | ||
active_agent_id = chat_context.get_active_agent().id | ||
user_ids = [user.id for user in chat_context.users] | ||
owners = [("user", user_id) for user_id in user_ids] + [("agent", active_agent_id)] | ||
|
||
doc_references: list[DocReference] = search_docs_hybrid( | ||
developer_id=developer.id, | ||
owners=owners, | ||
query=query_text, | ||
query_embedding=query_embedding, | ||
) | ||
|
||
return past_messages, doc_references |
File renamed without changes.
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
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.