-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
41 lines (29 loc) · 955 Bytes
/
memory.py
File metadata and controls
41 lines (29 loc) · 955 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
35
36
37
38
39
40
41
from typing import TypedDict, Annotated
from langgraph.graph import add_messages, StateGraph, END
from main import llm
from langchain_core.messages import AIMessage, HumanMessage
from langgraph.checkpoint.memory import MemorySaver
memory = MemorySaver()
class BasicChatState(TypedDict):
messages: Annotated[list, add_messages]
def chatbot(state: BasicChatState):
return {
"messages": [llm.invoke(state["messages"])]
}
graph = StateGraph(BasicChatState)
graph.add_node("chatbot", chatbot)
graph.add_edge("chatbot", END)
graph.set_entry_point("chatbot")
app = graph.compile(checkpointer=memory)
config = {"configurable": {
"thread_id": 1
}}
while True:
user_input = input("User: ")
if(user_input in ["exit", "end"]):
break
else:
result = app.invoke({
"messages": [HumanMessage(content=user_input)]
}, config=config)
print("AI: " + result["messages"][-1].content)