Hey there, everyone!
I’m facing a problem with my agent built with create_react_agent. I start a conversation questioning about some infos and it calls the tools correctly, but sometimes it just stops invoking the tools, even when there is not data related to the subject in the history.
Let me give you some examples:
I ask about the stock price of apple, the llm invoke the proper tool and give me the result back.
Later on the conversation, i ask about another company, never asked before and the llm returns the pre-trainned data, not the updated one.
When this happens, i keep asking it to invoke the tool, and it says that will invoke it, even return the correct name of the tool, but does not generate a ToolMessage.
I don’t know if it has something to do with the SummarizationNode, but i noticed this problem after implementing it on my graph.
Here is the code:
from datetime import datetime
from dotenv import load_dotenv
from langchain.agents import create_agent
from langchain_core.messages.utils import count_tokens_approximately
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
from langgraph.prebuilt import create_react_agent
from langgraph.prebuilt.chat_agent_executor import AgentState
from langmem.short_term import RunningSummary, SummarizationNode
from pydantic import BaseModel, Field
from app.artificial_inteligence.agents.financial_agent.prompt import (
FINANCIAL_AGENT_PROMPT,
)
from app.artificial_inteligence.agents.financial_agent.prompts.summary_prompt import (
EXISTING_SUMMARY_PROMPT,
FINAL_PROMPT,
INITIAL_SUMMARY_PROMPT,
)
from app.artificial_inteligence.agents.financial_agent.tools import (
financial_agent_tools,
)
from app.artificial_inteligence.agents.financial_agent.tools.tickers_semantic_search_tool import (
get_code_name_matches,
)
from app.artificial_inteligence.utils.math_tool import math_operation
load_dotenv()
model = ChatGoogleGenerativeAI(
model=“gemini-flash-lite-latest”,
temperature=0,
)
class GuardRailResponse(BaseModel):
approved: bool = Field(
description=“Retorne True se o dado analisado não constitue uma recomendação de investimento.”
)
correct_answer: str = Field(
description=“A mensagem reestruturada para que não se caracterize como uma recomendação de investimento.”
)
class State(AgentState):
context: dict[str, RunningSummary]
tools = [*financial_agent_tools, get_code_name_matches, math_operation]
summarization_node = SummarizationNode(
token_counter=count_tokens_approximately, # função para contar tokens
model=model, # modelo usado para gerar o resumo
max_tokens=15000, # máximo de tokens acumulados antes de resumir
max_summary_tokens=512, # tamanho máximo do resumo em si
max_tokens_before_summary=10000, # quando começar a resumir o histórico
output_messages_key=“llm_input_messages”, # onde armazenar as mensagens resumidas
existing_summary_prompt=EXISTING_SUMMARY_PROMPT,
initial_summary_prompt=INITIAL_SUMMARY_PROMPT,
final_prompt=FINAL_PROMPT,
)
def financial_agent(user_name: str, checkpointer: AsyncPostgresSaver):
prompt = FINANCIAL_AGENT_PROMPT.replace(“{user_name}”, user_name).replace(
“{current_date}”, datetime.now().strftime(“%d/%m/%Y %H:%M:%S”)
)
agent = create_react_agent(
pre_model_hook=summarization_node,
model=model,
tools=tools,
prompt=prompt,
checkpointer=checkpointer,
state_schema=State,
)
return agent