yashAI007 commited on
Commit
2bd81f8
·
1 Parent(s): af67114

first commt

Browse files
Files changed (4) hide show
  1. .env +1 -0
  2. app.py +237 -0
  3. chatbot.py +41 -0
  4. requirements.txt +0 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY = gsk_0hPoH6j7ExBFHfANeVjZruldUJSHRRTDIzBU
app.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
+ # from chabot import chatbot
3
+ # from langchain_core.messages import HumanMessage
4
+
5
+ # config = {"configurable": {"thread_id": "1"}}
6
+
7
+ # # ---- Gradio Chat Function ----
8
+ # def chat_fn(message, history):
9
+ # """
10
+ # message: current user message (string)
11
+ # history: list of previous messages [(user, bot), ...]
12
+ # """
13
+
14
+ # # Send message to LangGraph chatbot
15
+ # res = chatbot.invoke(
16
+ # {"messages": [HumanMessage(content=message)]},
17
+ # config=config
18
+ # )
19
+
20
+ # # Get AI response
21
+ # ai_message = res["messages"][-1].content
22
+
23
+ # return ai_message
24
+
25
+
26
+ # # ---- Create UI ----
27
+ # demo = gr.ChatInterface(
28
+ # fn=chat_fn,
29
+ # title="LangGraph + Groq Chatbot",
30
+ # description="Chatbot powered by LangGraph, Groq (Qwen3-32B), and memory",
31
+ # )
32
+
33
+ # if __name__ == "__main__":
34
+ # demo.launch()
35
+
36
+
37
+
38
+ # import gradio as gr
39
+ # import uuid
40
+ # from langchain_core.messages import HumanMessage
41
+ # from chabot import chatbot
42
+
43
+ # # -----------------------
44
+ # # Generate Unique Thread ID
45
+ # # -----------------------
46
+ # def new_thread():
47
+ # config = {"configurable": {"thread_id": str(uuid.uuid4())[:8] }}
48
+ # return config
49
+
50
+
51
+ # # -----------------------
52
+ # # Chat Function
53
+ # # -----------------------
54
+ # def chat_fn(message, history, thread_id):
55
+ # config = {"configurable": {"thread_id": thread_id}}
56
+
57
+ # res = chatbot.invoke(
58
+ # {"messages": [HumanMessage(content=message)]},
59
+ # config=config
60
+ # )
61
+
62
+ # ai_message = res["messages"][-1].content
63
+ # return ai_message
64
+
65
+
66
+ # # -----------------------
67
+ # # Start With Default Thread
68
+ # # -----------------------
69
+ # default_thread = new_thread()
70
+ # default_thread = default_thread["configurable"]["thread_id"]
71
+
72
+ # # -----------------------
73
+ # # Gradio UI
74
+ # # -----------------------
75
+ # with gr.Blocks() as demo:
76
+
77
+ # # Store thread id in hidden state
78
+ # thread_state = gr.State(default_thread)
79
+
80
+ # with gr.Row():
81
+
82
+ # # -------- Left Sidebar --------
83
+ # with gr.Column(scale=1):
84
+ # gr.Markdown("## 🧵 Current Thread ID")
85
+ # thread_display = gr.Textbox(
86
+ # value=default_thread,
87
+ # interactive=False,
88
+ # label="Thread ID"
89
+ # )
90
+
91
+ # new_chat_btn = gr.Button("➕ New Chat")
92
+
93
+ # # -------- Main Chat Area --------
94
+ # with gr.Column(scale=4):
95
+ # chatbot_ui = gr.ChatInterface(
96
+ # fn=lambda msg, hist: chat_fn(msg, hist, thread_state.value),
97
+ # title="LangGraph + Groq Chatbot",
98
+ # description="Memory enabled chatbot with unique threads",
99
+ # )
100
+
101
+ # # -----------------------
102
+ # # New Chat Button Logic
103
+ # # -----------------------
104
+ # def reset_chat():
105
+ # new_id = new_thread()
106
+ # return new_id, new_id, []
107
+
108
+ # new_chat_btn.click(
109
+ # reset_chat,
110
+ # outputs=[thread_state, thread_display, chatbot_ui.chatbot]
111
+ # )
112
+
113
+
114
+ # demo.launch()
115
+
116
+
117
+
118
+ import gradio as gr
119
+ import uuid
120
+ from langchain_core.messages import HumanMessage
121
+ from chatbot import chatbot
122
+
123
+
124
+ # -----------------------
125
+ # Generate Thread ID
126
+ # -----------------------
127
+ def new_thread_id():
128
+ return str(uuid.uuid4())[:8]
129
+
130
+
131
+ # -----------------------
132
+ # First Thread
133
+ # -----------------------
134
+ first_thread = new_thread_id()
135
+
136
+
137
+ # -----------------------
138
+ # Chat Function
139
+ # -----------------------
140
+ def chat_fn(message, chat_history, thread_id, all_threads):
141
+
142
+ config = {"configurable": {"thread_id": thread_id}}
143
+
144
+ res = chatbot.invoke(
145
+ {"messages": [HumanMessage(content=message)]},
146
+ config=config
147
+ )
148
+
149
+ ai_message = res["messages"][-1].content
150
+
151
+ # Append in NEW format
152
+ chat_history.append({"role": "user", "content": message})
153
+ chat_history.append({"role": "assistant", "content": ai_message})
154
+
155
+ all_threads[thread_id] = chat_history
156
+
157
+ return "", chat_history, all_threads
158
+
159
+
160
+ # -----------------------
161
+ # Create New Thread
162
+ # -----------------------
163
+ def create_new_thread(all_threads):
164
+ new_id = new_thread_id()
165
+ all_threads[new_id] = []
166
+
167
+ return (
168
+ all_threads,
169
+ gr.update(choices=list(all_threads.keys()), value=new_id),
170
+ new_id,
171
+ []
172
+ )
173
+
174
+
175
+ # -----------------------
176
+ # Switch Thread
177
+ # -----------------------
178
+ def switch_thread(thread_id, all_threads):
179
+ history = all_threads.get(thread_id, [])
180
+ return thread_id, history
181
+
182
+
183
+ # -----------------------
184
+ # UI
185
+ # -----------------------
186
+ with gr.Blocks() as demo:
187
+
188
+ thread_state = gr.State(first_thread)
189
+ threads_state = gr.State({first_thread: []})
190
+
191
+ with gr.Row():
192
+
193
+ # Sidebar
194
+ with gr.Column(scale=1):
195
+ gr.Markdown("## 🧵 Threads")
196
+
197
+ thread_radio = gr.Radio(
198
+ choices=[first_thread],
199
+ value=first_thread,
200
+ label="Select Thread"
201
+ )
202
+
203
+ new_chat_btn = gr.Button("➕ New Chat")
204
+
205
+ # Main Chat
206
+ with gr.Column(scale=4):
207
+
208
+ chatbot_ui = gr.Chatbot()
209
+ msg_box = gr.Textbox(placeholder="Type message and press Enter")
210
+
211
+ msg_box.submit(
212
+ chat_fn,
213
+ inputs=[msg_box, chatbot_ui, thread_state, threads_state],
214
+ outputs=[msg_box, chatbot_ui, threads_state],
215
+ )
216
+
217
+ # New Chat Button
218
+ new_chat_btn.click(
219
+ create_new_thread,
220
+ inputs=threads_state,
221
+ outputs=[
222
+ threads_state,
223
+ thread_radio,
224
+ thread_state,
225
+ chatbot_ui
226
+ ]
227
+ )
228
+
229
+ # Switch Thread
230
+ thread_radio.change(
231
+ switch_thread,
232
+ inputs=[thread_radio, threads_state],
233
+ outputs=[thread_state, chatbot_ui]
234
+ )
235
+
236
+
237
+ demo.launch()
chatbot.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langgraph.graph import START,END,StateGraph
2
+ from typing import TypedDict, Annotated
3
+ from langchain_core.messages import BaseMessage,HumanMessage
4
+ from langchain_groq import ChatGroq
5
+ from langgraph.graph.message import add_messages
6
+ from langgraph.checkpoint.memory import InMemorySaver
7
+ from dotenv import load_dotenv
8
+
9
+ load_dotenv()
10
+
11
+ checkpointer= InMemorySaver()
12
+
13
+ llm = ChatGroq(
14
+ model="qwen/qwen3-32b",
15
+ temperature=0,
16
+ max_tokens=None,
17
+ reasoning_format="parsed",
18
+ timeout=None,
19
+ max_retries=2,
20
+ # other params...
21
+ )
22
+
23
+
24
+ class ChatStat(TypedDict):
25
+ messages: Annotated[list[BaseMessage],add_messages]
26
+
27
+
28
+ def chat_node(state : ChatStat):
29
+ messages = state['messages']
30
+ response = llm.invoke(messages)
31
+ return {'messages': [response]}
32
+
33
+
34
+ graph = StateGraph(ChatStat)
35
+
36
+ graph.add_node("chat_node",chat_node)
37
+
38
+ graph.add_edge(START,'chat_node')
39
+ graph.add_edge("chat_node",END)
40
+
41
+ chatbot = graph.compile(checkpointer=checkpointer)
requirements.txt ADDED
Binary file (7.74 kB). View file