For complex behaviors, chain components so that one component's output serves as context for another. This allows for 'Chain of Thought' reasoning.
Key reasoning components include:
SituationRepresentation: Summarizes the current situation using recent observations and relevant memories.QuestionOfRecentMemories: Asks the model a specific question (e.g., an 'Internal Monologue') based on the current context.
When assembling the components dictionary, ensure the component_order in ConcatActComponent matches the logical dependency (e.g., situation_representation must appear before the component that depends on it).
from concordia.contrib.components.agent import situation_representation_via_narrative
from concordia.components import agent as agent_components
@dataclasses.dataclass
class ReflectiveAgent(prefab_lib.Prefab):
def build(self, model, memory_bank):
name = self.params.get("name", "Agent")
# 1. Base Components
instructions = agent_components.instructions.Instructions(
agent_name=name)
observation = agent_components.observation.LastNObservations(
history_length=100)
memory = agent_components.memory.AssociativeMemory(
memory_bank=memory_bank)
# 2. Advanced Components (Chain of Thought)
# Step A: Summarize the situation
situation = situation_representation_via_narrative.SituationRepresentation(
model=model,
observation_component_key=agent_components.observation.DEFAULT_OBSERVATION_COMPONENT_KEY,
declare_entity_as_protagonist=True,
)
# Step B: Apply a Guiding Principle (uses Step A context)
principle = agent_components.question_of_recent_memories.QuestionOfRecentMemories(
model=model,
pre_act_label=f"{name}'s Internal Monologue",
question=f"How can {name} best achieve their goals in this situation?",
answer_prefix=f"{name} thinks: ",
add_to_memory=False, # Don't clutter memory with every thought
components=[
"Instructions",
"situation_representation" # <--- Depends on Step A
],
)
# 3. Assemble Components (Order matters!)
components = {
"Instructions": instructions,
agent_components.memory.DEFAULT_MEMORY_COMPONENT_KEY: memory,
agent_components.observation.DEFAULT_OBSERVATION_COMPONENT_KEY: observation,
"situation_representation": situation,
"guiding_principle": principle,
}
# The Act Component sees everything in 'components'
act_component = agent_components.concat_act_component.ConcatActComponent(
model=model,
component_order=list(components.keys()),
)
return entity_agent_with_logging.EntityAgentWithLogging(
agent_name=name,
act_component=act_component,
context_components=components,
)