Skip to content

Commit

Permalink
fix in sem
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyushuo committed Sep 2, 2024
1 parent 80036df commit 9f481dd
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 223 deletions.
5 changes: 3 additions & 2 deletions examples/distributed_simulation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def run_main_process(
agent_type: str = "random",
max_value: int = 100,
sleep_time: float = 1.0,
cpp_server: bool = False,
) -> None:
"""Run main process"""
agentscope.init(
Expand All @@ -127,7 +128,7 @@ def run_main_process(
port_id = idx % server_per_host
model_id = i % model_per_host
host = hosts[host_id]
port = base_port + port_id
port = base_port + port_id if not cpp_server else base_port
config_name = f"model_{model_id + 1}"
if agent_type == "random":
configs.append(
Expand Down Expand Up @@ -166,7 +167,7 @@ def run_main_process(
* participant_per_moderator
],
host=hosts[i // moderator_per_host],
port=base_port + server_per_host + i % moderator_per_host,
port=base_port + server_per_host + i % moderator_per_host if not cpp_server else base_port,
agent_type=agent_type,
max_value=max_value,
sleep_time=sleep_time,
Expand Down
51 changes: 25 additions & 26 deletions examples/distributed_simulation/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import re
from typing import Optional, Union, Sequence
import concurrent.futures

from loguru import logger

Expand Down Expand Up @@ -111,46 +112,44 @@ def __init__(
) -> None:
super().__init__(name)
self.max_value = max_value
if agent_type == "llm":
self.participants = [
LLMParticipant(
def create_llm_participant(config):
return LLMParticipant(
name=config["name"],
model_config_name=config["model_config_name"],
max_value=max_value,
).to_dist(
host=config["host"],
port=config["port"],
)
for config in part_configs
]
else:
self.participants = [
RandomParticipant(
name=config["name"],
max_value=max_value,
sleep_time=sleep_time,
).to_dist(
host=config["host"],
port=config["port"],
)
for config in part_configs
]

def create_random_participant(config):
return RandomParticipant(
name=config["name"],
max_value=max_value,
sleep_time=sleep_time,
).to_dist(
host=config["host"],
port=config["port"],
)
create_participant = {"random": create_random_participant, "llm": create_llm_participant}[agent_type]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(create_participant, config) for config in part_configs}

self.participants = []
for future in concurrent.futures.as_completed(futures):
result = future.result()
self.participants.append(result)

def reply(self, x: Optional[Union[Msg, Sequence[Msg]]] = None) -> Msg:
results = []
msg = Msg(
name="moderator",
role="user",
content=f"Now give a number between 0 and {self.max_value}.",
)
for p in self.participants:
results.append(p(msg))
summ = 0
for r in results:
try:
summ += int(r["content"])
except Exception as e:
print(e)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(lambda p: p(msg), p) for p in self.participants}
futures_2 = {executor.submit(lambda r: int(r["content"]), future.result()) for future in concurrent.futures.as_completed(futures)}
summ = sum(future.result() for future in concurrent.futures.as_completed(futures_2))
return Msg(
name=self.name,
role="assistant",
Expand Down
Loading

0 comments on commit 9f481dd

Please sign in to comment.