Skip to content

LangGraph-inspired Orchestration LLM Building Block for Mesa prototype #2746

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
26 changes: 26 additions & 0 deletions mesa/experimental/llm_layer/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from mesa import Agent

Check warning on line 1 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L1

Added line #L1 was not covered by tests


class BasicAgent(Agent):
def __init__(self, model):

Check warning on line 5 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L4-L5

Added lines #L4 - L5 were not covered by tests
# Pass the parameters to the parent class.
super().__init__(model)

Check warning on line 7 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L7

Added line #L7 was not covered by tests

def step(self):

Check warning on line 9 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L9

Added line #L9 was not covered by tests
if self.random.random() > 0.5:
print(f"[Basic {self.unique_id}] Collecting nearby resource.")

Check warning on line 11 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L11

Added line #L11 was not covered by tests
else:
print(f"[Basic {self.unique_id}] Waiting...")

Check warning on line 13 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L13

Added line #L13 was not covered by tests


class CognitiveAgent(Agent):
def __init__(self, model, orchestrator):
super().__init__(model)
self.orchestrator = orchestrator
self.memory = []

Check warning on line 20 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L16-L20

Added lines #L16 - L20 were not covered by tests

def step(self):
context = {"goal": "collect", "memory": self.memory}
action = self.orchestrator.execute_graph("plan", self, context)
self.memory.append(action)
print(f"[Cognitive {self.unique_id}] {action}")

Check warning on line 26 in mesa/experimental/llm_layer/agents.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/agents.py#L22-L26

Added lines #L22 - L26 were not covered by tests
25 changes: 25 additions & 0 deletions mesa/experimental/llm_layer/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from agents import BasicAgent, CognitiveAgent

Check warning on line 1 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L1

Added line #L1 was not covered by tests

from mesa import Model

Check warning on line 3 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L3

Added line #L3 was not covered by tests


class HybridModel(Model):
def __init__(self, num_agents, orchestrator):
super().__init__()
self.num_agents = num_agents
self.orchestrator = orchestrator

Check warning on line 10 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L6-L10

Added lines #L6 - L10 were not covered by tests

# Create 3 basic agents and 2 cognitive agents
BasicAgent.create_agents(model=self, n=3)
CognitiveAgent.create_agents(model=self, n=2, orchestrator=orchestrator)

Check warning on line 14 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L13-L14

Added lines #L13 - L14 were not covered by tests

def step(self):
self.agents.shuffle_do("step")

Check warning on line 17 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L16-L17

Added lines #L16 - L17 were not covered by tests


def llm_collect(agent, state):
return f"[LLM] Reasoned to collect based on memory length {len(state['memory'])}"

Check warning on line 21 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L20-L21

Added lines #L20 - L21 were not covered by tests


def wait_tool(agent, state):
return "[RULE] Wait due to uncertainty or cooldown"

Check warning on line 25 in mesa/experimental/llm_layer/models.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/models.py#L24-L25

Added lines #L24 - L25 were not covered by tests
35 changes: 35 additions & 0 deletions mesa/experimental/llm_layer/orchestrator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# LangGraph-inspired orchestrator (future-proof structure)
class Orchestrator:
def __init__(self):
self.nodes = {}
self.edges = {}

Check warning on line 5 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L2-L5

Added lines #L2 - L5 were not covered by tests

def add_node(self, name, func):
self.nodes[name] = func

Check warning on line 8 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L7-L8

Added lines #L7 - L8 were not covered by tests

def add_edge(self, from_node, to_node):
self.edges.setdefault(from_node, []).append(to_node)

Check warning on line 11 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L10-L11

Added lines #L10 - L11 were not covered by tests

def add_conditional_edges(self, from_node, condition_fn):
self.edges[from_node] = [

Check warning on line 14 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L13-L14

Added lines #L13 - L14 were not covered by tests
(condition_fn, target) for target in self.nodes if target != from_node
]

def execute_graph(self, start_node, agent, state):
current_node = start_node

Check warning on line 19 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L18-L19

Added lines #L18 - L19 were not covered by tests
while current_node:
result = self.nodes[current_node](agent, state)
state["last_output"] = result
next_node = self._resolve_next_node(current_node, state)

Check warning on line 23 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L21-L23

Added lines #L21 - L23 were not covered by tests
if not next_node:
break
current_node = next_node
return result

Check warning on line 27 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L25-L27

Added lines #L25 - L27 were not covered by tests

def _resolve_next_node(self, current_node, state):

Check warning on line 29 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L29

Added line #L29 was not covered by tests
if current_node not in self.edges:
return None

Check warning on line 31 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L31

Added line #L31 was not covered by tests
for condition_fn, target in self.edges[current_node]:
if condition_fn(state):
return target
return None

Check warning on line 35 in mesa/experimental/llm_layer/orchestrator.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/orchestrator.py#L34-L35

Added lines #L34 - L35 were not covered by tests
18 changes: 18 additions & 0 deletions mesa/experimental/llm_layer/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from models import HybridModel, llm_collect, wait_tool
from orchestrator import Orchestrator

Check warning on line 2 in mesa/experimental/llm_layer/run.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/run.py#L1-L2

Added lines #L1 - L2 were not covered by tests

# Set up the orchestrator and graph
orchestrator = Orchestrator()
orchestrator.add_node("plan", llm_collect)
orchestrator.add_node("wait", wait_tool)

Check warning on line 7 in mesa/experimental/llm_layer/run.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/run.py#L5-L7

Added lines #L5 - L7 were not covered by tests

# Add conditional edge: alternate between collecting and waiting
orchestrator.add_conditional_edges(

Check warning on line 10 in mesa/experimental/llm_layer/run.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/run.py#L10

Added line #L10 was not covered by tests
"plan", lambda state: "wait" if len(state["memory"]) % 2 == 0 else None
)

# Run the model
model = HybridModel(num_agents=5, orchestrator=orchestrator)

Check warning on line 15 in mesa/experimental/llm_layer/run.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/run.py#L15

Added line #L15 was not covered by tests
for step in range(3):
print(f"\n--- Step {step + 1} ---")
model.step()

Check warning on line 18 in mesa/experimental/llm_layer/run.py

View check run for this annotation

Codecov / codecov/patch

mesa/experimental/llm_layer/run.py#L17-L18

Added lines #L17 - L18 were not covered by tests
Loading