File size: 1,526 Bytes
1da3761
54a011e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
import os

# Set your OpenAI API Key
os.environ["OPENAI_API_KEY"] = "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"

# 🌐 OpenRouter base URL
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"

# πŸ€– Choose model (you can change this)
MODEL_NAME = "openai/gpt-oss-120b:free"

# Initialize LLM
llm = ChatOpenAI(
    model=MODEL_NAME,
    temperature=0.7,
    base_url=OPENROUTER_BASE_URL
)


# Memory (stores conversation)
memory = ConversationBufferMemory()

# Conversation chain
conversation = ConversationChain(
    llm=llm,
    memory=memory,
    verbose=True
)

# Streamlit UI
st.set_page_config(page_title="Simple Chatbot", page_icon="πŸ€–")
st.title("πŸ€– Simple LangChain Chatbot")

# Session state for chat history
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

# User input
user_input = st.text_input("You:", placeholder="Ask something...")

if user_input:
    # Get response from LangChain
    response = conversation.predict(input=user_input)

    # Store chat
    st.session_state.chat_history.append(("You", user_input))
    st.session_state.chat_history.append(("Bot", response))

# Display chat history
for role, text in st.session_state.chat_history:
    if role == "You":
        st.markdown(f"**πŸ§‘ You:** {text}")
    else:
        st.markdown(f"**πŸ€– Bot:** {text}")