> ## 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.

# Simple Bot

> Build a basic stateless agent that communicates with ChatOpenAI in LangGraph

In this section, we will see how to build a basic stateless AI agent using ChatOpenAI and LangGraph.

## Objectives

1. Define the State schema for storing message history.
2. Initialize ChatOpenAI and invoke it from a graph node.
3. Build a stateless agent graph with a single processor node.
4. Compile and run the agent.

## Agent I: Simple Agent Bot

#### Goal

Build a simple stateless agent using LangGraph that takes user messages and passes them directly to an LLM for processing.

#### Sample Input

```python theme={null}
{"messages": [HumanMessage(content="Tell me a quick 1-line joke.")]}
```

#### Sample Output

```python theme={null}
"Why don't skeletons fight each other? They don't have the guts."
```

#### Plan

1. Define the state schema (`AgentState`) containing `messages` (a list of messages).
2. Define the node function `process` that invokes the LLM with the messages.
3. Build the graph by adding the node and connecting START to it and it to END.
4. Compile and invoke the agent.

### Code Implementation

#### 1. Define the State Schema

We define a schema `AgentState` with a list of messages:

```python theme={null}
from typing import TypedDict
from langchain_core.messages import HumanMessage

class AgentState(TypedDict):
    messages: list[HumanMessage]
```

#### 2. Define the Node Function

The node function receives the state, invokes the model, and prints the response:

```python theme={null}
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o")

def process(state: AgentState) -> AgentState:
    response = llm.invoke(state["messages"])
    print(f"\nAI: {response.content}")
    return state
```

#### 3. Build and Compile the Graph

We setup the StateGraph, register the node, and link entry and finish points:

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

graph = StateGraph(AgentState)
graph.add_node("process", process)

graph.add_edge(START, "process")
graph.add_edge("process", END) 

agent = graph.compile()
```

#### 4. Invoke the Agent

We invoke the compiled agent:

```python theme={null}
user_msg = [HumanMessage(content="Tell me a quick 1-line joke.")]
agent.invoke({"messages": user_msg})
```

## Exercise: Pirate Speak Translator Agent 🏴‍☠️

#### Goal

Build a custom system prompt agent that translates whatever user inputs into dramatic Pirate Speak.

#### Sample Input

```python theme={null}
{"messages": [HumanMessage(content="Hello, how are you doing today?")]}
```

#### Sample Output

```python theme={null}
"Ahoy, matey! How be ye farin' this fine day?"
```

#### Plan

1. Reuse or create an `AgentState` containing the message list.
2. Write a node function `pirate_node` that inserts a `SystemMessage` specifying the pirate persona ahead of the user messages, invokes the LLM, and prints the pirate response.
3. Initialize the `StateGraph`, register the pirate node, hook it to START/END, and compile.
4. Test the compiled agent with a human message and verify the pirate speak output.

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

  # 1. State Schema
  class AgentState(TypedDict):
      messages: list[HumanMessage]

  llm = ChatOpenAI(model="gpt-4o")

  # 2. Node Function
  def pirate_node(state: AgentState) -> AgentState:
      sys_msg = SystemMessage(content="You are a pirate. Translate the user's input into dramatic pirate speak.")
      response = llm.invoke([sys_msg] + state["messages"])
      print(f"\nPirate AI: {response.content}")
      return state

  # 3. Create Graph
  graph = StateGraph(AgentState)
  graph.add_node("pirate_agent", pirate_node)
  graph.add_edge(START, "pirate_agent")
  graph.add_edge("pirate_agent", END)
  app = graph.compile()

  # 4. Invoke
  app.invoke({"messages": [HumanMessage(content="Hello, how are you doing today?")]})
  ```
</Accordion>

## Practice & Exercises

To reinforce what you've learned in this section, practice with the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice defining simple agent structures, customizing system instructions, and compiling basic workflows.

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