MetaGPT icon indicating copy to clipboard operation
MetaGPT copied to clipboard

How to convert the output on the terminal into a TXT file?

Open yousia33 opened this issue 1 year ago • 2 comments

Code

from metagpt.config2 import Config
from metagpt.roles import Role
from metagpt.actions import Action
import asyncio
from metagpt.environment import Environment
from metagpt.team import Team
from metagpt.actions import write_teaching_plan

gpt4t = Config.default()  # Use default configuration from `config2.yaml` file

career1 = "Reporter"
career2 = "Editor"
career3 = "Photographer"
name1 = "Bob"
name2 = "Alice"
name3 = "Eric"

a1 = Action(config=gpt4t,name="Say",language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career1,career2,career3) )
a2 = Action(config=gpt4t,name="Say", language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career2,career1,career3) )
a3 = Action(config=gpt4t,name="Say", language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career3,career2,career1) )


A = Role(name="Bob", profile=career1, goal="explain my job to kids,remember {} is a {} and {} is a {}".format(name2,career2,name3,career3), actions=[a1], watch=[a2], config=gpt4t)

B = Role(name="Alice", profile=career2, goal="explain my job to kids", actions=[a2], watch=[a1], config=gpt4t)

C = Role(name="Eric", profile=career3, goal="explain my job to kids", actions=[a3], watch=[a1, a2],config=gpt4t)

env = Environment(desc="Film production")

team = Team(investment=10.0, env=env, roles=[A, B, C])
asyncio.run(team.run(idea="Topic: job explanation. Under 300 words per message.", send_to="Bob", n_round=2))

reference: customize_llms_for_roles_or_actions

yousia33 avatar Jun 16 '24 05:06 yousia33

I hope that conversations between different characters can be generated separately

yousia33 avatar Jun 16 '24 06:06 yousia33

You can extend the Environment class and customize the run method to capture the output of each role and then write it to a file.


from metagpt.config2 import Config
from metagpt.roles import Role
from metagpt.actions import Action
import asyncio
from metagpt.environment import Environment
from metagpt.team import Team
from metagpt.actions import write_teaching_plan

from metagpt.logs import logger
from contextvars import ContextVar
from metagpt.logs import set_llm_stream_logfunc


OUTPUT_FILE = ContextVar("output_file")


class MyEnvironment(Environment):
    
    async def run(self, k=1):
        """
        Process all Role runs at once
        """
        for _ in range(k):
            futures = []
            for role in self.roles.values():
                future = self._run_role(role)
                futures.append(future)

            await asyncio.gather(*futures)
            logger.debug(f"is idle: {self.is_idle}")

    async def _run_role(self, role):
        with open(role.name, "a") as f:
            OUTPUT_FILE.set(f)
            return await role.run()


def process_llm_stream(msg):
    f = OUTPUT_FILE.get()
    f.write(msg)

set_llm_stream_logfunc(process_llm_stream)

gpt4t = Config.default()  # Use default configuration from `config2.yaml` file

career1 = "Reporter"
career2 = "Editor"
career3 = "Photographer"
name1 = "Bob"
name2 = "Alice"
name3 = "Eric"

a1 = Action(config=gpt4t,name="Say",language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career1,career2,career3) )
a2 = Action(config=gpt4t,name="Say", language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career2,career1,career3) )
a3 = Action(config=gpt4t,name="Say", language="Chinese", instruction="Use chinese and don't repeat it,write a explanation to introduce your {} daily work and communications with {} and {} for kids".format(career3,career2,career1) )


A = Role(name="Bob", profile=career1, goal="explain my job to kids,remember {} is a {} and {} is a {}".format(name2,career2,name3,career3), actions=[a1], watch=[a2], config=gpt4t)

B = Role(name="Alice", profile=career2, goal="explain my job to kids", actions=[a2], watch=[a1], config=gpt4t)

C = Role(name="Eric", profile=career3, goal="explain my job to kids", actions=[a3], watch=[a1, a2],config=gpt4t)

env = MyEnvironment(desc="Film production")
team = Team(investment=10.0, env=env, roles=[A, B, C])
asyncio.run(team.run(idea="Topic: job explanation. Under 300 words per message.", send_to="Bob", n_round=2))

shenchucheng avatar Jul 16 '24 07:07 shenchucheng