vsj0702 commited on
Commit
9c9855b
·
verified ·
1 Parent(s): 4e5a20d

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +14 -33
chatbot.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
- from openai import OpenAI
3
  from langchain_core.prompts import ChatPromptTemplate
 
4
  from html import escape
5
  import edge_tts
6
  import asyncio
@@ -9,12 +10,13 @@ import uuid
9
 
10
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
11
 
12
-
13
  class CodeAssistantBot:
14
  def __init__(self):
15
- self.client = OpenAI(
 
16
  base_url="https://openrouter.ai/api/v1",
17
- api_key=OPENROUTER_API_KEY
 
18
  )
19
 
20
  self.analysis_prompt = ChatPromptTemplate.from_messages([
@@ -26,12 +28,10 @@ class CodeAssistantBot:
26
  "Code: {code}\nInput: {input}\nOutput: {output}\nError: {error}\n"
27
  "Summary: {summary}\nRecent: {recent}\nQuestion: {question}")
28
  ])
29
-
30
  self.summary_prompt = ChatPromptTemplate.from_messages([
31
  ("system", "Summarize key technical points from the conversation so far."),
32
  ("user", "Conversation: {conversation}")
33
  ])
34
-
35
  self.voice_prompt = ChatPromptTemplate.from_messages([
36
  ("system",
37
  "You are a friendly narrator voice bot. Given a technical answer and its context,"
@@ -43,8 +43,10 @@ class CodeAssistantBot:
43
  ])
44
 
45
  def analyze_code(self, code, input, output, error, question, summary="", history=None):
 
46
  recent = "\n".join([f"User: {q}\nBot: {a}" for q, a in (history or [])[-4:]])
47
- prompt = self.analysis_prompt.format_messages({
 
48
  'code': code,
49
  'input': input,
50
  'output': output,
@@ -53,14 +55,11 @@ class CodeAssistantBot:
53
  'recent': recent,
54
  'question': question
55
  })
56
- completion = self.client.chat.completions.create(
57
- model="qwen/qwen3-coder:free",
58
- messages=[m.dict() for m in prompt]
59
- )
60
- return completion.choices[0].message.content.strip()
61
 
62
  def narrate_response(self, code, input, output, error, answer, summary=""):
63
- prompt = self.voice_prompt.format_messages({
 
 
64
  'code': code,
65
  'input': input,
66
  'output': output,
@@ -68,29 +67,12 @@ class CodeAssistantBot:
68
  'summary': summary,
69
  'answer': answer
70
  })
71
- completion = self.client.chat.completions.create(
72
- model="qwen/qwen3-coder:free",
73
- messages=[m.dict() for m in prompt]
74
- )
75
- return completion.choices[0].message.content.strip()
76
-
77
- def summarize_conversation(self, conversation):
78
- prompt = self.summary_prompt.format_messages({
79
- 'conversation': conversation
80
- })
81
- completion = self.client.chat.completions.create(
82
- model="qwen/qwen3-coder:free",
83
- messages=[m.dict() for m in prompt]
84
- )
85
- return completion.choices[0].message.content.strip()
86
-
87
 
88
  async def text_to_speech(text, filename):
89
  voice = "fr-FR-VivienneMultilingualNeural"
90
  communicate = edge_tts.Communicate(text, voice)
91
  await communicate.save(filename)
92
 
93
-
94
  def render_chatbot(code, input, output, error):
95
  st.markdown("""
96
  <style>
@@ -139,11 +121,11 @@ def render_chatbot(code, input, output, error):
139
  response = bot.analyze_code(code, input, output, error, question, summary, history)
140
  st.session_state.conversation.append((question, response))
141
  st.session_state.chat_display_count = 5
142
-
143
  if len(st.session_state.conversation) >= 3:
144
  try:
145
  full_chat = "\n".join([f"User: {q}\nBot: {a}" for q, a in st.session_state.conversation[-10:]])
146
- st.session_state.chat_summary = bot.summarize_conversation(full_chat)
 
147
  except:
148
  pass
149
 
@@ -172,7 +154,6 @@ def render_chatbot(code, input, output, error):
172
  st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
173
 
174
  audio_file = st.session_state.narrated_audio.get((q, a))
175
-
176
  if not audio_file:
177
  if st.button("🔊 Narrate", key=f"narrate_{idx}"):
178
  status_placeholder = st.empty()
 
1
  import streamlit as st
2
+ from langchain_openai import ChatOpenAI
3
  from langchain_core.prompts import ChatPromptTemplate
4
+ from langchain_core.output_parsers import StrOutputParser
5
  from html import escape
6
  import edge_tts
7
  import asyncio
 
10
 
11
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
12
 
 
13
  class CodeAssistantBot:
14
  def __init__(self):
15
+ self.model = ChatOpenAI(
16
+ model="meta-llama/llama-3-70b-instruct",
17
  base_url="https://openrouter.ai/api/v1",
18
+ api_key=OPENROUTER_API_KEY,
19
+ temperature=0.6
20
  )
21
 
22
  self.analysis_prompt = ChatPromptTemplate.from_messages([
 
28
  "Code: {code}\nInput: {input}\nOutput: {output}\nError: {error}\n"
29
  "Summary: {summary}\nRecent: {recent}\nQuestion: {question}")
30
  ])
 
31
  self.summary_prompt = ChatPromptTemplate.from_messages([
32
  ("system", "Summarize key technical points from the conversation so far."),
33
  ("user", "Conversation: {conversation}")
34
  ])
 
35
  self.voice_prompt = ChatPromptTemplate.from_messages([
36
  ("system",
37
  "You are a friendly narrator voice bot. Given a technical answer and its context,"
 
43
  ])
44
 
45
  def analyze_code(self, code, input, output, error, question, summary="", history=None):
46
+ parser = StrOutputParser()
47
  recent = "\n".join([f"User: {q}\nBot: {a}" for q, a in (history or [])[-4:]])
48
+ chain = self.analysis_prompt | self.model | parser
49
+ return chain.invoke({
50
  'code': code,
51
  'input': input,
52
  'output': output,
 
55
  'recent': recent,
56
  'question': question
57
  })
 
 
 
 
 
58
 
59
  def narrate_response(self, code, input, output, error, answer, summary=""):
60
+ parser = StrOutputParser()
61
+ narration_chain = self.voice_prompt | self.model | parser
62
+ return narration_chain.invoke({
63
  'code': code,
64
  'input': input,
65
  'output': output,
 
67
  'summary': summary,
68
  'answer': answer
69
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  async def text_to_speech(text, filename):
72
  voice = "fr-FR-VivienneMultilingualNeural"
73
  communicate = edge_tts.Communicate(text, voice)
74
  await communicate.save(filename)
75
 
 
76
  def render_chatbot(code, input, output, error):
77
  st.markdown("""
78
  <style>
 
121
  response = bot.analyze_code(code, input, output, error, question, summary, history)
122
  st.session_state.conversation.append((question, response))
123
  st.session_state.chat_display_count = 5
 
124
  if len(st.session_state.conversation) >= 3:
125
  try:
126
  full_chat = "\n".join([f"User: {q}\nBot: {a}" for q, a in st.session_state.conversation[-10:]])
127
+ summarizer = bot.summary_prompt | bot.model | StrOutputParser()
128
+ st.session_state.chat_summary = summarizer.invoke({'conversation': full_chat})
129
  except:
130
  pass
131
 
 
154
  st.markdown(f'<div class="chat-message bot-message">{formatted}</div>', unsafe_allow_html=True)
155
 
156
  audio_file = st.session_state.narrated_audio.get((q, a))
 
157
  if not audio_file:
158
  if st.button("🔊 Narrate", key=f"narrate_{idx}"):
159
  status_placeholder = st.empty()