refine code

This commit is contained in:
geekan 2024-01-09 17:01:21 +08:00
parent 33db023e94
commit 7e20e54717
20 changed files with 43 additions and 45 deletions

View file

@ -61,7 +61,7 @@ class AgentCreator(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([CreateAgent])
self.add_actions([CreateAgent])
async def _act(self) -> Message:
logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})")

View file

@ -57,7 +57,7 @@ class SimpleCoder(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SimpleWriteCode])
self.add_actions([SimpleWriteCode])
async def _act(self) -> Message:
logger.info(f"{self._setting}: to do {self.rc.todo}({self.rc.todo.name})")
@ -76,7 +76,7 @@ class RunnableCoder(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SimpleWriteCode, SimpleRunCode])
self.add_actions([SimpleWriteCode, SimpleRunCode])
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)
async def _act(self) -> Message:

View file

@ -46,7 +46,7 @@ class SimpleCoder(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._watch([UserRequirement])
self._init_actions([SimpleWriteCode])
self.add_actions([SimpleWriteCode])
class SimpleWriteTest(Action):
@ -75,7 +75,7 @@ class SimpleTester(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SimpleWriteTest])
self.add_actions([SimpleWriteTest])
# self._watch([SimpleWriteCode])
self._watch([SimpleWriteCode, SimpleWriteReview]) # feel free to try this too
@ -114,7 +114,7 @@ class SimpleReviewer(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([SimpleWriteReview])
self.add_actions([SimpleWriteReview])
self._watch([SimpleWriteTest])

View file

@ -49,7 +49,7 @@ class Debator(Role):
def __init__(self, **data: Any):
super().__init__(**data)
self._init_actions([SpeakAloud])
self.add_actions([SpeakAloud])
self._watch([UserRequirement, SpeakAloud])
async def _observe(self) -> int:

View file

@ -104,7 +104,7 @@ class Context(LLMMixin, BaseModel):
class ContextMixin:
"""Mixin class for configurable objects"""
"""Mixin class for configurable objects: Priority: more specific < parent"""
_context: Optional[Context] = None

View file

@ -33,7 +33,7 @@ class Architect(Role):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
# Initialize actions specific to the Architect role
self._init_actions([WriteDesign])
self.add_actions([WriteDesign])
# Set events or actions the Architect should watch or be aware of
self._watch({WritePRD})

View file

@ -84,7 +84,7 @@ class Engineer(Role):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._init_actions([WriteCode])
self.add_actions([WriteCode])
self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug])
self.code_todos = []
self.summarize_todos = []

View file

@ -60,7 +60,7 @@ class InvoiceOCRAssistant(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([InvoiceOCR])
self.add_actions([InvoiceOCR])
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)
async def _act(self) -> Message:
@ -82,10 +82,10 @@ class InvoiceOCRAssistant(Role):
resp = await todo.run(file_path)
if len(resp) == 1:
# Single file support for questioning based on OCR recognition results
self._init_actions([GenerateTable, ReplyQuestion])
self.add_actions([GenerateTable, ReplyQuestion])
self.orc_data = resp[0]
else:
self._init_actions([GenerateTable])
self.add_actions([GenerateTable])
self.set_todo(None)
content = INVOICE_OCR_SUCCESS

View file

@ -33,7 +33,7 @@ class ProductManager(Role):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._init_actions([PrepareDocuments, WritePRD])
self.add_actions([PrepareDocuments, WritePRD])
self._watch([UserRequirement, PrepareDocuments])
self.todo_action = any_to_name(PrepareDocuments)

View file

@ -33,5 +33,5 @@ class ProjectManager(Role):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._init_actions([WriteTasks])
self.add_actions([WriteTasks])
self._watch([WriteDesign])

View file

@ -44,7 +44,7 @@ class QaEngineer(Role):
# FIXME: a bit hack here, only init one action to circumvent _think() logic,
# will overwrite _think() in future updates
self._init_actions([WriteTest])
self.add_actions([WriteTest])
self._watch([SummarizeCode, WriteTest, RunCode, DebugError])
self.test_round = 0

View file

