-
Notifications
You must be signed in to change notification settings - Fork 52
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] Enhance FileManager
and simplify the use of file managers
#193
[Agent] Enhance FileManager
and simplify the use of file managers
#193
Conversation
@@ -1,6 +1,9 @@ | |||
.DEFAULT_GOAL = format lint type_check | |||
.DEFAULT_GOAL = dev |
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.
.DEFAULT_GOAL
不能是多目标
from erniebot_agent.utils.logging import logger | ||
from erniebot_agent.utils.mixins import GradioMixin | ||
|
||
logger = logging.getLogger(__name__) |
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.
采用统一的logging风格,后同。
@@ -27,15 +28,16 @@ | |||
ToolResponse, | |||
) | |||
from erniebot_agent.chat_models.base import ChatModel | |||
from erniebot_agent.file_io import protocol |
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.
直接使用模块protocol,而不是从中import方法,这样有利于测试时做patch。后同。
if file is None: | ||
raise RuntimeError(f"Unregistered ID {repr(val)} is used by {repr(tool)}.") | ||
elif is_remote_file_id(val): | ||
if protocol.is_file_id(val): |
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.
重构后FileManager
不再考虑多FileManager
共享一个FileRegistry
的情形,这大大简化了我们要处理的情况,例如这里只需要判断FileManager
在自己的registry中持有的文件。
@@ -40,14 +40,14 @@ def handlers(self) -> List[CallbackHandler]: | |||
|
|||
def add_handler(self, handler: CallbackHandler): | |||
if handler in self._handlers: | |||
raise RuntimeError(f"The callback handler {handler} is already registered.") | |||
raise ValueError(f"The callback handler {handler} is already registered.") |
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.
按照Python文档中的定义调整异常类型,后同。
erniebot-agent/src/erniebot_agent/file_io/global_file_manager.py
Outdated
Show resolved
Hide resolved
erniebot-agent/src/erniebot_agent/file_io/global_file_manager.py
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,119 @@ | |||
import contextlib |
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.
后续可用于模拟文件服务器
class TestAgentResponseAnnotations(unittest.TestCase): | ||
def setUp(self): | ||
class TestAgentResponseAnnotations(unittest.IsolatedAsyncioTestCase): | ||
async def asyncSetUp(self): |
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.
资源有申请就有释放。后同。
access_token: Optional[str], save_dir: Optional[str], **opts: Any | ||
) -> FileManager: | ||
if access_token is None: | ||
access_token = C.get_global_access_token() |
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.
是否需要将eb模型和file在环境变量中的ak进行强绑定呢~用户如果在环境变量中设置了ak的话就无法使用local file了。
不知道有没有一种情况用户只想将ak用于eb的模型调起 但是他这个ak没有开通remote file的服务。
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.
已增加enable_remote_file
选项控制是否启用remote file管理功能。
else: | ||
continue | ||
agent_files.append(AgentFile(file=file, type=file_type, used_by=tool.tool_name)) | ||
raise RuntimeError(f"Unregistered file with ID {repr(val)} is used by {repr(tool)}.") |
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.
感觉是不是可以把file type拼进去,用户可以知道是输入的文件出问题还是输出的?
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.
Done
return _global_file_manager | ||
|
||
|
||
async def configure_global_file_manager( |
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.
这里需要一个参数来设置使用是否使用remote_file,就是上午说的需要一个开关。用户设置了access token,是希望使用aistudio的tool,而不希望使用remote file。
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.
已增加enable_remote_file
选项控制是否启用remote file管理功能。
save_dir: Optional[FilePath] = None, | ||
*, | ||
default_file_type: Optional[Literal["local", "remote"]] = None, | ||
prune_on_close: bool = True, |
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.
prune_on_close参数主要什么作用?
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.
这个参数可以控制是否在对象close时自动清理那些可以安全删除的文件,如果设置为False
的话文件会被保留
return _global_file_manager | ||
|
||
|
||
async def configure_global_file_manager( |
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.
改成set_global_file_manager?
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.
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.
最新的代码里提供了set
方法,用于直接设置FileManager
对象。
更新之后factory的get_file_manager好像没有地方使用了,这个文件可以删除吗 |
已删除 |
f"A file is used by {repr(tool)}, but the agent has no file manager to fetch it." | ||
raise RuntimeError( | ||
f"Unregistered file with ID {repr(val)} is used by {repr(tool)}." | ||
f" File type: {file_type}" |
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.
突然发现在look_up_file_by_id中 如果file为None已经抛出FileError了
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.
已修改
file = self._file_manager.look_up_file_by_id(val) | ||
if protocol.is_file_id(val): | ||
file_manager = await self._get_file_manager() | ||
file = file_manager.look_up_file_by_id(val) |
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.
看是否要在这个地方try catch还是让他自然报错
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.
已修改为try-catch
…addlePaddle#193) * Fix makefiles * Fix bugs * Enhance file_io * Update library code * Update examples * Update tests * Fix exceptions * Fix error info * Remove use of environment variables in integration tests * Fix protocol * Fix type hints * Fix * Fix typing * Fix style * Fix cleanup bugs * Fix data race * Fix bugs * Show file type * Fix bugs * Fix linting issues * Fix linting issues * Fix integration tests * Remove unused file * Fix and enhance * Fix style * Fix CI * Update CI config * Fix style * Remove anchors * Enhance mixins
主要改动如下:
FileManager
的使用方式。具体而言:对于初级用户,修改全局file manager的使用方式,提供配置全局file manager的功能;对于进阶用户,提供基础的资源管理功能,允许更精细化的资源、对象生命周期控制。以下是对于初级用户的示例用法。用户不需要关心file manager,只需要设置环境变量或全局配置一次。File.write_contents_to
方法,用于将文件内容转存到本地文件中。更多细节详见comments。