> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Graph I: Hello World Graph

> Build your first simple stateful graph in LangGraph

In this section, you will learn the basics of building a LangGraph by constructing a simple "Hello World" graph.

## Objectives

1. Understand and define the `AgentState` structure.
2. Create simple node functions to process and update state.
3. Set up a basic LangGraph structure.
4. Compile and invoke a LangGraph graph.
5. Understand how data flows through a single node in LangGraph.

## Project Setup

Before writing any code, let's set up a clean, isolated project environment using `uv` and install the required dependencies:

### 1. Initialize the Project

Create a new directory for your project and initialize it with `uv`:

```bash theme={null}
# Create and enter the project directory
mkdir hello-langgraph
cd hello-langgraph

# Initialize a new project with uv
uv init
```

### 2. Install Dependencies

Install the required `langgraph` package using `uv add`:

```bash theme={null}
uv add langgraph
```

This will automatically create a virtual environment (`.venv`) and lock exact versions in `uv.lock`. Now you can create a file (e.g., `main.py`) and start editing!

## Graph 0: Hello World

#### Goal

Build a simple graph that updates a state containing only one variable `msg` to `"Hello, World!"`.

#### Sample Input

```python theme={null}
{"msg": ""}
```

#### Sample Output

```python theme={null}
{"msg": "Hello, World!"}
```

#### Plan

1. Define the state schema (`AgentState0`) with a single variable `msg`.
2. Define the node function `hello_node` that sets `msg` to `"Hello, World!"`.
3. Build and compile the graph.
4. Invoke the graph with an initial empty message.

### Code Implementation

#### 1. Define the State Schema

We define a state schema with a single field `msg` of type `str`:

```python theme={null}
from typing import TypedDict

class AgentState0(TypedDict):
    msg: str
```

#### 2. Define the Node Function

The node function updates the `msg` field in the state to `"Hello, World!"`:

```python theme={null}
def hello_node(state: AgentState0) -> AgentState0:
    state['msg'] = "Hello, World!"
    return state
```

#### 3. Build and Compile the Graph

We build a single-node graph:

```python theme={null}
from langgraph.graph import StateGraph, START, END

# Initialize graph
graph0 = StateGraph(AgentState0)

# Add node and transition
graph0.add_node("hello_world", hello_node)
graph0.set_entry_point("hello_world")
graph0.set_finish_point("hello_world")

# Compile
app0 = graph0.compile()
```

#### 4. Invoke the Graph

We invoke the graph by passing an initial state with an empty string:

```python theme={null}
result0 = app0.invoke({"msg": ""})
print(result0)
# Output: {'msg': 'Hello, World!'}
```

## Graph I: Hello World Graph

#### Goal

Build a simple graph that takes a user's name as input and outputs a personalized greeting.

#### Sample Input

```python theme={null}
{"name": "Amit"}
```

#### Sample Output

```python theme={null}
{"name": "Amit", "greet": "Hey Amit, how is your day going?"}
```

#### Plan

1. Define the `AgentState` schema containing `name` and `greet` variables.
2. Define the node function `greeting_node` that reads the input `name` and constructs the personalized greeting.
3. Build and compile the graph.
4. Invoke the graph with a custom name.

### Code Implementation

#### 1. Define the State Schema

The state defines the data structure shared across the graph. We use a `TypedDict` for this:

```python theme={null}
from typing import TypedDict

class AgentState(TypedDict):
    name: str
    greet: str
```

#### 2. Define the Node Function

A node is just a Python function that takes the current state as input and returns an updated dictionary of fields to merge back into the state:

```python theme={null}
def greeting_node(state: AgentState) -> AgentState:
    state['greet'] = f"Hey {state['name']}, how is your day going?"
    return state
```

#### 3. Build and Compile the Graph

We use `StateGraph` to define our nodes and layout:

```python theme={null}
from langgraph.graph import StateGraph, START, END

# Initialize the graph with the state schema
graph = StateGraph(AgentState)

# Add the node to the graph
graph.add_node("greeter", greeting_node)

# Set the entry point and the finish point
graph.set_entry_point("greeter")
graph.set_finish_point("greeter")

# Compile the graph into a runnable application
app = graph.compile()
```

#### 4. Invoke the Graph

We invoke the compiled application by passing the initial state:

```python theme={null}
result = app.invoke({"name": "Amit"})
print(result)
# Output: {'name': 'Amit', 'greet': 'Hey Amit, how is your day going?'}
```

## Exercise: Personalized Compliment Agent 🏗

#### Goal

Create a Personalized Compliment Agent using LangGraph.

#### Sample Input

```python theme={null}
{"name": "Amit"}
```

#### Sample Output

```python theme={null}
"Amit, you're doing an amazing job learning LangGraph!"
```

#### Plan

1. Define a state with `name` and `compliment` keys.
2. Use a single node that updates `compliment` using the input `name`.
3. Build, compile, and invoke the graph.

<Accordion title="Solution">
  ```python theme={null}
  from typing import TypedDict
  from langgraph.graph import StateGraph, START, END

  # 1. State Schema
  class AgentState(TypedDict):
      name: str
      compliment: str

  # 2. Node Function
  def compliment_node(state: AgentState) -> AgentState:
      state['compliment'] = f"{state['name']}, you're doing an amazing job learning LangGraph!"
      return state

  # 3. Create Graph
  graph = StateGraph(AgentState)
  graph.add_node("complimenter", compliment_node)
  graph.set_entry_point("complimenter")
  graph.set_finish_point("complimenter")

  # 4. Compile and Run
  app = graph.compile()
  result = app.invoke({"name": "Amit"})
  print(result['compliment'])
  ```
</Accordion>

## Practice & Exercises

To reinforce what you've learned in this section (defining schemas, creating nodes, compiling and invoking graphs), practice with the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice variable state schemas, node functions, building/compiling graphs, and implementing a personalized compliment agent.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langgraph/Graph1_Hello_World.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langgraph/Graph1_Hello_World.ipynb) | <a href="/public/notebooks/langgraph/Graph1_Hello_World.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
