Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -85,26 +85,35 @@ def get_llm():
|
|
| 85 |
temperature=0.3,
|
| 86 |
)
|
| 87 |
|
| 88 |
-
# ββ Chat β history is list of
|
| 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
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
lc_messages.append(HumanMessage(content=user_message))
|
| 99 |
|
| 100 |
response = get_llm().invoke(lc_messages)
|
| 101 |
-
return "", history + [
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 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())
|