Spaces:
Build error
Build error
| 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}") |