Spaces:
Running
Running
File size: 1,346 Bytes
513b44d 61bec69 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
import os
# Set OpenRouter API Key and Base URL
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
os.environ['OPENAI_API_BASE'] = 'https://openrouter.ai/api/v1'
# Set up the model
llm = ChatOpenAI(
temperature=0.7,
model_name="deepseek/deepseek-chat-v3-0324:free" # correct model path
)
# Set up memory
if "memory" not in st.session_state:
st.session_state.memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory=st.session_state.memory,
verbose=False
)
# Streamlit UI
st.set_page_config(page_title="LLM Chatbot", page_icon="🤖")
st.title("Langchain Chatbot by Muhammad Izhan")
user_input = st.text_input("You:", key="input")
if user_input:
response = conversation.predict(input=user_input)
st.session_state.memory.chat_memory.add_user_message(user_input)
st.session_state.memory.chat_memory.add_ai_message(response)
st.write(f"**Bot:** {response}")
# Show chat history
if st.checkbox("Show Chat History"):
for message in st.session_state.memory.chat_memory.messages:
role = "You" if message.type == "human" else "Bot"
st.markdown(f"**{role}:** {message.content}")
|