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

如何在自己定义的role引入自己编写的tool? #1646

Open
cjrcjrljy opened this issue Dec 17, 2024 · 3 comments
Open

如何在自己定义的role引入自己编写的tool? #1646

cjrcjrljy opened this issue Dec 17, 2024 · 3 comments

Comments

@cjrcjrljy
Copy link

官方示例docs中用写好的DataInterpreter这个role进行tool的使用,但是我想自己创建一个class role来使用自定义的tool,要如何做呢

@shenchucheng
Copy link
Collaborator

可以继承DataInterpreter,然后tools传入你的工具

@cjrcjrljy
Copy link
Author

但是DataInterpreter的一些功能我不需要,并且写了新的提示词后还是会执行一些我不需要的步骤,所以怎样自己创建一个新的role然后可以使用tools

@iorisa
Copy link
Collaborator

iorisa commented Jan 16, 2025

DI的用法有示例:https://github.com/geekan/MetaGPT/blob/main/examples/di/crawl_webpage.py
主要是2部分:

  1. 创建对象的时候,把你要使用的工具注册进去,像这样(注意要确保import scrape_web_playwright成功):
di = DataInterpreter(tools=["scrape_web_playwright"])
  1. 创建工具的时候要使用@register_tool装饰器,还要添加google docstring格式的注释,DI根据这些注释来理解工具的用法和使用场景。
@register_tool(tags=["web scraping", "web"])
async def scrape_web_playwright(url: str):
    """
    Asynchronously Scrape and save the HTML structure and inner text content of a web page using Playwright.

    Args:
        url (str): The main URL to fetch inner text from.

    Returns:
        dict: The inner text content and html structure of the web page, keys are 'inner_text', 'html'.
    """
    ......

如果还要更进一步的改造,DI的调用模式是:_observe(.) -> _plan_and_act(.)循环,你可以根据你的需要继承和重写这两个函数。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants