An LLM agent that uses SharedWorkingMemory to remember information.
This agent extends BaseLLMAgent with memory capabilities, allowing it to:
- Access shared working memory in its system context
- Learn new information from conversations
- Merge learned information back into shared memory
The agent automatically includes memory context in its prompts and extracts memory updates from LLM responses using structured output.
Usage
alias Mojentic.Context.SharedWorkingMemory
alias Mojentic.Agents.BaseLLMAgentWithMemory
alias Mojentic.LLM.Broker
# Create shared memory
memory = SharedWorkingMemory.new(%{
"User" => %{"name" => "Alice"}
})
# Create broker
broker = Broker.new("qwen2.5:7b", Ollama)
# Create agent with memory
agent = BaseLLMAgentWithMemory.new(
broker: broker,
memory: memory,
behaviour: "You are a helpful assistant.",
instructions: "Answer questions and remember new facts.",
response_model: %{
"type" => "object",
"required" => ["text"],
"properties" => %{
"text" => %{"type" => "string", "description" => "Your response"}
}
}
)
# Generate response - memory is automatically included
{:ok, response, updated_memory} =
BaseLLMAgentWithMemory.generate_response_with_memory(agent, "What's my name?")Examples
# Create agent that remembers user preferences
memory = SharedWorkingMemory.new(%{})
broker = Broker.new("qwen2.5:7b", Ollama)
agent = BaseLLMAgentWithMemory.new(
broker: broker,
memory: memory,
behaviour: "You are a personal assistant.",
instructions: "Help the user and remember their preferences.",
response_model: %{
"type" => "object",
"required" => ["response"],
"properties" => %{
"response" => %{"type" => "string"}
}
}
)
{:ok, response, memory} =
BaseLLMAgentWithMemory.generate_response_with_memory(
agent,
"I prefer dark mode and vim keybindings"
)
# Memory now contains user preferences
Summary
Functions
Creates initial messages with behaviour, memory, and instructions.
Generates a response with memory context and updates.
Creates a new BaseLLMAgentWithMemory.
Updates an agent's memory reference.
Types
@type t() :: %Mojentic.Agents.BaseLLMAgentWithMemory{ behaviour: String.t(), broker: Mojentic.LLM.Broker.t(), instructions: String.t(), memory: Mojentic.Context.SharedWorkingMemory.t(), response_model: map(), tools: [module()] | nil }
Functions
Creates initial messages with behaviour, memory, and instructions.
The messages include:
- System message with agent behaviour
- Memory context (what the agent remembers)
- User message with instructions
Parameters
agent- The BaseLLMAgentWithMemory instance
Returns
List of Message structs.
Examples
messages = BaseLLMAgentWithMemory.create_initial_messages(agent)
#=> [
# %Message{role: :system, content: "You are..."},
# %Message{role: :user, content: "This is what you remember:..."},
# %Message{role: :user, content: "Answer questions..."}
# ]
Generates a response with memory context and updates.
This function:
- Creates initial messages with current memory
- Adds the user's content
- Requests structured output with memory field
- Merges memory updates back into SharedWorkingMemory
- Returns the response and updated memory
Parameters
agent- The BaseLLMAgentWithMemory instancecontent- The user's input string
Returns
{:ok, response_data, updated_memory}on success{:error, reason}on failure
Examples
{:ok, response, memory} =
BaseLLMAgentWithMemory.generate_response_with_memory(
agent,
"My favorite color is blue"
)
# response contains the structured response (without memory field)
# memory is updated SharedWorkingMemory with new information
Creates a new BaseLLMAgentWithMemory.
Parameters
opts: Keyword list with::broker- LLM broker instance (required):memory- SharedWorkingMemory instance (required):behaviour- System message defining agent personality (required):instructions- Instructions for processing events (required):response_model- JSON schema for structured output (required):tools- List of tool modules (optional)
Examples
agent = BaseLLMAgentWithMemory.new(
broker: broker,
memory: memory,
behaviour: "You are a helpful assistant.",
instructions: "Answer questions and learn new facts.",
response_model: %{
"type" => "object",
"required" => ["answer"],
"properties" => %{
"answer" => %{"type" => "string"}
}
}
)
Updates an agent's memory reference.
Returns a new agent struct with the updated memory.
Parameters
agent- The BaseLLMAgentWithMemory instancememory- The new SharedWorkingMemory instance
Examples
agent = BaseLLMAgentWithMemory.update_memory(agent, new_memory)