modelscope / agentscope

Start building LLM-empowered multi-agent applications in an easier way.
https://doc.agentscope.io/
Apache License 2.0
5.19k stars 316 forks source link

Add environment modules #378

Closed pan-x-c closed 1 week ago

pan-x-c commented 3 months ago

Description

Add Environment Modules (Env and its sub-classes)

Env is a key concept of AgentScope, representing global data shared among agents.

Each env has name and value, and multiple envs can be organized into a tree structure, where each env can have multiple children envs and one parent env.

Different implementations of envs may have different event functions, which are marked by @event_func. Users can bind EventListener to specific event functions, and the listener will be activated when the event function is called.

Similar to AgentBase sub-classes, Env sub-classes also support the to_dist call, and the parameter is the same as AgentBase.

Decouple distribution-related functions from other AgentScope modules

Move to_dist into RpcMeta

RpcMeta is a meta-class that integrates all to_dist related methods/parameters. Now, to_dist function/parameter can be used directly if the class is a subclass of RpcMeta. The original to_dist and _AgentMeta is removed.

Extend RpcAgent into RpcObject

RpcObject is an enhanced version of RpcAgent, and can be used to represent any object (unlike RpcAgent which can only represent Agent) Users can call any public methods or get any attributes on the RpcObject, and the RpcObject will forward the request to the real object running in the RPC server. The original RpcAgent is removed.

Add @async_func and AsyncResult to replace the reply and Placeholder

Because the RpcObject supports calling any public methods and returns any results, the old Placeholder cannot cover this complex scenario. Therefore, @async_func and AsyncResult are added.

Functions decorated with @async_func will automatically return AsyncResult objects in distributed mode. The usage of AsyncResult is the same as Placeholder, which is instantiated only when its internal objects are accessed. AsyncResult can be used to represent any type of data, not just Msg. The original PlaceholderMessage is removed.

Through the above modification, the agent and message modules are decoupled from the distributed mode. Note that the user interface of the distributed mode is unchanged, all modifications above are completely hidden from the user, and all previous distributed examples can be run without any modification.

Add Guess Two Third of the Average Game example

The code of "Very Large-Scale Multi-Agent Simulation in AgentScope" is released under examples/paper_large_scale_simulation

Checklist

Please check the following items before code is ready to be reviewed.

pan-x-c commented 2 months ago

Because the gRPC server relies on the RpcEnv,rpc_env.py and the modules it depends on can't be moved to examples, all other modules have been moved to examples/environments

DavdGao commented 2 months ago

Because the gRPC server relies on the RpcEnv,rpc_env.py and the modules it depends on can't be moved to examples, all other modules have been moved to examples/environments

The current implementation is too complex, and we didn't decide on the final implementation of this environment module.

However, in this PR, the env module is actually embedded in the main library. If we can decouple it from the main library, we should consider to create a branch to maintain the simulation related code.

pan-x-c commented 2 weeks ago

To be discussed

  1. the name of distributed functions. In the current version, async_func/sync_func may be confused with async in Python asyncio. unblocking_func / blocking_func may be a better name.
  2. the old agent_id is renamed to oid (a short version of object_id), which may cause misunderstandings and incompatibility with legacy code. However, here we really need an id field for all objects, not just agents.