-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Refactoring v3 mvp #1398
Refactoring v3 mvp #1398
Conversation
1. 改进工作流执行器逻辑: - 优化block执行条件判断,确保所有前置blocks完成 - 改进输入收集机制,提高效率并增加错误检查 2. 更新调度规则配置: - 将chat_normal规则类型从prefix改为keyword - 添加新的关键词触发条件 - 修改fallback规则的工作流为directStore 3. 新增功能: - 添加ChatMemoryDirectStore模块用于直接存储聊天记录 - 在聊天消息构造器中添加当前时间支持 4. 其他优化: - 改进帮助信息生成逻辑
…ring-v3-mvp # Conflicts: # framework/workflow/core/execution/executor.py # framework/workflow/implementations/blocks/llm/chat.py # framework/workflow/implementations/blocks/memory/chat_memory.py # framework/workflow/implementations/blocks/system/help.py # framework/workflow/implementations/blocks/system_blocks.py
带有变量替换的 ChatMessageConstructor 执行的序列图sequenceDiagram
participant ChatMessageConstructor
participant WorkflowExecutor
participant LLMChatMessage
ChatMessageConstructor->>WorkflowExecutor: resolve()
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor->>ChatMessageConstructor: substitute_variables(system_prompt_format, executor)
activate ChatMessageConstructor
ChatMessageConstructor->>WorkflowExecutor: get_variable(var_name, default)
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor-->>ChatMessageConstructor: replace_var(match)
deactivate ChatMessageConstructor
ChatMessageConstructor->>ChatMessageConstructor: substitute_variables(user_prompt_format, executor)
activate ChatMessageConstructor
ChatMessageConstructor->>WorkflowExecutor: get_variable(var_name, default)
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor-->>ChatMessageConstructor: replace_var(match)
deactivate ChatMessageConstructor
ChatMessageConstructor->>LLMChatMessage: Create LLMChatMessage(role='system', content=system_prompt)
ChatMessageConstructor->>LLMChatMessage: Create LLMChatMessage(role='user', content=user_prompt)
ChatMessageConstructor-->>ChatMessageConstructor: return llm_msg
文件级别变更
提示和命令与 Sourcery 互动
自定义您的体验访问您的 仪表板 以:
获取帮助Original review guide in EnglishReviewer's Guide by SourceryThis pull request introduces a new Sequence diagram for ChatMemoryDirectStore executionsequenceDiagram
participant User
participant ChatMemoryDirectStore
participant MemoryManager
participant ScopeRegistry
participant ComposerRegistry
User->>ChatMemoryDirectStore: execute(user_msg: IMMessage)
activate ChatMemoryDirectStore
ChatMemoryDirectStore->>MemoryManager: resolve()
activate MemoryManager
deactivate MemoryManager
ChatMemoryDirectStore->>ScopeRegistry: resolve()
activate ScopeRegistry
ChatMemoryDirectStore->>ScopeRegistry: get_scope(scope_type)
activate ScopeRegistry
deactivate ScopeRegistry
deactivate ScopeRegistry
ChatMemoryDirectStore->>ComposerRegistry: resolve()
activate ComposerRegistry
ChatMemoryDirectStore->>ComposerRegistry: get_composer("default")
activate ComposerRegistry
deactivate ComposerRegistry
deactivate ComposerRegistry
ChatMemoryDirectStore->>ComposerRegistry: compose(user_msg.sender, composed_messages)
activate ComposerRegistry
deactivate ComposerRegistry
ChatMemoryDirectStore->>MemoryManager: store(scope, memory_entries)
activate MemoryManager
deactivate MemoryManager
deactivate ChatMemoryDirectStore
Sequence diagram for ChatMessageConstructor execution with variable substitutionsequenceDiagram
participant ChatMessageConstructor
participant WorkflowExecutor
participant LLMChatMessage
ChatMessageConstructor->>WorkflowExecutor: resolve()
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor->>ChatMessageConstructor: substitute_variables(system_prompt_format, executor)
activate ChatMessageConstructor
ChatMessageConstructor->>WorkflowExecutor: get_variable(var_name, default)
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor-->>ChatMessageConstructor: replace_var(match)
deactivate ChatMessageConstructor
ChatMessageConstructor->>ChatMessageConstructor: substitute_variables(user_prompt_format, executor)
activate ChatMessageConstructor
ChatMessageConstructor->>WorkflowExecutor: get_variable(var_name, default)
activate WorkflowExecutor
deactivate WorkflowExecutor
ChatMessageConstructor-->>ChatMessageConstructor: replace_var(match)
deactivate ChatMessageConstructor
ChatMessageConstructor->>LLMChatMessage: Create LLMChatMessage(role='system', content=system_prompt)
ChatMessageConstructor->>LLMChatMessage: Create LLMChatMessage(role='user', content=user_prompt)
ChatMessageConstructor-->>ChatMessageConstructor: return llm_msg
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
嘿 @chuanSir123 - 我已经审查了你的更改 - 这里有一些反馈:
总体评论:
- 考虑将此 PR 中所做更改的描述添加到描述部分。
- 包含对新的
ChatMemoryDirectStore
块的用途和用法的简要说明可能会有所帮助。
以下是我在审查期间查看的内容
- 🟢 一般问题:一切看起来都不错
- 🟢 安全性:一切看起来都不错
- 🟢 测试:一切看起来都不错
- 🟡 复杂性:发现 1 个问题
- 🟢 文档:一切看起来都不错
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进您的评论。
Original comment in English
Hey @chuanSir123 - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a description of the changes made in this PR to the description section.
- It might be helpful to include a brief explanation of the purpose and usage of the new
ChatMemoryDirectStore
block.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
||
return {} |
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.
issue (complexity): 考虑将通用初始化逻辑提取到共享基类或辅助方法中,以减少代码重复。
现在您有三个类,它们具有几乎相同的逻辑来解析内存管理器、确定作用域和检索注册表。为了在不改变功能的情况下减少复杂性和重复,请考虑将通用初始化步骤提取到共享基类或辅助方法中。例如,您可以创建一个如下所示的基类:
class BaseChatMemoryStore(Block):
container: DependencyContainer
def __init__(self, scope_type: Optional[str] = None):
self.scope_type = scope_type
def setup_scope(self):
self.memory_manager = self.container.resolve(MemoryManager)
if self.scope_type is None:
self.scope_type = self.memory_manager.config.default_scope
scope_registry = self.container.resolve(ScopeRegistry)
self.scope = scope_registry.get_scope(self.scope_type)
def get_composer(self):
composer_registry = self.container.resolve(ComposerRegistry)
return composer_registry.get_composer("default")
然后更新您的 ChatMemoryStore
和 ChatMemoryDirectStore
以从此基类继承。例如:
class ChatMemoryStore(BaseChatMemoryStore):
name = "chat_memory_store"
inputs = {
"user_msg": Input("user_msg", "用户消息", IMMessage, "用户消息"),
"llm_resp": Input("llm_resp", "LLM 响应", LLMChatResponse, "LLM 响应")
}
outputs = {}
def execute(self, user_msg: IMMessage, llm_resp: LLMChatResponse) -> Dict[str, Any]:
self.setup_scope()
composer = self.get_composer()
composed_messages = [user_msg]
if llm_resp.choices and llm_resp.choices[0].message:
composed_messages.append(llm_resp.choices[0].message)
print(composed_messages)
memory_entries = composer.compose(user_msg.sender, composed_messages)
self.memory_manager.store(self.scope, memory_entries)
return {}
class ChatMemoryDirectStore(BaseChatMemoryStore):
name = "chat_memory_store"
inputs = {"user_msg": Input("user_msg", IMMessage, "User message")}
outputs = {}
def execute(self, user_msg: IMMessage) -> Dict[str, Any]:
self.setup_scope()
composer = self.get_composer()
composed_messages = [user_msg]
print(composed_messages)
memory_entries = composer.compose(user_msg.sender, composed_messages)
self.memory_manager.store(self.scope, memory_entries)
return {}
此重构保持所有功能完整,同时减少重复的代码块和整体复杂性。
Original comment in English
issue (complexity): Consider extracting the common initialization logic into a shared base class or helper methods to reduce code duplication.
You now have three classes with nearly identical logic for resolving the memory manager, determining the scope, and retrieving registries. To reduce complexity and duplication without altering functionality, consider extracting the common initialization steps into a shared base class or helper methods. For example, you might create a base class like this:
class BaseChatMemoryStore(Block):
container: DependencyContainer
def __init__(self, scope_type: Optional[str] = None):
self.scope_type = scope_type
def setup_scope(self):
self.memory_manager = self.container.resolve(MemoryManager)
if self.scope_type is None:
self.scope_type = self.memory_manager.config.default_scope
scope_registry = self.container.resolve(ScopeRegistry)
self.scope = scope_registry.get_scope(self.scope_type)
def get_composer(self):
composer_registry = self.container.resolve(ComposerRegistry)
return composer_registry.get_composer("default")
Then update your ChatMemoryStore
and ChatMemoryDirectStore
to inherit from this base class. For example:
class ChatMemoryStore(BaseChatMemoryStore):
name = "chat_memory_store"
inputs = {
"user_msg": Input("user_msg", "用户消息", IMMessage, "用户消息"),
"llm_resp": Input("llm_resp", "LLM 响应", LLMChatResponse, "LLM 响应")
}
outputs = {}
def execute(self, user_msg: IMMessage, llm_resp: LLMChatResponse) -> Dict[str, Any]:
self.setup_scope()
composer = self.get_composer()
composed_messages = [user_msg]
if llm_resp.choices and llm_resp.choices[0].message:
composed_messages.append(llm_resp.choices[0].message)
print(composed_messages)
memory_entries = composer.compose(user_msg.sender, composed_messages)
self.memory_manager.store(self.scope, memory_entries)
return {}
class ChatMemoryDirectStore(BaseChatMemoryStore):
name = "chat_memory_store"
inputs = {"user_msg": Input("user_msg", IMMessage, "User message")}
outputs = {}
def execute(self, user_msg: IMMessage) -> Dict[str, Any]:
self.setup_scope()
composer = self.get_composer()
composed_messages = [user_msg]
print(composed_messages)
memory_entries = composer.compose(user_msg.sender, composed_messages)
self.memory_manager.store(self.scope, memory_entries)
return {}
This refactor keeps all functionality intact while reducing duplicated code blocks and overall complexity.
LGTM, thanks! |
Sourcery 总结
重构了工作流执行器、变量替换和帮助命令生成。引入了一个新的工作流用于直接存储聊天记忆以及一个相应的块。更新了回退调度规则以使用新的工作流。
新特性:
chat:directStore
,它直接将用户消息存储到聊天记忆中,当其他调度规则未能匹配时,作为回退机制触发。此工作流简化了保存聊天历史记录的过程,并通过为未匹配的消息提供默认操作来改善用户体验。增强功能:
ChatMessageConstructor
块中的变量替换,以支持对象属性和字典键的访问。chat_memory_direct_store
块,用于直接存储聊天记忆。ChatMessageConstructor
块中的系统提示中添加当前日期时间。Original summary in English
Summary by Sourcery
Refactor the workflow executor, variable substitution, and help command generation. Introduce a new workflow for directly storing chat memory and a corresponding block. Update the fallback dispatch rule to use the new workflow.
New Features:
chat:directStore
that directly stores user messages to chat memory, triggered as a fallback mechanism when other dispatch rules fail to match. This workflow simplifies the process of saving chat history and improves the user experience by providing a default action for unmatched messagesEnhancements:
ChatMessageConstructor
block to support object properties and dictionary keys access.chat_memory_direct_store
block to directly store chat memory.ChatMessageConstructor
block