Skip to content

Commit

Permalink
Add Memory to LLM Agent (#983)
Browse files Browse the repository at this point in the history
* refactor: remove unused superagent agent type

* bump redis version to 5.0.4

* feat: add buffer memory

* add message type

* remove unused import

* add memory to llm agent

* fix openai tool calling object's serialization

* use xml syntax in the prompt for chat history

* rename call_tool to execute_tool

* list messages in the ascending order of their creation
  • Loading branch information
elisalimli committed May 12, 2024
1 parent ae46cf1 commit 65ffb9c
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 209 deletions.
6 changes: 6 additions & 0 deletions libs/superagent/app/agents/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from functools import cached_property
from typing import Any, List, Optional

from langchain.agents import AgentExecutor
Expand Down Expand Up @@ -78,6 +79,11 @@ def prompt(self) -> Any:
def tools(self) -> Any:
...

# TODO: Set a proper return type when we remove Langchain agent type
@cached_property
async def memory(self) -> Any:
...

@abstractmethod
def get_agent(self) -> AgentExecutor:
...
Expand Down
8 changes: 4 additions & 4 deletions libs/superagent/app/agents/langchain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from functools import cached_property

from decouple import config
from langchain.agents import AgentType, initialize_agent
Expand Down Expand Up @@ -62,9 +63,8 @@ def _get_llm(self):
max_tokens=llm_data.params.max_tokens,
)

async def _get_memory(
self,
) -> None | MotorheadMemory | ConversationBufferWindowMemory:
@cached_property
async def memory(self) -> None | MotorheadMemory | ConversationBufferWindowMemory:
# if memory is already set, in the main agent base class, return it
if not self.session_id:
raise ValueError("Session ID is required to initialize memory")
Expand Down Expand Up @@ -95,7 +95,7 @@ async def _get_memory(

async def get_agent(self):
llm = self._get_llm()
memory = await self._get_memory()
memory = await self.memory
tools = self.tools
prompt = self.prompt

Expand Down

0 comments on commit 65ffb9c

Please sign in to comment.