您的位置:首页 > 游戏 > 游戏 > 网络营销的工作岗位_怎么开微信商城_自学seo能找到工作吗_网站检测工具

网络营销的工作岗位_怎么开微信商城_自学seo能找到工作吗_网站检测工具

2025/2/22 2:45:53 来源:https://blog.csdn.net/qq_41472205/article/details/144517846  浏览:    关键词:网络营销的工作岗位_怎么开微信商城_自学seo能找到工作吗_网站检测工具
网络营销的工作岗位_怎么开微信商城_自学seo能找到工作吗_网站检测工具

1. PrepareDocuments Action

定义了一个 PrepareDocuments 类,它继承自 Action 类,并实现了一个用于准备项目文档的功能。具体来说,它的主要作用是初始化项目文件夹,设置 Git 环境,并将新增的需求写入 docs/requirements.txt 文件。部分代码跟源码有点不同。

import shutil
from pathlib import Path
from typing import Optionalfrom metagpt.actions import Action
from metagpt.const import REQUIREMENT_FILENAME
from metagpt.utils.file_repository import FileRepository
from metagpt.utils.git_repository import GitRepository
from metagpt.utils.project_repo import ProjectRepofrom metagpt.schema import Documentimport dataclasses@dataclasses.dataclass  
class ActionOutput:content: strinstruct_content: Documentdef __init__(self, content: str, instruct_content: Document):self.content = contentself.instruct_content = instruct_contentclass PrepareDocuments(Action):"""PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt."""name: str = "PrepareDocuments"i_context: Optional[str] = None@propertydef config(self):return self.context.configdef _init_repo(self):"""Initialize the Git environment."""if not self.config.project_path:name = self.config.project_name or FileRepository.new_filename()path = Path(self.config.workspace.path) / nameelse:path = Path(self.config.project_path)if path.exists() and not self.config.inc:shutil.rmtree(path)self.config.project_path = pathself.context.git_repo = GitRepository(local_path=path, auto_init=True)self.context.repo = ProjectRepo(self.context.git_repo)async def run(self, with_messages, **kwargs):"""Create and initialize the workspace folder, initialize the Git environment."""self._init_repo()# Write the newly added requirements from the main parameter idea to `docs/requirement.txt`.doc = await self.repo.docs.save(filename=REQUIREMENT_FILENAME, content=with_messages[0].content)# Send a Message notification to the WritePRD action, instructing it to process requirements using# `docs/requirement.txt` and `docs/prd/`.return ActionOutput(content=doc.content, instruct_content=doc)

2. 示例

context = Context()
from metagpt.schema import Message
USER_REQUIREMENT = """开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结"""from metagpt.actions import UserRequirement
user_msg = Message(role="User", content=USER_REQUIREMENT, cause_by=UserRequirement)
product_manager = PrepareDocuments(context=context)rsp = await product_manager.run([user_msg])
print(rsp)

结果:

2024-12-16 20:31:30.767 | INFO     | metagpt.utils.file_repository:save:57 - save to: D:\llm\MetaGPT\workspace\20241216203129\docs\requirement.txtActionOutput(content='开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结', instruct_content=开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结)

在workspace文件夹中产生的项目结构
在这里插入图片描述

3. 代码详解

导入部分

import shutil
from pathlib import Path
from typing import Optional
  • shutil 用于文件和目录的操作(如删除目录)。
  • Path 是 pathlib 模块的一部分,用于处理路径。
  • Optional 是类型注解中的一个工具,表示某个值可以是某个类型或者 None。
from metagpt.actions import Action
from metagpt.const import REQUIREMENT_FILENAME
from metagpt.utils.file_repository import FileRepository
from metagpt.utils.git_repository import GitRepository
from metagpt.utils.project_repo import ProjectRepo
from metagpt.schema import Document

导入了 metagpt 模块中的一些类和常量,主要用于操作 Git 仓库、文件库和项目仓库等。

ActionOutput 类

@dataclasses.dataclass  
class ActionOutput:content: strinstruct_content: Documentdef __init__(self, content: str, instruct_content: Document):self.content = contentself.instruct_content = instruct_content

这个类用于保存操作的输出结果,包含两个字段:

  • content:操作的结果内容,类型为 str。
  • instruct_content:Document 类型,可能是与操作相关的文档内容。

@dataclasses.dataclass 装饰器简化了类的定义,自动生成了初始化方法。

PrepareDocuments 类

class PrepareDocuments(Action):"""PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt."""name: str = "PrepareDocuments"i_context: Optional[str] = None

PrepareDocuments 类继承自 Action 类,表示一个特定的行动,目的是初始化项目文件夹并将新的需求添加到 docs/requirements.txt 文件。

  • name 是该动作的名称,i_context 是上下文(可选),它会用于定义在动作执行期间的环境信息。
@property
def config(self):return self.context.config

config 是一个只读属性,返回当前上下文的配置对象。

初始化 Git 仓库

def _init_repo(self):"""Initialize the Git environment."""if not self.config.project_path:name = self.config.project_name or FileRepository.new_filename()path = Path(self.config.workspace.path) / nameelse:path = Path(self.config.project_path)if path.exists() and not self.config.inc:shutil.rmtree(path)self.config.project_path = pathself.context.git_repo = GitRepository(local_path=path, auto_init=True)self.context.repo = ProjectRepo(self.context.git_repo)
  • _init_repo 方法用于初始化 Git 仓库。它会检查是否有现有的项目路径,如果没有,则创建一个新的路径。
  • 如果该路径已经存在且配置中没有指示是否覆盖(config.inc 为 False),则删除现有的目录。
  • 随后,使用 GitRepository 初始化一个新的 Git 仓库,并使用 ProjectRepo 创建一个项目仓库。

执行操作

async def run(self, with_messages, **kwargs):"""Create and initialize the workspace folder, initialize the Git environment."""self._init_repo()

run 方法是操作的主要执行入口。它会调用 _init_repo 方法初始化 Git 环境。

# Write the newly added requirements from the main parameter idea to `docs/requirement.txt`.
doc = await self.repo.docs.save(filename=REQUIREMENT_FILENAME, content=with_messages[0].content)

将传入的 with_messages[0].content 写入 docs/requirement.txt 文件。with_messages 似乎是包含多个消息的列表,with_messages[0] 可能是包含需求内容的消息。
使用 await 表明这是一个异步操作,repo.docs.save 方法将内容保存到文档中。

# Send a Message notification to the WritePRD action, instructing it to process requirements using
# `docs/requirement.txt` and `docs/prd/`.
return ActionOutput(content=doc.content, instruct_content=doc)

操作完成后,返回一个 ActionOutput 对象,包含保存的文档内容和相关指示信息,通知后续的行动(例如处理需求的 WritePRD 行动)。

总结

PrepareDocuments 类是一个在项目中进行文件准备和 Git 环境初始化的行动。
它会根据配置初始化 Git 仓库,并将传入的需求内容写入 docs/requirements.txt 文件。
最后,返回一个 ActionOutput 对象,包含文档的内容以及需要后续处理的信息。

PrepareDocuments的数据结构

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com