Skip to content

Commit e4ea0aa

Browse files
committed
integrated memory in llm_agent
1 parent 5de6f1b commit e4ea0aa

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

mesa_llm/llm_agent.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
from mesa_llm.module_llm import ModuleLLM
2-
2+
from mesa_llm.memory import Memory #to be done
33

44
class LLMAgent:
5-
def __init__(self, api_key: str, model: str = "openai/gpt-4o", system_prompt: str|None = None):
6-
"""Initialize the LLMAgent with a ModuleLLM instance."""
5+
"""
6+
LLMAgent manages an LLM backend and optionally connects to a memory module.
7+
8+
Parameters:
9+
api_key (str): The API key for the LLM provider.
10+
model (str): The model to use for the LLM in the format 'provider/model'. Defaults to 'openai/gpt-4o'.
11+
system_prompt (str | None): Optional system prompt to be used in LLM completions.
12+
memory (Memory | None): Optional memory instance to attach to this agent. Can only be set once.
13+
14+
Attributes:
15+
llm (ModuleLLM): The internal LLM interface used by the agent.
16+
memory (Memory | None): The memory module attached to this agent, if any.
17+
18+
Methods:
19+
attach_memory(memory):
20+
Attach a memory instance to this agent. Raises an error if a memory is already attached.
21+
22+
set_model(api_key, model):
23+
Update the LLM model used by the agent.
24+
25+
set_system_prompt(system_prompt):
26+
Update the system prompt used in completions.
27+
28+
Notes:
29+
- Each agent can only have one memory instance associated with it.
30+
- If no memory is passed at initialization, one can be attached later using `attach_memory()`.
31+
- Reassigning or replacing memory after it's been attached is not allowed and will raise a ValueError.
32+
"""
33+
34+
35+
def __init__(self, api_key: str, model: str = "openai/gpt-4o", system_prompt: str|None = None, memory: Memory|None =None):
736
self.llm = ModuleLLM(api_key=api_key, model=model, system_prompt=system_prompt)
37+
self._memory = None
38+
if memory is not None:
39+
self.attach_memory(memory)
40+
41+
@property
42+
def memory(self):
43+
return self._memory
44+
45+
def attach_memory(self, memory):
46+
if self._memory is not None:
47+
raise ValueError("Memory already attached to this agent.")
48+
self._memory = memory
849

950
def set_model(self, api_key: str, model: str = "openai/gpt-4o") -> None:
1051
"""Set the model of the Agent."""

0 commit comments

Comments
 (0)