@ -34,7 +34,7 @@ class Researcher(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions(
self.add_actions(
[CollectLinks(name=self.name), WebBrowseAndSummarize(name=self.name), ConductResearch(name=self.name)]
)
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)

View file

@ -23,7 +23,7 @@
from __future__ import annotations
from enum import Enum
from typing import Any, Iterable, Optional, Set, Type
from typing import Any, Iterable, Optional, Set, Type, Union
from pydantic import BaseModel, ConfigDict, Field, SerializeAsAny, model_validator
@ -222,20 +222,18 @@ class Role(SerializationMixin):
def _init_action_system_message(self, action: Action):
action.set_prefix(self._get_prefix())
def refresh_system_message(self):
self.llm.system_prompt = self._get_prefix()
def add_action(self, action: Action):
"""Add action to the role."""
self.add_actions([action])
def set_memory(self, memory: Memory):
self.rc.memory = memory
def add_actions(self, actions: list[Union[Action, Type[Action]]]):
"""Add actions to the role.
def init_actions(self, actions):
self._init_actions(actions)
def _init_actions(self, actions):
self._reset()
for idx, action in enumerate(actions):
Args:
actions: list of Action classes or instances
"""
for action in actions:
if not isinstance(action, Action):
## 默认初始化
i = action(name="", llm=self.llm)
else:
if self.is_human and not isinstance(action.llm, HumanProvider):
@ -247,7 +245,7 @@ class Role(SerializationMixin):
i = action
self._init_action_system_message(i)
self.actions.append(i)
self.states.append(f"{idx}. {action}")
self.states.append(f"{len(self.actions)}. {action}")
def _set_react_mode(self, react_mode: str, max_react_loop: int = 1):
"""Set strategy of the Role reacting to observed Message. Variation lies in how
@ -302,7 +300,7 @@ class Role(SerializationMixin):
self.rc.env = env
if env:
env.set_addresses(self, self.addresses)
self.refresh_system_message() # add env message to system message
self.llm.system_prompt = self._get_prefix()
@property
def action_count(self):

View file

@ -38,5 +38,5 @@ class Sales(Role):
action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.asearch)
else:
action = SearchAndSummarize()
self._init_actions([action])
self.add_actions([action])
self._watch([UserRequirement])

View file

@ -48,12 +48,12 @@ class Searcher(Role):
engine (SearchEngineType): The type of search engine to use.
"""
super().__init__(**kwargs)
self._init_actions([SearchAndSummarize(engine=self.engine)])
self.add_actions([SearchAndSummarize(engine=self.engine)])
def set_search_func(self, search_func):
"""Sets a custom search function for the searcher."""
action = SearchAndSummarize(name="", engine=SearchEngineType.CUSTOM_ENGINE, search_func=search_func)
self._init_actions([action])
self.add_actions([action])
async def _act_sp(self) -> Message:
"""Performs the search action in a single process."""

View file

@ -52,7 +52,7 @@ class SkAgent(Role):
def __init__(self, **data: Any) -> None:
"""Initializes the Engineer role with given attributes."""
super().__init__(**data)
self._init_actions([ExecuteTask()])
self.add_actions([ExecuteTask()])
self._watch([UserRequirement])
self.kernel = make_sk_kernel()

View file

@ -47,7 +47,7 @@ class Teacher(Role):
for topic in TeachingPlanBlock.TOPICS:
act = WriteTeachingPlanPart(context=self.rc.news[0].content, topic=topic, llm=self.llm)
actions.append(act)
self._init_actions(actions)
self.add_actions(actions)
if self.rc.todo is None:
self._set_state(0)

View file

@ -40,7 +40,7 @@ class TutorialAssistant(Role):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._init_actions([WriteDirectory(language=self.language)])
self.add_actions([WriteDirectory(language=self.language)])
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)
async def _handle_directory(self, titles: Dict) -> Message:
@ -63,7 +63,7 @@ class TutorialAssistant(Role):
directory += f"- {key}\n"
for second_dir in first_dir[key]:
directory += f" - {second_dir}\n"
self._init_actions(actions)
self.add_actions(actions)
async def _act(self) -> Message:
"""Perform an action as determined by the role.

View file

@ -67,7 +67,7 @@ class RoleA(Role):
def __init__(self, **kwargs):
super(RoleA, self).__init__(**kwargs)
self._init_actions([ActionPass])
self.add_actions([ActionPass])
self._watch([UserRequirement])
@ -79,7 +79,7 @@ class RoleB(Role):
def __init__(self, **kwargs):
super(RoleB, self).__init__(**kwargs)
self._init_actions([ActionOK, ActionRaise])
self.add_actions([ActionOK, ActionRaise])
self._watch([ActionPass])
self.rc.react_mode = RoleReactMode.BY_ORDER
@ -92,7 +92,7 @@ class RoleC(Role):
def __init__(self, **kwargs):
super(RoleC, self).__init__(**kwargs)
self._init_actions([ActionOK, ActionRaise])
self.add_actions([ActionOK, ActionRaise])
self._watch([UserRequirement])
self.rc.react_mode = RoleReactMode.BY_ORDER
self.rc.memory.ignore_id = True

View file

@ -33,7 +33,7 @@ class MockAction(Action):
class MockRole(Role):
def __init__(self, name="", profile="", goal="", constraints="", desc=""):
super().__init__(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc)
self._init_actions([MockAction()])
self.add_actions([MockAction()])
def test_basic():
@ -111,7 +111,7 @@ async def test_send_to():
def test_init_action():
role = Role()
role.init_actions([MockAction, MockAction])
role.add_actions([MockAction, MockAction])
assert role.action_count == 2
@ -127,7 +127,7 @@ async def test_recover():
role.publish_message(None)
role.llm = mock_llm
role.init_actions([MockAction, MockAction])
role.add_actions([MockAction, MockAction])
role.recovered = True
role.latest_observed_msg = Message(content="recover_test")
role.rc.state = 0
@ -144,7 +144,7 @@ async def test_think_act():
mock_llm.aask.side_effect = ["ok"]
role = Role()
role.init_actions([MockAction])
role.add_actions([MockAction])
await role.think()
role.rc.memory.add(Message("run"))
assert len(role.get_memories()) == 1