Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to serialize message objects in AgentScope and remove unused arguments. #388

Merged
merged 10 commits into from
Aug 27, 2024
1 change: 0 additions & 1 deletion docs/sphinx_doc/en/source/tutorial/201-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class AgentBase(Operator):
sys_prompt: Optional[str] = None,
model_config_name: str = None,
use_memory: bool = True,
memory_config: Optional[dict] = None,
) -> None:

# ... [code omitted for brevity]
Expand Down
1 change: 0 additions & 1 deletion docs/sphinx_doc/zh_CN/source/tutorial/201-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class AgentBase(Operator):
sys_prompt: Optional[str] = None,
model_config_name: str = None,
use_memory: bool = True,
memory_config: Optional[dict] = None,
) -> None:

# ... [code omitted for brevity]
Expand Down
4 changes: 0 additions & 4 deletions examples/conversation_mixture_of_agents/conversation_moa.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(
name: str,
moa_module: MixtureOfAgents, # changed to passing moa_module here
use_memory: bool = True,
memory_config: Optional[dict] = None,
) -> None:
"""Initialize the dialog agent.

Expand All @@ -35,14 +34,11 @@ def __init__(
The inited MoA module you want to use as the main module.
use_memory (`bool`, defaults to `True`):
Whether the agent has memory.
memory_config (`Optional[dict]`):
The config of memory.
"""
super().__init__(
name=name,
sys_prompt="",
use_memory=use_memory,
memory_config=memory_config,
)
self.moa_module = moa_module # change model init to moa_module

Expand Down
5 changes: 3 additions & 2 deletions examples/distributed_parallel_optimization/answerer_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
return Msg(
self.name,
content=f"Unable to load web page [{x.url}].",
role="assistant",
url=x.url,
)
# prepare prompt
Expand All @@ -49,12 +50,12 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
" the following web page:\n\n"
f"{response['html_to_text']}"
f"\n\nBased on the above web page,"
f" please answer my question\n{x.query}",
f" please answer my question\n{x.metadata}",
),
)
# call llm and generate response
response = self.model(prompt).text
msg = Msg(self.name, content=response, url=x.url)
msg = Msg(self.name, content=response, role="assistant", url=x.url)

self.speak(msg)

