File size: 1,513 Bytes
b42229b
513b44d
61bec69
 
 
 
b42229b
19c1ffe
61bec69
b42229b
 
 
f637ab2
 
61bec69
 
 
f637ab2
b42229b
19c1ffe
61bec69
 
b42229b
61bec69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42229b
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
46
47
48
import os
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

# Get API key from environment variables
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")

# Check if API key is available
if not OPENAI_API_KEY:
    st.error("πŸ”‘ API key not found. Please set OPENAI_API_KEY in Space secrets.")
    st.stop()

# Set up the model
llm = ChatOpenAI(
    temperature=0.7,
    model_name="deepseek/deepseek-chat-v3-0324:free",
    openai_api_key=OPENAI_API_KEY,  # Use the variable here
    openai_api_base="https://openrouter.ai/api/v1"
)

# Rest of your chatbot code...
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}")

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}")