Skip to content

Commit

Permalink
refactor placeholder message
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Jan 23, 2024
1 parent c4a86dc commit 4d6e036
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
8 changes: 4 additions & 4 deletions examples/distributed/distributed_debate.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ def run_main_process(parsed_args: argparse.Namespace) -> None:
with msghub(participants=participants, announcement=hint):
for _ in range(3):
pro_resp = pro_agent(x)
logger.chat(pro_resp.update_value())
logger.chat(pro_resp)
con_resp = con_agent(pro_resp)
logger.chat(con_resp.update_value())
logger.chat(con_resp)
x = judge_agent(con_resp)
logger.chat(x.update_value())
logger.chat(x)
x = judge_agent(x)
logger.chat(x.update_value())
logger.chat(x)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions examples/distributed/distributed_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def parse_args() -> argparse.Namespace:
def setup_assistant_server(assistant_host: str, assistant_port: int) -> None:
"""Set up assistant rpc server"""
agentscope.init(
model_configs="configs/model_configs.json",
model_configs="configs/model_configs_pxc.json",
)
assistant_server_launcher = RpcAgentServerLauncher(
name="Assitant",
Expand All @@ -54,7 +54,7 @@ def setup_assistant_server(assistant_host: str, assistant_port: int) -> None:
def run_main_process(assistant_host: str, assistant_port: int) -> None:
"""Run dialog main process"""
agentscope.init(
model_configs="configs/model_configs.json",
model_configs="configs/model_configs_pxc.json",
)
assistant_agent = RpcDialogAgent(
name="Assistant",
Expand All @@ -73,7 +73,7 @@ def run_main_process(assistant_host: str, assistant_port: int) -> None:
msg = user_agent()
while not msg.content.endswith("exit"):
msg = assistant_agent(msg)
logger.chat(msg.update_value())
logger.chat(msg)
time.sleep(0.5)
msg = user_agent(msg)

Expand Down
10 changes: 5 additions & 5 deletions notebook/distributed_debate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@
"with msghub(participants=participants, announcement=hint):\n",
" for _ in range(3):\n",
" pro_resp = pro_agent(x)\n",
" logger.chat(pro_resp.update_value())\n",
" logger.chat(pro_resp)\n",
" con_resp = con_agent(pro_resp)\n",
" logger.chat(con_resp.update_value())\n",
" logger.chat(con_resp)\n",
" x = judge_agent(con_resp)\n",
" logger.chat(x.update_value())\n",
" logger.chat(x)\n",
" x = judge_agent(x)\n",
" logger.chat(x.update_value())\n"
" logger.chat(x)\n"
]
},
{
Expand Down Expand Up @@ -187,7 +187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
"version": "3.1.0"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion notebook/distributed_dialog.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"msg = user_agent()\n",
"while not msg.content.endswith(\"exit\"):\n",
" msg = assistant_agent(msg)\n",
" logger.chat(msg.update_value())\n",
" logger.chat(msg)\n",
" time.sleep(0.5)\n",
" msg = user_agent(msg)"
]
Expand Down
32 changes: 20 additions & 12 deletions src/agentscope/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,16 @@ def __init__(
timestamp=timestamp,
**kwargs,
)
# placeholder indicates whether the real message is still in rpc server
self._is_placeholder = True
self._host = host
self._port = port

self._client = RpcAgentClient(host, port)
self._task_id = task_id
# placeholder indicates whether the real message is still in rpc server
self._is_placeholder = True

def __is_local(self, key: Any) -> bool:
return key in PlaceholderMessage.LOCAL_ATTRS \
or not self._is_placeholder

def __getattr__(self, __name: str) -> Any:
"""Get attribute value from PlaceholderMessage. Get value from rpc
Expand All @@ -251,20 +254,25 @@ def __getattr__(self, __name: str) -> Any:
Args:
__name (`str`):
Attribute name.
"""
if (
__name not in PlaceholderMessage.LOCAL_ATTRS
and self._is_placeholder
):
if not self.__is_local(__name):
self.update_value()
return MessageBase.__getattr__(self, __name)

def __getitem__(self, __key: Any) -> Any:
"""Get item value from PlaceholderMessage. Get value from rpc
agent server if necessary.
Args:
__key (`Any`):
Item name.
"""
if not self.__is_local(__key):
self.update_value()
return MessageBase.__getitem__(self, __key)

def to_str(self) -> str:
if self._is_placeholder:
return f"{self.name}: [message from {self._host}:{self._port}]"
else:
return f"{self.name}: {self.content}"
return f"{self.name}: {self.content}"

def update_value(self) -> MessageBase:
"""Get attribute values from rpc agent server immediately"""
Expand Down
13 changes: 13 additions & 0 deletions tests/rpc_agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ def test_single_rpc_agent_server(self) -> None:
result = agent_a(msg)
# get name without waiting for the server
self.assertEqual(result.name, "a")
self.assertEqual(result["name"], "a")
js_placeholder_result = result.serialize()
self.assertTrue(result._is_placeholder) # pylint: disable=W0212
placeholder_result = deserialize(js_placeholder_result)
self.assertTrue(isinstance(placeholder_result, PlaceholderMessage))
self.assertEqual(placeholder_result.name, "a")
self.assertEqual(placeholder_result["name"], "a")
self.assertTrue(
placeholder_result._is_placeholder, # pylint: disable=W0212
)
Expand Down Expand Up @@ -131,6 +133,17 @@ def test_connect_to_an_existing_rpc_server(self) -> None:
self.assertEqual(result.name, "a")
# waiting for server
self.assertEqual(result.content, msg.content)
# test dict usage
msg = Msg(name="System", content={"text": "hi world"})
result = agent_a(msg)
# get name without waiting for the server
self.assertEqual(result["name"], "a")
# waiting for server
self.assertEqual(result["content"], msg.content)
# test to_str
msg = Msg(name="System", content={"text": "test"})
result = agent_a(msg)
self.assertEqual(result.to_str(), "a: {'text': 'test'}")
launcher.shutdown()

def test_multi_rpc_agent(self) -> None:
Expand Down

0 comments on commit 4d6e036

Please sign in to comment.