File size: 1,277 Bytes
61f1726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langgraph.graph import StateGraph, START, END
from typing import TypedDict, Annotated
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph.message import add_messages
from langgraph.checkpoint.sqlite import SqliteSaver
import sqlite3

from langchain_groq import ChatGroq
from pathlib import Path
import os
from dotenv import load_dotenv

load_dotenv()

full_path_dir = Path(__file__).resolve().parent

groq_api_key=os.getenv("GROQ_API_KEY")

llm=ChatGroq(model="llama-3.1-8b-instant",groq_api_key=groq_api_key)

# Define state
class ChatState(TypedDict):
    messages: Annotated[list[BaseMessage], add_messages] 

# Define node function
def chat_node(state: ChatState) -> ChatState:
    # take user query from state
    messages=state['messages']

    # send to llm
    response=llm.invoke(messages)

    # response store in state
    return {'messages': [response]}

# connection
conn=sqlite3.connect(database=f'{full_path_dir}/dbfiles/chatbot.db', check_same_thread=False)

# Checkpoint
checkpointer=SqliteSaver(conn=conn)

# graph
graph=StateGraph(ChatState)

# add node
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=checkpointer)