-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_engine.py
More file actions
50 lines (41 loc) · 1.82 KB
/
Copy pathchat_engine.py
File metadata and controls
50 lines (41 loc) · 1.82 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
from langchain_groq import ChatGroq
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph
#from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage
class ChatBot:
AVAILABLE_MODELS = { # add models as per your Groq account
# DOC : https://console.groq.com/docs/models (note models change over time)
"llama-3.3-70b-versatile": "llama-3.3-70b-versatile",
"qwen/qwen3-32b": "qwen/qwen3-32b",
"groq/compound": "groq/compound",
"llama-3.1-8b-instant": "llama-3.1-8b-instant"
}
def __init__(self, groq_api_key: str, model_name: str = "qwen/qwen3-32b"):
self.groq_api_key = groq_api_key
self.model_name = model_name
self.llm = self._setup_llm()
self.app = self._setup_workflow()
def _setup_llm(self):
return ChatGroq(
temperature=0.1,
groq_api_key=self.groq_api_key,
model_name=self.model_name
)
def _setup_workflow(self):
workflow = StateGraph(state_schema=MessagesState)
def call_model(state: MessagesState):
#sys_msg = SystemMessage(content="You are a helpful assistant ")
response = self.llm.invoke(state["messages"])
return {"messages": response}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
return workflow.compile(checkpointer=MemorySaver())
def chat(self, message: str, thread_id: str):
return self.app.invoke(
{"messages": [{"role": "user", "content": message}]},
{"configurable": {"thread_id": thread_id}}
)
def update_model(self, model_name: str):
self.model_name = model_name
self.llm = self._setup_llm()
self.app = self._setup_workflow()