Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from langchain.schema import HumanMessage
|
| 4 |
+
from agent import chat_agent
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class Message:
|
| 9 |
+
actor: str
|
| 10 |
+
payload: str
|
| 11 |
+
|
| 12 |
+
USER = "user"
|
| 13 |
+
ASSISTANT = "ai"
|
| 14 |
+
MESSAGES = "messages"
|
| 15 |
+
|
| 16 |
+
# Setting up the UI page title and config
|
| 17 |
+
st.set_page_config(page_title="AI Chatbot", page_icon="🤖")
|
| 18 |
+
st.title("🤖 First AI Chatbot")
|
| 19 |
+
|
| 20 |
+
# ✅ Initialize chat history if not already present
|
| 21 |
+
# Initialize chat history
|
| 22 |
+
if MESSAGES not in st.session_state:
|
| 23 |
+
st.session_state[MESSAGES] = [Message(actor=ASSISTANT, payload="Hi! How can I help you?")]
|
| 24 |
+
|
| 25 |
+
msg: Message
|
| 26 |
+
for msg in st.session_state[MESSAGES]:
|
| 27 |
+
st.chat_message(msg.actor).write(msg.payload)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Input box for user message
|
| 31 |
+
if prompt := st.chat_input("Type your message..."):
|
| 32 |
+
st.session_state[MESSAGES].append(Message(actor=USER, payload=prompt))
|
| 33 |
+
st.chat_message(USER).write(prompt)
|
| 34 |
+
result = chat_agent.invoke({
|
| 35 |
+
"messages": [HumanMessage(content=prompt)]
|
| 36 |
+
})
|
| 37 |
+
response: str = result["messages"][-1].content
|
| 38 |
+
st.session_state[MESSAGES].append(Message(actor= ASSISTANT, payload= response))
|
| 39 |
+
st.chat_message(ASSISTANT).write(response)
|