-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile.py
More file actions
48 lines (32 loc) · 1.15 KB
/
Copy pathfile.py
File metadata and controls
48 lines (32 loc) · 1.15 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
from langchain_openai import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph, START, MessagesState
import os
os.environ.get("OPENAI_API_KEY")
# Initialize model
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
# Create graph
builder = StateGraph(state_schema=MessagesState)
def chat_node(state: MessagesState):
system_message = SystemMessage(content="You're a kind therapy assistant.")
history = state["messages"]
prompt = [system_message] + history
response = model.invoke(prompt)
return {"messages": response}
builder.add_node("chat", chat_node)
builder.add_edge(START, "chat")
# Compile graph with MemorySaver
memory = MemorySaver()
chat_app = builder.compile(checkpointer=memory)
thread_id = "1"
while True:
user_input = input("You: ")
state_update = {"messages": [HumanMessage(content=user_input)]}
result = chat_app.invoke(
state_update,
{"configurable": {"thread_id": thread_id}}
)
print(result)
ai_msg = result["messages"][-1]
print("Bot:", ai_msg.content)