🚀
langraph
7. Simple Bot Agent

Simple Bot Agent 🤖

Objectives ✅
  1. Define state structure with a list of Human Message objects.
  2. Initialize a GPT-4o model using LangChain's ChatOpenAI
  3. Sending and handling different types of messages
  4. Building and compiling the graph of the Agent
from typing import TypedDict, List
from langchain_core.messages import HumanMessage
from langchain_ollama import OllamaLLM
from langgraph.graph import StateGraph, START, END
 
class AgentState(TypedDict):
    messages: List[HumanMessage]
 
ollama_model = "llama3.1:8b"
 
llm = OllamaLLM(temperature=0.8, model=ollama_model)
def process(state: AgentState) -> AgentState:
    # Extract the content from the last HumanMessage
    prompt = state["messages"][-1].content
    response = llm(prompt)
    state["messages"].append(HumanMessage(content=response))
    return state
graph = StateGraph(AgentState)
 
graph.add_node('process', process)
graph.add_edge(START, 'process')
graph.add_edge('process', END)
 
app = graph.compile()
 

"simple bot agent"

User Prompt

user_input = '''Hello, who are you?'''
 
result = app.invoke({"messages": [HumanMessage(content=user_input)]})
 
print(result['messages'][-1].content)
OUTPUT:
 
"I'm an artificial intelligence model known as Llama. Llama stands for Large Language Model Meta AI."

User Input

user_input = input("Enter: ")
 
result = app.invoke({"messages": [HumanMessage(content=user_input)]})
 
print(result["messages"][-1].content)
OUTPUT:
 
"It seems like you're referencing the famous painting, "The Blue Boy," by Thomas Gainsborough. However, it's also possible that you're simply expressing excitement or enthusiasm with a playful twist on "blue boy." If you'd like to discuss art, use "The Blue Boy" as an example of 18th-century English art, or if you have another context in mind for the term, please let me know. I'm here to help clarify or provide information based on your interest."

© 2024 Driptanil Datta.All rights reserved

Made with Love ❤️

Last updated on Mon Oct 20 2025