langchain-ai / langgraph

Build resilient language agents as graphs.
https://langchain-ai.github.io/langgraph/
MIT License
6.96k stars 1.12k forks source link

DOC: <Please write a comprehensive title after the 'DOC: ' prefix>How to add prompt to every react_agent in every team? #1764

Open guapia233 opened 2 months ago

guapia233 commented 2 months ago

Issue with current documentation:

hierarchical_agent_teams.ipynb link:https://github.com/langchain-ai/langgraph/blob/main/docs/docs/tutorials/multi_agent/hierarchical_agent_teams.ipynb

Idea or request for content:

I had been working on this example code for a long time in the field of penetration testing (although it was constantly being changed by the authorities) and started to notice a problem. For example, now there is a scan team consisting of a supervisor and two agents, each equipped with a tool. A team is given a task to be completed in two steps, which requires the cooperation of two agents to complete step by step, but often the task will be stuck in the cycle of the first agent, even if the first part of the task has been completed, and many times the team will not realize the existence of the second tool during the execution (the definition is correct, a separate test can succeed). I would like to know how this is caused and how it can be improved. In the example code, only the supervisor has prompts.How would you add a prompt to each agent, perhaps to solve this problem? I would be grateful if you could answer!

py-taha commented 6 days ago

You can add additional prompt to your react_agent by using state_modifier . So, if you set state_modifier as a string then it will be converted into SystemMessage and will be added to the beginning of the messages list. Same would happen if you pass a SystemMessage on your own.

For more you can refer the documentation here

py-taha commented 6 days ago

This is what I understood by your comment:

  1. You have a team which has the following members: a. Supervisor b. Agent 1 - has 1 tool c. Agent 2 - has 1 tool

What you are facing is: your execution get stuck in the first agent and never goes to the second even though the agent 1's task is completed. If this is correct then I'd suggest to first setup LangSmith (if not done yet), it's extremely simple and straight forward.

  1. Make sure you are passing the messages list with relevant messages.
  2. I personally like to add a brief description for each members so the supervisor take better decision
  3. Maintain a prev key in your state which stores which node/agent has been already called and pass it to the supervisor.
  4. Prompt your react_agent better

Let me know if this helps, cheers.

guapia233 commented 5 days ago

@py-taha I've already use langsimth before, and have done the first two points. Therefore, I will modify codes from the last two points you suggested:add prompts and maintain a prev key. I really appreciate the advice, it was exactly what I needed!

py-taha commented 5 days ago

@py-taha I've already use langsimth before, and have done the first two points. Therefore, I will modify codes from the last two points you suggested:add prompts and maintain a prev key. I really appreciate the advice, it was exactly what I needed!

I appreciate it. Let me know if that doesn't work, I'd be happy to help even further.