Repository navigation
Expand file tree
/
Copy pathsimple_agent_lngraph.py
More file actions
68 lines (44 loc) · 1.77 KB
/
Copy pathsimple_agent_lngraph.py
File metadata and controls
68 lines (44 loc) · 1.77 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from typing import Annotated, TypedDict
from dotenv import load_dotenv
from openai import OpenAI
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, END
# Load environment variables from .env file
load_dotenv()
openai_key = os.getenv("OPENAI_API_KEY")
llm_name = "gpt-6-luna" # gpt-4o-mini shuts down Jan 14, 2027
client = OpenAI(api_key=openai_key)
model = ChatOpenAI(api_key=openai_key, model=llm_name, reasoning_effort="none")
# STEP 1: Build a Basic Chatbot
from langgraph.graph.message import add_messages
class State(TypedDict):
# Messages have the type "list". The `add_messages` function
# in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
def bot(state: State):
# print(state.items())
print(state["messages"])
return {"messages": [model.invoke(state["messages"])]}
graph_builder = StateGraph(State)
# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
graph_builder.add_node("bot", bot)
# STEP 3: Add an entry point to the graph
graph_builder.set_entry_point("bot")
# STEP 4: and end point to the graph
graph_builder.set_finish_point("bot")
# STEP 5: Compile the graph
graph = graph_builder.compile()
# res = graph.invoke({"messages": ["Hello, how are you?"]})
# print(res["messages"])
while True:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit", "q"]:
print("Goodbye!")
break
for event in graph.stream({"messages": ("user", user_input)}):
for value in event.values():
print("Assistant:", value["messages"][-1].content)