-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentic_chatbot_backend.py
More file actions
44 lines (32 loc) · 1.01 KB
/
Copy pathagentic_chatbot_backend.py
File metadata and controls
44 lines (32 loc) · 1.01 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
from langgraph.graph import StateGraph, END, START
from typing import TypedDict, Annotated
from langchain_core.messages import BaseMessage, HumanMessage
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import os
from langgraph.checkpoint.memory import MemorySaver
load_dotenv()
from langchain_groq import ChatGroq
llm = ChatGroq(
model="openai/gpt-oss-20b",
groq_api_key=os.getenv("GROQ_API_KEY"),
temperature=0,
)
from langgraph.graph.message import add_messages
class ChatState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
def chat_node(state: ChatState):
# take user query from state
message = state['messages']
# send to llm
resp = llm.invoke(message)
# response store state
return {'messages': [resp]}
checkpoint = MemorySaver()
graph = StateGraph(ChatState)
# add nodes
graph.add_node('chat_node', chat_node)
# add edges
graph.add_edge(START, 'chat_node')
graph.add_edge('chat_node', END)
chatbot = graph.compile(checkpointer=checkpoint)