MrSimple01 commited on
Commit
fd5733d
Β·
verified Β·
1 Parent(s): 241a044

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -6
app.py CHANGED
@@ -85,26 +85,35 @@ def get_llm():
85
  temperature=0.3,
86
  )
87
 
88
- # ── Chat β€” history is list of (user_str, bot_str) tuples ─────────────────────
89
 
90
  def chat(user_message: str, history: list):
91
  if not user_message.strip():
92
  return "", history
93
 
94
  lc_messages = [SystemMessage(content=SYSTEM_PROMPT)]
95
- for human, assistant in history:
96
- lc_messages.append(HumanMessage(content=human))
97
- lc_messages.append(AIMessage(content=assistant))
 
 
98
  lc_messages.append(HumanMessage(content=user_message))
99
 
100
  response = get_llm().invoke(lc_messages)
101
- return "", history + [(user_message, response.content)]
 
 
 
102
 
103
 
104
  def use_sample(question: str, history: list):
105
  clean = question.split(" ", 1)[-1]
106
  return chat(clean, history)
107
 
 
 
 
 
108
  # ── UI ────────────────────────────────────────────────────────────────────────
109
 
110
  with gr.Blocks(title="ShopAssist FAQ") as demo:
@@ -141,7 +150,7 @@ with gr.Blocks(title="ShopAssist FAQ") as demo:
141
 
142
  txt.submit(chat, [txt, chatbot], [txt, chatbot])
143
  send.click(chat, [txt, chatbot], [txt, chatbot])
144
- clear.click(lambda: ([], ""), outputs=[chatbot, txt])
145
 
146
  if __name__ == "__main__":
147
  demo.launch(theme=gr.themes.Soft())
 
85
  temperature=0.3,
86
  )
87
 
88
+ # ── Chat β€” history is list of {"role": ..., "content": ...} dicts ─────────────
89
 
90
  def chat(user_message: str, history: list):
91
  if not user_message.strip():
92
  return "", history
93
 
94
  lc_messages = [SystemMessage(content=SYSTEM_PROMPT)]
95
+ for msg in history:
96
+ if msg["role"] == "user":
97
+ lc_messages.append(HumanMessage(content=msg["content"]))
98
+ elif msg["role"] == "assistant":
99
+ lc_messages.append(AIMessage(content=msg["content"]))
100
  lc_messages.append(HumanMessage(content=user_message))
101
 
102
  response = get_llm().invoke(lc_messages)
103
+ return "", history + [
104
+ {"role": "user", "content": user_message},
105
+ {"role": "assistant", "content": response.content},
106
+ ]
107
 
108
 
109
  def use_sample(question: str, history: list):
110
  clean = question.split(" ", 1)[-1]
111
  return chat(clean, history)
112
 
113
+
114
+ def clear_chat():
115
+ return [], ""
116
+
117
  # ── UI ────────────────────────────────────────────────────────────────────────
118
 
119
  with gr.Blocks(title="ShopAssist FAQ") as demo:
 
150
 
151
  txt.submit(chat, [txt, chatbot], [txt, chatbot])
152
  send.click(chat, [txt, chatbot], [txt, chatbot])
153
+ clear.click(clear_chat, outputs=[chatbot, txt])
154
 
155
  if __name__ == "__main__":
156
  demo.launch(theme=gr.themes.Soft())