Limit historical messages with Agent and Redis #20831
-
Checked other resources
Commit to Help
Example Codeimport os
from dotenv import load_dotenv
from fastapi import FastAPI
from typing import Any
from langchain_community.chat_message_histories import RedisChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_core.runnables.history import RunnableWithMessageHistory
from langserve import add_routes
# External Class for tool
from tool_creator import ToolBuilder
load_dotenv()
tool_builder = ToolBuilder()
tools = tool_builder.initialize_tools()
system_prompt="""
You are an assistant of a digital platform called XXX, reply in the same language of user.
Respond only using available tools.
If user has general questions, reply that you are not responsible for answering them.
"""
human_prompt = "{input}"
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
MessagesPlaceholder('chat_history'),
("human", human_prompt),
MessagesPlaceholder('agent_scratchpad'),
])
llm = ChatOpenAI(
model_name=os.getenv('OPENAI_CHAT_MODEL'),
temperature=os.getenv('OPENAI_TEMPERATURE'),
max_tokens=os.getenv('OPENAI_MAX_TOKENS')
)
agent = create_openai_tools_agent(
llm=llm,
tools=tools,
prompt=prompt
)
app = FastAPI()
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
max_iterations=5,
verbose=True,
handle_parsing_errors=True
)
REDIS_URL = "redis://localhost:6379/0"
config={"configurable": {"session_id": "<SESSION_ID>"}}
agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),
input_messages_key="input",
history_messages_key="chat_history",
)
add_routes(
app,
agent_with_chat_history
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8000) DescriptionI created this python script to use langserve and expose an Agent that has 3 tools inside and stores the user history on redisDB. These tools (rag type) are incorporated via an AgentExecutor. My question is: how can I limit the history messages that are loaded by the Agent? thanks System InfoSystem Information
Package Information
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To limit the history messages loaded by the Agent to the last 5 saved messages, you'll need to adjust the logic that handles the retrieval and storage of chat history in Redis. This involves modifying how messages are added to and retrieved from the Redis database to ensure only the last 5 messages are considered for each session. For the retrieval part, when fetching messages from Redis to load into the chat history, you can use Redis' def get_last_messages(session_id, count=5):
messages = redis_client.lrange(session_id, -count, -1)
return messages This function fetches the last 5 messages for a given For the storage part, ensure that when messages are added to Redis, if the total exceeds your desired history length (in this case, 5 messages), older messages are removed. This can be managed by a combination of Redis' def add_message(session_id, message):
redis_client.lpush(session_id, message)
redis_client.ltrim(session_id, -5, -1) This approach ensures that only the last 5 messages are kept in the Redis list for any session, effectively limiting the chat history that the Agent processes to the most recent 5 messages. This method directly addresses the need to reduce token consumption by limiting the history size.
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
To limit the history messages loaded by the Agent to the last 5 saved messages, you'll need to adjust the logic that handles the retrieval and storage of chat history in Redis. This involves modifying how messages are added to and retrieved from the Redis database to ensure only the last 5 messages are considered for each session.
For the retrieval part, when fetching messages from Redis to load into the chat history, you can use Redis'
LRANGE
command to get only the last 5 messages. Here's a conceptual adjustment you might make in the part of your code that retrieves chat history: