How can we trim messages while using create_supervisor prebuilt in langGraph? #5087
Unanswered
iamnikhildogra
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried using trim messages as a tool in a separate agent and then passing on to the supervisor agent but it didn't seem to work? Can anyone please help?
Code snippet-
self.supervisor = create_supervisor(
agents=[create_query_agent(auth_user_id), refiner_agent, image_gen_agent, preprocessor_agent],
model=ChatOpenAI(model="gpt-4o", openai_api_key=cnf.OPEN_AI_API_KEY),
prompt=(
generate_supervisor_prompt(auth_user_id)
),
)
Here the preprocessor agent includes the tool for trimming messages-
preprocessor_agent = create_react_agent(
model=ChatOpenAI(model="gpt-4o", openai_api_key=cnf.OPEN_AI_API_KEY),
tools=[trim_messages_wrapper],
prompt="You are a preprocessor agent. You are responsible for preprocessing the messages to the last 4000 tokens.",
name="preprocessor"
)
@tool
def trim_messages_wrapper(state: dict):
"""Trim the messages to the last 4000 tokens."""
llm = ChatOpenAI(model="gpt-4o", openai_api_key=cnf.OPEN_AI_API_KEY)
messages = state.get("messages", [])
trimmed_messages = trim_messages(
messages,
strategy="last",
token_counter=llm,
max_tokens=4000, # Adjust this value based on your needs
start_on="human",
end_on=("human", "tool"),
include_system=True
)
state["messages"] = trimmed_messages
return state
Beta Was this translation helpful? Give feedback.
All reactions