Expand Down
4 changes: 3 additions & 1 deletion examples/distributed_parallel_optimization/searcher_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
Msg(
name=self.name,
content=result,
role="assistant",
url=result["link"],
query=x.content,
metadata=x.content,
)
for result in results
],
role="assistant",
)
self.speak(
Msg(
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_simulation/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
"""Generate a random value"""
# generate a response in content
response = self.generate_random_response()
msg = Msg(self.name, content=response)
msg = Msg(self.name, content=response, role="assistant")
return msg


Expand Down
26 changes: 2 additions & 24 deletions src/agentscope/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def __init__(
sys_prompt: Optional[str] = None,
model_config_name: str = None,
use_memory: bool = True,
memory_config: Optional[dict] = None,
to_dist: Optional[Union[DistConf, bool]] = False,
) -> None:
r"""Initialize an agent from the given arguments.
Expand All @@ -160,8 +159,6 @@ def __init__(
configuration.
use_memory (`bool`, defaults to `True`):
Whether the agent has memory.
memory_config (`Optional[dict]`):
The config of memory.
to_dist (`Optional[Union[DistConf, bool]]`, default to `False`):
The configurations passed to :py:meth:`to_dist` method. Used in
:py:class:`_AgentMeta`, when this parameter is provided,
Expand Down Expand Up @@ -189,7 +186,6 @@ def __init__(
See :doc:`Tutorial<tutorial/208-distribute>` for detail.
"""
self.name = name
self.memory_config = memory_config
self.sys_prompt = sys_prompt

# TODO: support to receive a ModelWrapper instance
Expand All @@ -200,7 +196,7 @@ def __init__(
)

if use_memory:
self.memory = TemporaryMemory(memory_config)
self.memory = TemporaryMemory()
else:
self.memory = None

Expand Down Expand Up @@ -276,25 +272,7 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
f'"reply" function.',
)

def load_from_config(self, config: dict) -> None:
"""Load configuration for this agent.

Args:
config (`dict`): model configuration
"""

def export_config(self) -> dict:
"""Return configuration of this agent.

Returns:
The configuration of current agent.
"""
return {}

def load_memory(self, memory: Sequence[dict]) -> None:
r"""Load input memory."""

def __call__(self, *args: Any, **kwargs: Any) -> dict:
def __call__(self, *args: Any, **kwargs: Any) -> Msg:
"""Calling the reply function, and broadcast the generated
response to all audiences if needed."""
res = self.reply(*args, **kwargs)
Expand Down
4 changes: 0 additions & 4 deletions src/agentscope/agents/dialog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def __init__(
sys_prompt: str,
model_config_name: str,
use_memory: bool = True,
memory_config: Optional[dict] = None,
DavdGao marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""Initialize the dialog agent.

Expand All @@ -31,15 +30,12 @@ def __init__(
configuration.
use_memory (`bool`, defaults to `True`):
Whether the agent has memory.
memory_config (`Optional[dict]`):
The config of memory.
"""
super().__init__(
name=name,
sys_prompt=sys_prompt,
model_config_name=model_config_name,
use_memory=use_memory,
memory_config=memory_config,
)

def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
Expand Down
4 changes: 0 additions & 4 deletions src/agentscope/agents/dict_dialog_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def __init__(
sys_prompt: str,
model_config_name: str,
use_memory: bool = True,
memory_config: Optional[dict] = None,
max_retries: Optional[int] = 3,
) -> None:
"""Initialize the dict dialog agent.
Expand All @@ -39,8 +38,6 @@ def __init__(
configuration.
use_memory (`bool`, defaults to `True`):
Whether the agent has memory.
memory_config (`Optional[dict]`, defaults to `None`):
The config of memory.
max_retries (`Optional[int]`, defaults to `None`):
The maximum number of retries when failed to parse the model
output.
Expand All @@ -50,7 +47,6 @@ def __init__(
sys_prompt=sys_prompt,
model_config_name=model_config_name,
use_memory=use_memory,
memory_config=memory_config,
)

self.parser = None
Expand Down
2 changes: 1 addition & 1 deletion src/agentscope/agents/rag_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:

# call llm and generate response
response = self.model(prompt).text
msg = Msg(self.name, response)
msg = Msg(self.name, response, "assistant")

# Print/speak the message in this agent's voice
self.speak(msg)
Expand Down
12 changes: 4 additions & 8 deletions src/agentscope/agents/rpc_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
from typing import Type, Optional, Union, Sequence

from agentscope.agents.agent import AgentBase
from agentscope.message import (
PlaceholderMessage,
serialize,
Msg,
)
from agentscope.message import Msg
from agentscope.message import PlaceholderMessage
from agentscope.rpc import RpcAgentClient
from agentscope.serialize import serialize
from agentscope.server.launcher import RpcAgentServerLauncher
from agentscope.studio._client import _studio_client

Expand Down Expand Up @@ -122,8 +120,6 @@ def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
if self.client is None:
self._launch_server()
return PlaceholderMessage(
name=self.name,
content=None,
client=self.client,
x=x,
)
Expand All @@ -133,7 +129,7 @@ def observe(self, x: Union[Msg, Sequence[Msg]]) -> None:
self._launch_server()
self.client.call_agent_func(
func_name="_observe",
value=serialize(x), # type: ignore[arg-type]
value=serialize(x),
)

def clone_instances(
Expand Down
4 changes: 0 additions & 4 deletions src/agentscope/agents/text_to_image_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(
name: str,
model_config_name: str,
use_memory: bool = True,
memory_config: Optional[dict] = None,
) -> None:
"""Initialize the text to image agent.

Expand All @@ -33,15 +32,12 @@ def __init__(
configuration.
use_memory (`bool`, defaults to `True`):
Whether the agent has memory.
memory_config (`Optional[dict]`):
The config of memory.
"""
super().__init__(
name=name,
sys_prompt="",
model_config_name=model_config_name,
use_memory=use_memory,
memory_config=memory_config,
)

logger.warning(
Expand Down
4 changes: 2 additions & 2 deletions src/agentscope/logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Logging utilities."""
import json
import os
import sys
from typing import Optional, Literal, Any
Expand All @@ -9,6 +8,7 @@

from .utils.tools import _guess_type_by_extension
from .message import Msg
from .serialize import serialize
from .studio._client import _studio_client
from .web.gradio.utils import (
generate_image_from_name,
Expand Down Expand Up @@ -96,7 +96,7 @@ def _save_msg(msg: Msg) -> None:

logger.log(
LEVEL_SAVE_MSG,
json.dumps(msg, ensure_ascii=False, default=lambda _: None),
serialize(msg),
)


Expand Down
20 changes: 0 additions & 20 deletions src/agentscope/memory/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ class MemoryBase(ABC):

_version: int = 1

def __init__(
self,
config: Optional[dict] = None,
) -> None:
"""MemoryBase is a base class for memory of agents.

Args:
config (`Optional[dict]`, defaults to `None`):
Configuration of this memory.
"""
self.config = {} if config is None else config

def update_config(self, config: dict) -> None:
"""
Configure memory as specified in config
Args:
config (`dict`): Configuration of resetting this memory
"""
self.config = config

@abstractmethod
def get_memory(
self,
Expand Down
Loading