-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_basic_example.py
More file actions
34 lines (22 loc) · 946 Bytes
/
main_basic_example.py
File metadata and controls
34 lines (22 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from dotenv import load_dotenv
from typing import Annotated, Literal
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain.chat_models import init_chat_model
from pydantic import BaseModel, Field
from typing_extensions import TypedDict
load_dotenv()
llm = init_chat_model("anthropic:claude-sonnet-4-5-20250929")
#"anthropic:claude-sonnet-4-5-20250929" also possible as modern alternative
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
def chatbot(state:State):
return {"messages": [llm.invoke(state["messages"])]}
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START,"chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()
user_input = input("Enter a message: ")
state = graph.invoke({"messages": [{"role": "user", "content": user_input}]})
print(state["messages"][-1])