Skip to content
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

[Agent][Feat] Add remote file caching #139

Closed
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3ff5f74
Add file caching
Bobholamovic Dec 11, 2023
90e94c0
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 11, 2023
aa1ba26
Fix imports
Bobholamovic Dec 11, 2023
405edee
Fix bug
Bobholamovic Dec 11, 2023
b304ef3
Fix bug with dirty code
Bobholamovic Dec 11, 2023
000ebdb
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 11, 2023
7441f5c
Fix bug
Bobholamovic Dec 11, 2023
7cf623e
Refine unit tests
Bobholamovic Dec 13, 2023
9bfdb9a
Add unit tests
Bobholamovic Dec 13, 2023
fa5d4bf
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 13, 2023
5ea23b1
Eagerly create file
Bobholamovic Dec 13, 2023
d9b5071
Fix typing
Bobholamovic Dec 13, 2023
3a19e93
Fix bugs
Bobholamovic Dec 13, 2023
a796d27
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 13, 2023
655cfce
Merge branch 'develop' of https://github.com/PaddlePaddle/ERNIE-Bot-S…
Bobholamovic Dec 14, 2023
0a66bdb
More performant remote file client
Bobholamovic Dec 14, 2023
53e8878
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 14, 2023
4365e71
Fix ut cases
Bobholamovic Dec 14, 2023
df1eef4
Fix API name
Bobholamovic Dec 14, 2023
8366456
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 14, 2023
25e2525
Merge branch 'develop' into agent/dev/file_cache
Bobholamovic Dec 14, 2023
b532245
Merge branch 'develop' into agent/dev/file_cache
Bobholamovic Dec 18, 2023
e2164a1
Fix logging
Bobholamovic Dec 18, 2023
41809ae
Fix logging
Bobholamovic Dec 18, 2023
2373f55
Fix testing
Bobholamovic Dec 18, 2023
c43daa4
Merge remote-tracking branch 'official/develop' into agent/dev/file_c…
Bobholamovic Dec 18, 2023
22d5184
Merge remote-tracking branch 'official' into agent/dev/file_cache
Bobholamovic Dec 19, 2023
c567cd9
Fix precommit
Bobholamovic Dec 19, 2023
818bea0
Fix ut
Bobholamovic Dec 19, 2023
1529c88
Fix precommit
Bobholamovic Dec 19, 2023
4baf5f2
Fix file manager
Bobholamovic Dec 20, 2023
9b86141
Fix bugs
Bobholamovic Dec 20, 2023
cca8398
Fix bugs
Bobholamovic Dec 20, 2023
328268a
Fix FileManager factory
Bobholamovic Dec 20, 2023
ce65afc
Merge branch 'develop' into agent/dev/file_cache
Bobholamovic Dec 20, 2023
fb93046
Refactor file manager
Bobholamovic Dec 21, 2023
10585a1
Refactor file manager
Bobholamovic Dec 21, 2023
4ba7f04
Remove gradio typing
Bobholamovic Dec 21, 2023
398b2ef
Remove gradio type check
Bobholamovic Dec 21, 2023
7ca3855
Add prune method
Bobholamovic Dec 21, 2023
681e164
Add prune method
Bobholamovic Dec 21, 2023
3d64dc5
Merge remote-tracking branch 'official' into agent/dev/file_cache
Bobholamovic Dec 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions erniebot-agent/erniebot_agent/agents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import abc
import json
from typing import Any, Dict, List, Literal, Optional, Union
from typing import Any, Dict, List, Literal, Optional, Union, final

