Implementing Multi-Agent Simulations with Petting Zoo in LangChain

Multi-Agent Simulation Environments with Petting Zoo

This example demonstrates how to define and run multi-agent simulations using the Petting Zoo library, which serves as the multi-agent counterpart to Gymnasium. The implementation follows a similar pattern to single-agent environments but extends it to handle multiple interacting agents.

Required Dependencies

!pip install pettingzoo pygame rlcard

Core Module Imports

from collections import defaultdict
import inspect
import tenacity
from langchain.output_parsers import RegexParser
from langchain.schema import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI

Agent Implementation

The following agent class maintains the same core functionality as the Gymnasium example, with added resilience through random action fallback:

class SimulationAgent:
    @classmethod
    def get_environment_docs(cls, env):
        return env.unwrapped.__doc__

    def __init__(self, llm_model, environment):
        self.model = llm_model
        self.env = environment
        self.documentation = self.get_environment_docs(environment)
        
        self.agent_prompt = """
Your objective is to maximize cumulative reward.
You'll receive observations in this format:

Observation: <state>
Reward: <value>
Termination: <bool>
Truncation: <bool>
Total Return: <sum>

Respond exclusively with:

Action: <choice>
"""
        
        self.response_parser = RegexParser(
            pattern=r"Action: (.*)",
            output_keys=["action"],
            default_key="action"
        )
        
        self.conversation_history = []
        self.cumulative_reward = 0

    def select_random_action(self):
        return self.env.action_space.sample()

    def initialize(self):
        self.conversation_history = [
            SystemMessage(content=self.documentation),
            SystemMessage(content=self.agent_prompt)
        ]

Tags: LangChain PettingZoo Multi-Agent Systems reinforcement learning python

Posted on Sun, 11 Oct 2026 16:16:48 +0000 by PigsHidePies