Skip to content

Commit

Permalink
support redis memory
Browse files Browse the repository at this point in the history
  • Loading branch information
bdqfork committed Feb 19, 2024
1 parent 276541b commit 29c0e59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions libs/superagent/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ OPENROUTER_API_KEY=
# Mandatory for Neon DB
DATABASE_SHADOW_URL=
# Memory (mandatory)
MEMORY=
# Redis Memory
REDIS_URL=
REDIS_MEMORY_WINDOW=
# Motorhead Memory
MEMORY_API_URL=https://memory.superagent.sh
# NOTE: Vectorstores (one is mandatory if you plan on loading datasources)
VECTORSTORE=pinecone # `qdrant`, `weaviate` etc.
Expand Down
48 changes: 35 additions & 13 deletions libs/superagent/app/agents/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from decouple import config
from langchain.agents import AgentType, initialize_agent
from langchain.chains import LLMChain
from langchain.memory.motorhead_memory import MotorheadMemory
from langchain.memory import (
ConversationBufferWindowMemory,
MotorheadMemory,
RedisChatMessageHistory,
)
from langchain.prompts import MessagesPlaceholder, PromptTemplate
from langchain.schema import SystemMessage
from langchain_openai import AzureChatOpenAI, ChatOpenAI
Expand Down Expand Up @@ -181,18 +185,36 @@ async def _get_prompt(self, agent: Agent) -> str:
return SystemMessage(content=content)

async def _get_memory(self) -> List:
memory = MotorheadMemory(
session_id=(
f"{self.agent_id}-{self.session_id}"
if self.session_id
else f"{self.agent_id}"
),
memory_key="chat_history",
url=config("MEMORY_API_URL"),
return_messages=True,
output_key="output",
)
await memory.init()
memory_type = config("MEMORY", "Motorhead")
if memory_type == "redis":
memory = ConversationBufferWindowMemory(
chat_memory=RedisChatMessageHistory(
session_id=(
f"{self.agent_id}-{self.session_id}"
if self.session_id
else f"{self.agent_id}"
),
url=config("REDIS_URL"),
key_prefix="superagent:",
),
memory_key="chat_history",
return_messages=True,
output_key="output",
k=config("REDIS_MEMORY_WINDOW", 10),
)
else:
memory = MotorheadMemory(
session_id=(
f"{self.agent_id}-{self.session_id}"
if self.session_id
else f"{self.agent_id}"
),
memory_key="chat_history",
url=config("MEMORY_API_URL"),
return_messages=True,
output_key="output",
)
await memory.init()
return memory

async def get_agent(self):
Expand Down

0 comments on commit 29c0e59

Please sign in to comment.