MetaGPT icon indicating copy to clipboard operation
MetaGPT copied to clipboard

希望提供一个完整一点的“为角色或动作配置不同LLM” 示例

Open MinhatOwner opened this issue 8 months ago • 11 comments

希望提供一个完整一点的“为角色或动作配置不同LLM”

我按照官方的文档来不能实现这个 功能

MinhatOwner avatar Apr 10 '25 04:04 MinhatOwner

我这里是为action配置不同LLM Image

lwjin-1112 avatar Apr 11 '25 04:04 lwjin-1112

我这里是为action配置不同LLM Image

还是看不懂啊

MinhatOwner avatar Apr 11 '25 05:04 MinhatOwner

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

lwjin-1112 avatar Apr 11 '25 06:04 lwjin-1112

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

MinhatOwner avatar Apr 11 '25 06:04 MinhatOwner

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

同时你也可以参考这个链接,是之前提出的问题和回复:https://github.com/geekan/MetaGPT/issues/1737

lwjin-1112 avatar Apr 11 '25 07:04 lwjin-1112

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

同时你也可以参考这个链接,是之前提出的问题和回复:#1737

@lwjin-1112

我试了一下还是报错,下面是我的代码:


"""
为角色或动作配置不同LLM
@Time    : 2025年4月9日11点21分
@Author  : Minhat
@File    : 011_为角色或动作配置不同LLM.py
"""

import asyncio
import re

from metagpt.actions import Action
from metagpt.config2 import Config
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.team import Team

# 使用用户配置
openaiConfig = Config.from_llm_config({
    "api_type": "openai",
    "api_key": "sk-3242342343243242243",
    "model": "gpt-4-turbo",
    "base_url": "https://api.mixrai.com/v1"
})


class SimpleWriteCode(Action):
    """
    定义动作,该动作完成编写Python代码
    """
    PROMPT_TEMPLATE: str = """
    编写一个Python函数,可以{instruction}并提供两个可运行的测试用例。
    返回```python your_code_here ```,不要包含其他文本,
    你的代码:
    """

    name: str = "SimpleWriteCode"

    async def run(self, instruction: str):
        prompt = self.PROMPT_TEMPLATE.format(instruction=instruction)
        rsp: str = await self._aask(prompt)
        code_text: str = SimpleWriteCode.parse_code(rsp)
        return code_text

    @staticmethod
    def parse_code(rsp):
        pattern = r"```python(.*)```"
        match = re.search(pattern, rsp, re.DOTALL)
        code_text = match.group(1) if match else rsp
        return code_text


class WriteCoder(Role):
    """
    运行代码的智能体
    """
    name: str = "Alice"
    profile: str = "RunnableCoder"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.actions = [SimpleWriteCode(config = openaiConfig)]
        self._set_react_mode(react_mode="by_order")


async def main():
    idea = "编写一个把当前时间写到Windows的D盘名为LocalDatetime.txt文件中,如果文件不存在就创建该文件。"
    investment: float = 3.0
    n_round: int = 5
    team = Team()
    team.hire([WriteCoder()])
    team.invest(investment=investment)
    team.run_project(idea)
    await team.run(n_round=n_round)


if __name__ == '__main__':
    asyncio.run(main())

MinhatOwner avatar Apr 11 '25 07:04 MinhatOwner

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

同时你也可以参考这个链接,是之前提出的问题和回复:#1737

@lwjin-1112

我试了一下还是报错,下面是我的代码:

""" 为角色或动作配置不同LLM @Time : 2025年4月9日11点21分 @Author : Minhat @File : 011_为角色或动作配置不同LLM.py """

import asyncio import re

from metagpt.actions import Action from metagpt.config2 import Config from metagpt.roles import Role from metagpt.schema import Message from metagpt.team import Team

使用用户配置

openaiConfig = Config.from_llm_config({ "api_type": "openai", "api_key": "sk-3242342343243242243", "model": "gpt-4-turbo", "base_url": "https://api.mixrai.com/v1" })

class SimpleWriteCode(Action): """ 定义动作,该动作完成编写Python代码 """ PROMPT_TEMPLATE: str = """ 编写一个Python函数,可以{instruction}并提供两个可运行的测试用例。 返回python your_code_here ,不要包含其他文本, 你的代码: """

name: str = "SimpleWriteCode"

async def run(self, instruction: str):
    prompt = self.PROMPT_TEMPLATE.format(instruction=instruction)
    rsp: str = await self._aask(prompt)
    code_text: str = SimpleWriteCode.parse_code(rsp)
    return code_text

@staticmethod
def parse_code(rsp):
    pattern = r"```python(.*)```"
    match = re.search(pattern, rsp, re.DOTALL)
    code_text = match.group(1) if match else rsp
    return code_text

class WriteCoder(Role): """ 运行代码的智能体 """ name: str = "Alice" profile: str = "RunnableCoder"

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.actions = [SimpleWriteCode(config = openaiConfig)]
    self._set_react_mode(react_mode="by_order")

async def main(): idea = "编写一个把当前时间写到Windows的D盘名为LocalDatetime.txt文件中,如果文件不存在就创建该文件。" investment: float = 3.0 n_round: int = 5 team = Team() team.hire([WriteCoder()]) team.invest(investment=investment) team.run_project(idea) await team.run(n_round=n_round)

if name == 'main': asyncio.run(main())

stop!!!!!!!你暴露了你的key,快撤回

lwjin-1112 avatar Apr 11 '25 07:04 lwjin-1112

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

同时你也可以参考这个链接,是之前提出的问题和回复:#1737