from erniebot_agent import file_io
from erniebot_agent.agents.callback.callback_manager import CallbackManager
Expand All @@ -27,9 +27,9 @@
ToolResponse,
)
from erniebot_agent.chat_models.base import ChatModel
from erniebot_agent.file_io import protocol
Bobholamovic marked this conversation as resolved.
Show resolved Hide resolved
from erniebot_agent.file_io.base import File
from erniebot_agent.file_io.file_manager import FileManager
from erniebot_agent.file_io.protocol import is_local_file_id, is_remote_file_id
from erniebot_agent.memory.base import Memory
from erniebot_agent.messages import Message, SystemMessage
from erniebot_agent.tools.base import BaseTool
Expand Down Expand Up @@ -74,13 +74,14 @@ def __init__(
else:
self._callback_manager = CallbackManager(callbacks)
if file_manager is None:
file_manager = file_io.get_file_manager()
file_manager = file_io.get_global_file_manager()
self._file_manager = file_manager

@property
def tools(self) -> List[BaseTool]:
return self._tool_manager.get_tools()

@final
async def async_run(self, prompt: str, files: Optional[List[File]] = None) -> AgentResponse:
await self._callback_manager.on_run_start(agent=self, prompt=prompt)
agent_resp = await self._async_run(prompt, files)
Expand Down Expand Up @@ -205,6 +206,7 @@ def _messages_to_dicts(messages):
async def _async_run(self, prompt: str, files: Optional[List[File]] = None) -> AgentResponse:
raise NotImplementedError

@final
async def _async_run_tool(self, tool_name: str, tool_args: str) -> ToolResponse:
tool = self._tool_manager.get_tool(tool_name)
await self._callback_manager.on_tool_start(agent=self, tool=tool, input_args=tool_args)
Expand All @@ -216,6 +218,7 @@ async def _async_run_tool(self, tool_name: str, tool_args: str) -> ToolResponse:
await self._callback_manager.on_tool_end(agent=self, tool=tool, response=tool_resp)
return tool_resp

@final
async def _async_run_llm(self, messages: List[Message], **opts: Any) -> LLMResponse:
await self._callback_manager.on_llm_start(agent=self, llm=self.llm, messages=messages)
try:
Expand Down Expand Up @@ -262,7 +265,7 @@ async def _sniff_and_extract_files_from_args(
agent_files: List[AgentFile] = []
for val in args.values():
if isinstance(val, str):
if is_local_file_id(val):
if protocol.is_local_file_id(val):
if self._file_manager is None:
logger.warning(
f"A file is used by {repr(tool)}, but the agent has no file manager to fetch it."
Expand All @@ -271,7 +274,7 @@ async def _sniff_and_extract_files_from_args(
file = self._file_manager.look_up_file_by_id(val)
if file is None:
raise RuntimeError(f"Unregistered ID {repr(val)} is used by {repr(tool)}.")
elif is_remote_file_id(val):
elif protocol.is_remote_file_id(val):
if self._file_manager is None:
logger.warning(
f"A file is used by {repr(tool)}, but the agent has no file manager to fetch it."
Expand Down
6 changes: 4 additions & 2 deletions erniebot-agent/erniebot_agent/agents/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from dataclasses import dataclass
from typing import Dict, List, Literal, Optional, Tuple, Union

from erniebot_agent.file_io import protocol
from erniebot_agent.file_io.base import File
from erniebot_agent.file_io.protocol import extract_file_ids
from erniebot_agent.messages import AIMessage, Message


Expand Down Expand Up @@ -80,6 +80,8 @@ def get_output_files(self) -> List[File]:
return [agent_file.file for agent_file in self.files if agent_file.type == "output"]

def get_tool_input_output_files(self, tool_name: str) -> Tuple[List[File], List[File]]:
# XXX: If a tool is used mutliple times, all related files will be
# returned in flattened lists.
input_files: List[File] = []
output_files: List[File] = []
for agent_file in self.files:
Expand All @@ -94,7 +96,7 @@ def get_tool_input_output_files(self, tool_name: str) -> Tuple[List[File], List[

def output_dict(self) -> Dict[str, List]:
# 1. split the text into parts and add file id to each part
file_ids = extract_file_ids(self.text)
file_ids = protocol.extract_file_ids(self.text)

places = []
for file_id in file_ids:
Expand Down
2 changes: 1 addition & 1 deletion erniebot-agent/erniebot_agent/file_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from erniebot_agent.file_io.factory import get_file_manager
from erniebot_agent.file_io.factory import get_global_file_manager
4 changes: 2 additions & 2 deletions erniebot-agent/erniebot_agent/file_io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
id: str,
filename: str,
byte_size: int,
created_at: int,
created_at: str,
Bobholamovic marked this conversation as resolved.
Show resolved Hide resolved
purpose: str,
metadata: Dict[str, Any],
) -> None:
Expand All @@ -40,7 +40,7 @@ def __eq__(self, other: object) -> bool:
if isinstance(other, File):
return self.id == other.id
else:
return False
return NotImplemented

def __repr__(self) -> str:
attrs_str = self._get_attrs_str()
Expand Down
Loading