diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 26af8a287..0b85b33a6 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -55,16 +55,13 @@ class CreateAgent(Action): class AgentCreator(Role): - def __init__( - self, - name: str = "Matrix", - profile: str = "AgentCreator", - agent_template: str = MULTI_ACTION_AGENT_CODE_EXAMPLE, - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Matrix" + profile: str = "AgentCreator" + agent_template: str = MULTI_ACTION_AGENT_CODE_EXAMPLE + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._init_actions([CreateAgent]) - self.agent_template = agent_template async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") @@ -86,10 +83,6 @@ if __name__ == "__main__": creator = AgentCreator(agent_template=agent_template) - # msg = """Write an agent called SimpleTester that will take any code snippet (str) - # and return a testing code (str) for testing - # the given code snippet. Use pytest as the testing framework.""" - msg = """ Write an agent called SimpleTester that will take any code snippet (str) and do the following: 1. write a testing code (str) for testing the given code snippet, save the testing code as a .py file in the current working directory; diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index 6805fd460..679aee948 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -10,9 +10,8 @@ import subprocess import fire from metagpt.actions import Action -from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.roles import Role +from metagpt.roles.role import Role, RoleReactMode from metagpt.schema import Message @@ -23,8 +22,7 @@ class SimpleWriteCode(Action): your code: """ - def __init__(self, name: str = "SimpleWriteCode", context=None, llm: LLM = None): - super().__init__(name, context, llm) + name: str = "SimpleWriteCode" async def run(self, instruction: str): prompt = self.PROMPT_TEMPLATE.format(instruction=instruction) @@ -44,8 +42,7 @@ class SimpleWriteCode(Action): class SimpleRunCode(Action): - def __init__(self, name: str = "SimpleRunCode", context=None, llm: LLM = None): - super().__init__(name, context, llm) + name: str = "SimpleRunCode" async def run(self, code_text: str): result = subprocess.run(["python3", "-c", code_text], capture_output=True, text=True) @@ -55,13 +52,11 @@ class SimpleRunCode(Action): class SimpleCoder(Role): - def __init__( - self, - name: str = "Alice", - profile: str = "SimpleCoder", - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Alice" + profile: str = "SimpleCoder" + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._init_actions([SimpleWriteCode]) async def _act(self) -> Message: @@ -76,15 +71,13 @@ class SimpleCoder(Role): class RunnableCoder(Role): - def __init__( - self, - name: str = "Alice", - profile: str = "RunnableCoder", - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Alice" + profile: str = "RunnableCoder" + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._init_actions([SimpleWriteCode, SimpleRunCode]) - self._set_react_mode(react_mode="by_order") + self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value) async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") diff --git a/examples/build_customized_multi_agents.py b/examples/build_customized_multi_agents.py index 030a4b339..518aa6324 100644 --- a/examples/build_customized_multi_agents.py +++ b/examples/build_customized_multi_agents.py @@ -8,7 +8,6 @@ import re import fire from metagpt.actions import Action, UserRequirement -from metagpt.llm import LLM from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message @@ -28,9 +27,7 @@ class SimpleWriteCode(Action): Return ```python your_code_here ``` with NO other texts, your code: """ - - def __init__(self, name: str = "SimpleWriteCode", context=None, llm: LLM = None): - super().__init__(name, context, llm) + name: str = "SimpleWriteCode" async def run(self, instruction: str): prompt = self.PROMPT_TEMPLATE.format(instruction=instruction) @@ -43,13 +40,11 @@ class SimpleWriteCode(Action): class SimpleCoder(Role): - def __init__( - self, - name: str = "Alice", - profile: str = "SimpleCoder", - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Alice" + profile: str = "SimpleCoder" + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._watch([UserRequirement]) self._init_actions([SimpleWriteCode]) @@ -62,8 +57,7 @@ class SimpleWriteTest(Action): your code: """ - def __init__(self, name: str = "SimpleWriteTest", context=None, llm: LLM = None): - super().__init__(name, context, llm) + name: str = "SimpleWriteTest" async def run(self, context: str, k: int = 3): prompt = self.PROMPT_TEMPLATE.format(context=context, k=k) @@ -76,13 +70,11 @@ class SimpleWriteTest(Action): class SimpleTester(Role): - def __init__( - self, - name: str = "Bob", - profile: str = "SimpleTester", - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Bob" + profile: str = "SimpleTester" + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._init_actions([SimpleWriteTest]) # self._watch([SimpleWriteCode]) self._watch([SimpleWriteCode, SimpleWriteReview]) # feel free to try this too @@ -106,8 +98,7 @@ class SimpleWriteReview(Action): Review the test cases and provide one critical comments: """ - def __init__(self, name: str = "SimpleWriteReview", context=None, llm: LLM = None): - super().__init__(name, context, llm) + name: str = "SimpleWriteReview" async def run(self, context: str): prompt = self.PROMPT_TEMPLATE.format(context=context) @@ -118,13 +109,11 @@ class SimpleWriteReview(Action): class SimpleReviewer(Role): - def __init__( - self, - name: str = "Charlie", - profile: str = "SimpleReviewer", - **kwargs, - ): - super().__init__(name, profile, **kwargs) + name: str = "Charlie" + profile: str = "SimpleReviewer" + + def __init__(self, **kwargs): + super().__init__(**kwargs) self._init_actions([SimpleWriteReview]) self._watch([SimpleWriteTest]) @@ -147,7 +136,7 @@ async def main( ) team.invest(investment=investment) - team.start_project(idea) + team.run_project(idea) await team.run(n_round=n_round) diff --git a/metagpt/actions/write_tutorial.py b/metagpt/actions/write_tutorial.py index 742b6742b..f33a6b114 100644 --- a/metagpt/actions/write_tutorial.py +++ b/metagpt/actions/write_tutorial.py @@ -55,7 +55,7 @@ class WriteContent(Action): name: str = "WriteContent" llm: BaseGPTAPI = Field(default_factory=LLM) - directory: str = "" + directory: dict = dict() language: str = "Chinese" async def run(self, topic: str, *args, **kwargs) -> str: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b9fde7d05..8edbdfca1 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -400,7 +400,7 @@ class Role(BaseModel): observed_pure = [msg.dict(exclude={"id": True}) for msg in observed] existed_pure = [msg.dict(exclude={"id": True}) for msg in existed] for idx, new in enumerate(observed_pure): - if new["cause_by"] in self._rc.watch and new not in existed_pure: + if (new["cause_by"] in self._rc.watch and new not in existed_pure) or (not self._rc.watch): news.append(observed[idx]) return news diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 2fce739e2..791dff5e2 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -9,13 +9,16 @@ """ from pydantic import Field +from semantic_kernel import Kernel +from semantic_kernel.orchestration.sk_function_base import SKFunctionBase from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner -from semantic_kernel.planning.basic_planner import BasicPlanner +from semantic_kernel.planning.basic_planner import BasicPlanner, Plan from metagpt.actions import UserRequirement from metagpt.actions.execute_task import ExecuteTask from metagpt.llm import LLM +from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.roles import Role from metagpt.schema import Message @@ -37,9 +40,14 @@ class SkAgent(Role): profile: str = "sk_agent" goal: str = "Execute task based on passed in task description" constraints: str = "" + + plan: Plan = None planner_cls: BasicPlanner = BasicPlanner planner: BasicPlanner = Field(default_factory=BasicPlanner) llm: BaseGPTAPI = Field(default_factory=LLM) + kernel: Kernel = Field(default_factory=Kernel) + import_semantic_skill_from_directory: str = "" + import_skill: dict[str, SKFunctionBase] = dict() def __init__(self, **kwargs) -> None: """Initializes the Engineer role with given attributes.""" diff --git a/metagpt/utils/make_sk_kernel.py b/metagpt/utils/make_sk_kernel.py index 83b4005ec..5edddd618 100644 --- a/metagpt/utils/make_sk_kernel.py +++ b/metagpt/utils/make_sk_kernel.py @@ -21,12 +21,14 @@ def make_sk_kernel(): if CONFIG.openai_api_type == "azure": kernel.add_chat_service( "chat_completion", - AzureChatCompletion(CONFIG.deployment_name, CONFIG.openai_base_url, CONFIG.openai_api_key), + AzureChatCompletion( + deployment_name=CONFIG.deployment_name, base_url=CONFIG.openai_base_url, api_key=CONFIG.openai_api_key + ), ) else: kernel.add_chat_service( "chat_completion", - OpenAIChatCompletion(CONFIG.openai_api_model, CONFIG.openai_api_key), + OpenAIChatCompletion(model_id=CONFIG.openai_api_model, api_key=CONFIG.openai_api_key), ) return kernel