Skip to content

Commit

Permalink
Refactor agent functionality (#589)
Browse files Browse the repository at this point in the history
* Refactor agent implementation: Phase 1
- Introduce boto3_stubs
- Improve type safety
- Remove `AgentContentModel`s and integrated into `ContentModel`
- Simplify code to fetch `MessageModel`

* Change the type of tool result to list.
Backend
- `RunResult.body`
- `ConverseApiToolResult.content`
- `ToolResultContentModelBody.content`
- `ToolResultContentBody.content`
- Return value of `internet_search()`

Frontend
- `AgentToolResult.content`
- `ToolCardProps.content`
- `AgentToolsProps.content`
- `AgentEvent.content`

* Unify the duplicated implementation of chat between WebSocket API and REST API.
- Migrate specific implementation in `websocket.process_chat_input()` to `usecases.chat()`.
- Remove the implementation of chat in `websocket.process_chat_input()`, and use `usecases.chat()` instead.

* Unify the implementation of agent chat and normal chat.
- Add agent (tool use) functionality to `ConverseApiStreamHandler`.
  - Add `toolConfig` support to `bedrock.compose_args_for_converse_api()`.
- Migrate the functionality of `AgentRunner` to `usecases.chat()`.
  - Migrate the test cases of agent functionality to `test_chat.py`.
  - Remove `agent.py` and `test_agent.py`.
- Change the parameters of `AgentTool.run()`.
  - Add `tool_use_id`.
  - Change `arg: T` to `input: dict[str, JsonValue]`.
- Now agent chat supports multi-modal content.
  - Use `InputChatContent` even if `bot.hasAgent`.

* Update mypy
- mypy 1.11.2 -> 1.13.0

* Fix mypy errors

* Fix python format warnings

* Fix errors of test cases.

* fix: correct content parsing logic in ToolCard to prevent duplicate entries

- Replaced flatMap with map to correctly handle each content item individually
- Removed dependency on content[0] to avoid repeated display of the first item

* chore: remove duplicated test for agent

* Add agent thought to `AgentToolList`.
- Improve type safety of frontend's `Content` and `AgentContent`.
- Support agent thought and multi-turn tool use in `agentThinkingState`.
- Add agent thought and localized tool name to `AgentToolList`.
  - Add localized name of `knowledge_base_tool`.
- Display text content as a thought that received before `AGENT_THINKING` message.

* Fix: Error handling in `ConverseApiStreamHandler`

* Remove `output` from result of `knowledge_base_tool`.

* Fix: Description of `ContentModel.body`

* Implement 'Retrieved Context Citation' of agent chat.
- Redefine the schema of related documents.
- Add database entity `RELATED_DOCUMENT`.
  - Store `SearchResult` and `ToolResult` as related document.
  - Delete related documents when a conversation is deleted.
- Add REST API `GET /conversation/{conversation_id}/related-documents` to retrieve related documents of a conversation.
  - Remove REST API `POST /conversation/related-documents`.
  - Now, related documents are linked to the entire conversation, not just a message.
- Refactor `ChatMessageMarkdown`
  - Convert citations with '[^source_id]' to sequential numbers in order of appearance such as '[^1]'.
  - Separate the component that displays a related document as `RelatedDocumentViewer`.
  - Instead of passing `ChatMessageMarkdown` the related document itself, pass a callback to retrieve the related document.
    - Treat used chunks as related documents as well.
  - Disable links to citation sources while streaming.
  - Optimize handling of streaming cursors.
- Improvement of internet search tool:
  - Treat search results as citable related documents, and display page titles and links in `RelatedDocumentViewer`.

* Omit `content` of `AGENT_TOOL_RESULT` notification if the content size exceeds 128KB
- If the result of agent has `source_id`, use it as the `source_id` of `RelatedDocument`.

* Fix: When notification via WebSocket fails, the entire process is treated as failure.

* Change the specifications for `AGENT_TOOL_RESULT` notification.
Specification changes
- Split `AGENT_TOOL_RESULT` to `AGENT_TOOL_RESULT` and `AGENT_RELATED_DOCUMENT`
  - `AGENT_TOOL_RESULT` contains only `status` of tool result.
  - `AGENT_RELATED_DOCUMENT` contains `RelatedDocument` of tool result.
- Change `ChatMessage`'s property `getRelatedDocument` back to `relatedDocuments`.
  - Also changed `ChatMessageMarkdown`, `AgentToolList`.
- Add `relatedDocuments` to `ToolCard`.
  - Show tool result as `RelatedDocument` instead of `AgentToolResultContent`.
  - For backward compatibility, show `AgentToolResultContent` if `RelatedDocument` is not found.

Type name changes
- `ConverseApiToolResult` and `RunResult`-> Integrated to `ToolRunResult`
  - Changed result type: `ToolResultModel` -> `RelatedDocumentModel`
  - Move the conversion code from `usecases/chat.py` to `agents/tools/agent_tool.py`
- `AgentResultType` -> `ToolFunctionResult`
- `AgentMessageModel` -> `SimpleMessageModel`
- `AgentMessage` -> `SimpleMessage`

* Display search results of Knowledge Base even when agents are not enabled.

---------

Co-authored-by: statefb <tmgf.15.su@gmail.com>
  • Loading branch information
Yukinobu-Mine and statefb authored Nov 28, 2024
1 parent 6251a73 commit 8218f75
Show file tree
Hide file tree
Showing 56 changed files with 4,012 additions and 2,818 deletions.
251 changes: 0 additions & 251 deletions backend/app/agents/agent.py

This file was deleted.

Loading

0 comments on commit 8218f75

Please sign in to comment.