@lwjin-1112 我试了一下还是报错,下面是我的代码: """ 为角色或动作配置不同LLM @time : 2025年4月9日11点21分 @author : Minhat @file : 011_为角色或动作配置不同LLM.py """ import asyncio import re from metagpt.actions import Action from metagpt.config2 import Config from metagpt.roles import Role from metagpt.schema import Message from metagpt.team import Team

使用用户配置

openaiConfig = Config.from_llm_config({ "api_type": "openai", "api_key": "sk-3242342343243242243", "model": "gpt-4-turbo", "base_url": "https://api.mixrai.com/v1" }) class SimpleWriteCode(Action): """ 定义动作,该动作完成编写Python代码 """ PROMPT_TEMPLATE: str = """ 编写一个Python函数,可以{instruction}并提供两个可运行的测试用例。 返回python your_code_here ,不要包含其他文本, 你的代码: """

name: str = "SimpleWriteCode"

async def run(self, instruction: str):
    prompt = self.PROMPT_TEMPLATE.format(instruction=instruction)
    rsp: str = await self._aask(prompt)
    code_text: str = SimpleWriteCode.parse_code(rsp)
    return code_text

@staticmethod
def parse_code(rsp):
    pattern = r"```python(.*)```"
    match = re.search(pattern, rsp, re.DOTALL)
    code_text = match.group(1) if match else rsp
    return code_text

class WriteCoder(Role): """ 运行代码的智能体 """ name: str = "Alice" profile: str = "RunnableCoder"

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.actions = [SimpleWriteCode(config = openaiConfig)]
    self._set_react_mode(react_mode="by_order")

async def main(): idea = "编写一个把当前时间写到Windows的D盘名为LocalDatetime.txt文件中,如果文件不存在就创建该文件。" investment: float = 3.0 n_round: int = 5 team = Team() team.hire([WriteCoder()]) team.invest(investment=investment) team.run_project(idea) await team.run(n_round=n_round) if name == 'main': asyncio.run(main())

stop!!!!!!!你暴露了你的key,快撤回

我那个是假的吧

MinhatOwner avatar Apr 11 '25 07:04 MinhatOwner

我这里是为action配置不同LLMImage

还是看不懂啊

首先导入模块读取默认的配置 from metagpt.config2 import Config 然后修改model的值,在为action类配置时,因为是在role中添加动作,添加时指定config即可(第一个框那样);若是为role配置时,在env添加role时,指定config即可(像第一个框),简而言之,在哪里添加action或role,就在添加时指定config

好的,我试试,谢谢

同时你也可以参考这个链接,是之前提出的问题和回复:#1737

@lwjin-1112

我试了一下还是报错,下面是我的代码:

""" 为角色或动作配置不同LLM @Time : 2025年4月9日11点21分 @Author : Minhat @File : 011_为角色或动作配置不同LLM.py """

import asyncio import re

from metagpt.actions import Action from metagpt.config2 import Config from metagpt.roles import Role from metagpt.schema import Message from metagpt.team import Team

使用用户配置

openaiConfig = Config.from_llm_config({ "api_type": "openai", "api_key": "sk-3242342343243242243", "model": "gpt-4-turbo", "base_url": "https://api.mixrai.com/v1" })

class SimpleWriteCode(Action): """ 定义动作,该动作完成编写Python代码 """ PROMPT_TEMPLATE: str = """ 编写一个Python函数,可以{instruction}并提供两个可运行的测试用例。 返回python your_code_here ,不要包含其他文本, 你的代码: """

name: str = "SimpleWriteCode"

async def run(self, instruction: str):
    prompt = self.PROMPT_TEMPLATE.format(instruction=instruction)
    rsp: str = await self._aask(prompt)
    code_text: str = SimpleWriteCode.parse_code(rsp)
    return code_text

@staticmethod
def parse_code(rsp):
    pattern = r"```python(.*)```"
    match = re.search(pattern, rsp, re.DOTALL)
    code_text = match.group(1) if match else rsp
    return code_text

class WriteCoder(Role): """ 运行代码的智能体 """ name: str = "Alice" profile: str = "RunnableCoder"

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.actions = [SimpleWriteCode(config = openaiConfig)]
    self._set_react_mode(react_mode="by_order")

async def main(): idea = "编写一个把当前时间写到Windows的D盘名为LocalDatetime.txt文件中,如果文件不存在就创建该文件。" investment: float = 3.0 n_round: int = 5 team = Team() team.hire([WriteCoder()]) team.invest(investment=investment) team.run_project(idea) await team.run(n_round=n_round)

if name == 'main': asyncio.run(main())

在WriteCoder中,没有重写_act方法(红框部分),这让我是可以正常运行的

Image

lwjin-1112 avatar Apr 11 '25 08:04 lwjin-1112

我好像一直搞错了一个问题,“为角色或动作配置不同LLM”

其实我想做的是为不同的角色和动作配置不同厂商的LLM

MinhatOwner avatar Apr 11 '25 08:04 MinhatOwner

我好像一直搞错了一个问题,“为角色或动作配置不同LLM”

其实我想做的是为不同的角色和动作配置不同厂商的LLM

那你需要MCP + A2A .

qwas982 avatar Apr 14 '25 22:04 qwas982

This issue has no activity in the past 30 days. Please comment on the issue if you have anything to add.

github-actions[bot] avatar May 15 '25 00:05 github-actions[bot]

This issue was closed due to 45 days of inactivity. If you feel this issue is still relevant, please reopen the issue to continue the discussion.

github-actions[bot] avatar May 29 '25 00:05 github-actions[bot]