Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ from langchain_community.chat_message_histories import StreamlitChatMessageHisto
|
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
load_dotenv()
|
| 11 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
|
| 13 |
-
# Initialize
|
| 14 |
llm = ChatOpenAI(
|
| 15 |
openai_api_key=OPENAI_API_KEY,
|
| 16 |
model_name="gpt-4o-mini",
|
|
@@ -18,34 +18,31 @@ llm = ChatOpenAI(
|
|
| 18 |
max_tokens=50,
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
st.set_page_config(page_title="LangChain Chatbot
|
| 23 |
-
st.title("🤖 LangChain Chatbot
|
| 24 |
|
| 25 |
-
#
|
| 26 |
history = StreamlitChatMessageHistory(key="chat_messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
for msg in history.messages:
|
| 28 |
if msg.type == "human":
|
| 29 |
st.chat_message("user").write(msg.content)
|
| 30 |
-
|
| 31 |
st.chat_message("assistant").write(msg.content)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
memory = ConversationBufferMemory(chat_memory=history, return_messages=True)
|
| 35 |
-
|
| 36 |
-
# Create conversation chain
|
| 37 |
-
conversation = ConversationChain(llm=llm, memory=memory)
|
| 38 |
-
|
| 39 |
-
# Chat input
|
| 40 |
if prompt := st.chat_input("Say something..."):
|
| 41 |
-
# Display
|
| 42 |
-
history.add_user_message(prompt)
|
| 43 |
st.chat_message("user").write(prompt)
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Display and store assistant message
|
| 50 |
-
history.add_ai_message(response)
|
| 51 |
st.chat_message("assistant").write(response)
|
|
|
|
|
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
| 8 |
|
| 9 |
+
# Load API key
|
| 10 |
load_dotenv()
|
| 11 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
|
| 13 |
+
# Initialize model
|
| 14 |
llm = ChatOpenAI(
|
| 15 |
openai_api_key=OPENAI_API_KEY,
|
| 16 |
model_name="gpt-4o-mini",
|
|
|
|
| 18 |
max_tokens=50,
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Page config
|
| 22 |
+
st.set_page_config(page_title="LangChain Chatbot")
|
| 23 |
+
st.title("🤖 LangChain Chatbot (OpenAI)")
|
| 24 |
|
| 25 |
+
# History and memory
|
| 26 |
history = StreamlitChatMessageHistory(key="chat_messages")
|
| 27 |
+
memory = ConversationBufferMemory(chat_memory=history, return_messages=True)
|
| 28 |
+
conversation = ConversationChain(llm=llm, memory=memory)
|
| 29 |
+
|
| 30 |
+
# 💬 Display chat history (before new user input)
|
| 31 |
for msg in history.messages:
|
| 32 |
if msg.type == "human":
|
| 33 |
st.chat_message("user").write(msg.content)
|
| 34 |
+
elif msg.type == "ai":
|
| 35 |
st.chat_message("assistant").write(msg.content)
|
| 36 |
|
| 37 |
+
# 🔥 Chat input AFTER displaying history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if prompt := st.chat_input("Say something..."):
|
| 39 |
+
# Display + store user message
|
|
|
|
| 40 |
st.chat_message("user").write(prompt)
|
| 41 |
+
history.add_user_message(prompt)
|
| 42 |
|
| 43 |
+
# Response with word limit
|
| 44 |
+
response = conversation.predict(input=f"{prompt}\n\nPlease respond in complete sentence in no more than 50 words.")
|
| 45 |
+
|
| 46 |
+
# Display + store assistant message
|
|
|
|
|
|
|
| 47 |
st.chat_message("assistant").write(response)
|
| 48 |
+
history.add_ai_message(response)
|