vsj0702 commited on
Commit
b2bd09b
·
verified ·
1 Parent(s): 8f19a92

Adding summery to chatbot

Browse files
Files changed (1) hide show
  1. chatbot.py +30 -16
chatbot.py CHANGED
@@ -15,22 +15,30 @@ class CodeAssistantBot:
15
  self.client = Groq(api_key=GROQ_API_KEY)
16
  self.model = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.6)
17
  self.analysis_prompt = ChatPromptTemplate.from_messages([
18
- ("system", "You are an expert code assistant. Keep responses concise."),
19
- ("user", "Code: {code}\nOutput: {output}\nError: {error}\nQuestion: {question}")
 
 
 
 
 
20
  ])
21
  self.summary_prompt = ChatPromptTemplate.from_messages([
22
- ("system", "Summarize key technical points."),
23
  ("user", "Conversation: {conversation}")
24
  ])
25
 
26
- def analyze_code(self, code, output, error, question):
27
  try:
28
  parser = StrOutputParser()
 
29
  chain = self.analysis_prompt | self.model | parser
30
  return chain.invoke({
31
  'code': code,
32
  'output': output,
33
  'error': error,
 
 
34
  'question': question
35
  })
36
  except Exception as e:
@@ -40,11 +48,9 @@ async def text_to_speech(text, filename):
40
  voice = "fr-FR-VivienneMultilingualNeural"
41
  await edge_tts.Communicate(text, voice).save(filename)
42
 
43
-
44
  def render_chatbot(code, output, error):
45
  """Render the chatbot UI with code-block support and a scrollable chat container."""
46
-
47
- # Inject CSS to make chat container scrollable
48
  st.markdown("""
49
  <style>
50
  .chat-container {
@@ -74,35 +80,43 @@ def render_chatbot(code, output, error):
74
  </style>
75
  """, unsafe_allow_html=True)
76
 
77
- # Setup session state
78
  st.session_state.setdefault('conversation', [])
79
  st.session_state.setdefault('audio_count', 0)
 
80
 
81
- # Input row
82
  c1, c2 = st.columns([4, 1], gap='small')
83
  with c1:
84
  question = st.text_input("Ask your question…", key="chat_input")
85
  with c2:
86
  send = st.button("🚀")
87
 
88
- # Handle send
89
  if send and question:
90
  bot = CodeAssistantBot()
91
- response = bot.analyze_code(code, output, error, question)
 
 
 
 
92
  st.session_state.conversation.append((question, response))
93
 
94
- # Chat container
 
 
 
 
 
 
 
 
 
95
  for q, a in st.session_state.conversation:
96
- # User message
97
  st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
98
 
99
- # Bot message with code formatting
100
  def format_response(text):
101
  parts = text.split('```')
102
  result = ''
103
  for i, part in enumerate(parts):
104
  if i % 2 == 1:
105
- # Remove optional language tag
106
  lines = part.splitlines()
107
  if lines and lines[0].isalpha():
108
  lines = lines[1:]
@@ -114,9 +128,9 @@ def render_chatbot(code, output, error):
114
 
115
  formatted = format_response(a)
116
  st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
 
117
  st.markdown('</div>', unsafe_allow_html=True)
118
 
119
- # Auto-scroll inside chat container
120
  st.markdown("""
121
  <script>
122
  const c = window.parent.document.querySelector('.chat-container');
 
15
  self.client = Groq(api_key=GROQ_API_KEY)
16
  self.model = ChatGroq(model="llama-3.3-70b-versatile", temperature=0.6)
17
  self.analysis_prompt = ChatPromptTemplate.from_messages([
18
+ ("system",
19
+ "You are a skilled coding assistant. Use the following context and user input to help."
20
+ " Refer to previous summary and recent interactions to make answers accurate. "
21
+ "Keep your response short, relevant, and conversational — smaller is better if it covers what's needed."),
22
+ ("user",
23
+ "Code: {code}\nOutput: {output}\nError: {error}\n"
24
+ "Summary: {summary}\nRecent: {recent}\nQuestion: {question}")
25
  ])
26
  self.summary_prompt = ChatPromptTemplate.from_messages([
27
+ ("system", "Summarize key technical points from the conversation so far."),
28
  ("user", "Conversation: {conversation}")
29
  ])
30
 
31
+ def analyze_code(self, code, output, error, question, summary="", history=None):
32
  try:
33
  parser = StrOutputParser()
34
+ recent = "\n".join([f"User: {q}\nBot: {a}" for q, a in (history or [])[-4:]])
35
  chain = self.analysis_prompt | self.model | parser
36
  return chain.invoke({
37
  'code': code,
38
  'output': output,
39
  'error': error,
40
+ 'summary': summary,
41
+ 'recent': recent,
42
  'question': question
43
  })
44
  except Exception as e:
 
48
  voice = "fr-FR-VivienneMultilingualNeural"
49
  await edge_tts.Communicate(text, voice).save(filename)
50
 
 
51
  def render_chatbot(code, output, error):
52
  """Render the chatbot UI with code-block support and a scrollable chat container."""
53
+
 
54
  st.markdown("""
55
  <style>
56
  .chat-container {
 
80
  </style>
81
  """, unsafe_allow_html=True)
82
 
 
83
  st.session_state.setdefault('conversation', [])
84
  st.session_state.setdefault('audio_count', 0)
85
+ st.session_state.setdefault('chat_summary', "")
86
 
 
87
  c1, c2 = st.columns([4, 1], gap='small')
88
  with c1:
89
  question = st.text_input("Ask your question…", key="chat_input")
90
  with c2:
91
  send = st.button("🚀")
92
 
 
93
  if send and question:
94
  bot = CodeAssistantBot()
95
+ history = st.session_state.conversation[-4:]
96
+ summary = st.session_state.chat_summary
97
+
98
+ response = bot.analyze_code(code, output, error, question, summary, history)
99
+
100
  st.session_state.conversation.append((question, response))
101
 
102
+ # Optionally update running summary
103
+ if len(st.session_state.conversation) >= 3:
104
+ full_chat = "\n".join([f"User: {q}\nBot: {a}" for q, a in st.session_state.conversation[-10:]])
105
+ try:
106
+ parser = StrOutputParser()
107
+ summarizer = bot.summary_prompt | bot.model | parser
108
+ st.session_state.chat_summary = summarizer.invoke({'conversation': full_chat})
109
+ except:
110
+ pass # Summary failure shouldn't break the UI
111
+
112
  for q, a in st.session_state.conversation:
 
113
  st.markdown(f'<div class="chat-message user-message">{escape(q)}</div>', unsafe_allow_html=True)
114
 
 
115
  def format_response(text):
116
  parts = text.split('```')
117
  result = ''
118
  for i, part in enumerate(parts):
119
  if i % 2 == 1:
 
120
  lines = part.splitlines()
121
  if lines and lines[0].isalpha():
122
  lines = lines[1:]
 
128
 
129
  formatted = format_response(a)
130
  st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
131
+
132
  st.markdown('</div>', unsafe_allow_html=True)
133
 
 
134
  st.markdown("""
135
  <script>
136
  const c = window.parent.document.querySelector('.chat-container');