Lecture Notes on Agents with Lang Graph
Introduction to Lang Graph
- Lang Graph is built on top of Lang Chain.
- Primary focus is on creating agents or agentic workflows.
- Key functions of Lang Graph:
- Use of LLM (Large Language Model) to determine a sequence of actions.
- Combine multiple agents into an agent swarm for data sharing and collaboration.
Comparison with Other Frameworks
- Autogen and Crew AI: Higher-level frameworks with certain merits.
- Lang Graph: Steeper learning curve but provides a deeper understanding and customizable workflows.
- Focus on notes, edges, and state for comprehensive agent behavior control.
Key Concepts
- Notes: Functions or runnables that perform specific tasks; each note processes input and updates state.
- Edges: Connections that define the flow and order of execution between notes.
- State: Represents the data exchanged between notes, enabling dynamic workflows.
Example: Basic Notes and Edges
- Example workflow creation in VS Code:
- Initial setup includes installing Lang Graph via
pip install Lang graph.
- Usage of an OpenAI API key.
- Import classes such as
MessageGraph.
- Create nodes (e.g., Branch A, Branch B) and define edges to control the flow.
Visualizing Graphs
- Visualization accessible post-compilation via methods such as
get_graph().
- Helpful for understanding complex workflows.
Conditional Edges
- Ability to route actions based on input through conditional edges.
- Example of a simple conditional graph demonstrating this feature.
Cycles in Lang Graph
- Cyclic workflows are possible with Lang Graph but not with mainstream LCL (Lang Chain expression language).
- Example of a cyclic workflow with conditions to repeat tasks.
Real-World Application: Fake Weather API
- Creating an agent that interacts with a simulated weather API.
- Setup includes:
- Custom state maintaining API call count with a limit on retries.
- Integration of a weather-checking function utilizing a randomized output.
Workflow Implementation Steps
- Define messages and tool calls.
- Use conditional logic to manage API calls based on input messages.
- Create and invoke the Lang Graph workflow designed to return weather information.
Conclusion
- Understanding how to build agents using Lang Graph leads to complex workflow creation.
- Future discussions will focus on multi-agent workflows.
Main Takeaway: Lang Graph delivers robust flexibility and customizability for building and managing agent interactions and workflows, particularly useful in real-world applications like API integration.