AgentState and perform operations using them.
Objectives
- Define a more complex
AgentStatecontaining multiple fields. - Create a processing node that performs operations on list data and string data.
- Build and compile a LangGraph with multiple fields in its state.
- Invoke the graph with structured inputs and retrieve the computed result.
Graph II: Multiple Inputs Graph
Goal
Build a simple graph that takes a list of integers and a name as input, computes the sum of the integers, and returns a personalized message displaying the sum.Sample Input
Sample Output
Plan
- Define the state schema (
AgentState) containingvalues(list of integers),name(str), andresult(str). - Define the node function
process_valuesthat extracts the fields, calculates the sum, and formats the personalized greeting. - Build and compile the graph.
- Invoke the graph with multiple input values.
Code Implementation
1. Define the State Schema
We define a schemaAgentState with a list of integers, a name string, and a result string:
2. Define the Node Function
The node function receives the state, extracts the name and list of integers, computes the sum, and formats a greeting:3. Create the Graph
We define the graph structure, add the processor node, and set entry/finish points:4. Invoke the Graph
Pass multiple fields inside the initial dictionary:Exercise: Calculator Agent 🏆
Goal
Create a graph where you pass in a list of integers, a name, and an operation (addition"+", or multiplication "*"). Perform the corresponding operation in the node.
Sample Input
Sample Output
Plan
- Define the state schema with
name,values,operation, andresultkeys. - Write a node function that evaluates the operation field and calculates either the sum or product of the list.
- Build, compile, and run the graph.
Solution
Solution
Practice & Exercises
To reinforce what you’ve learned in this section (handling multiple input fields, processing list and string data), practice with the interactive notebook:Practice & Exercises
Practice managing multiple-input states, writing processing nodes, and building a calculator agent.💻 VS Code | 🚀 Colab | 📥 Download