Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,12 @@ import streamlit as st
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
# YOUR API KEY
|
| 5 |
-
|
| 6 |
-
os.environ["OPENAI_API_KEY"] = API_KEY
|
| 7 |
|
| 8 |
from langchain_openai import ChatOpenAI
|
| 9 |
-
import tiktoken
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Simple knowledge base - no complex splitting
|
| 14 |
-
knowledge = """
|
| 15 |
SR University is located in Warangal, Telangana, India.
|
| 16 |
Computer Science program focuses on AI/ML, DSA, Java/Python, AWS/Azure, software engineering.
|
| 17 |
|
|
@@ -24,46 +20,49 @@ Internship tips:
|
|
| 24 |
3. Apply startups: AngelList, Y Combinator
|
| 25 |
4. Practice system design, behavioral interviews
|
| 26 |
5. Target: Google, Microsoft, Hyderabad/Bangalore startups
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 30 |
-
|
| 31 |
-
def rag_chat(query):
|
| 32 |
-
prompt = f"""
|
| 33 |
-
Use ONLY this context to answer:
|
| 34 |
-
{knowledge}
|
| 35 |
-
|
| 36 |
-
Question: {query}
|
| 37 |
-
|
| 38 |
-
Answer concisely and accurately:
|
| 39 |
-
"""
|
| 40 |
-
return llm.invoke(prompt).content
|
| 41 |
-
|
| 42 |
-
return rag_chat
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
|
|
|
| 50 |
if "messages" not in st.session_state:
|
| 51 |
st.session_state.messages = []
|
| 52 |
|
| 53 |
-
#
|
| 54 |
for message in st.session_state.messages:
|
| 55 |
with st.chat_message(message["role"]):
|
| 56 |
st.markdown(message["content"])
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
if
|
| 60 |
-
|
|
|
|
| 61 |
with st.chat_message("user"):
|
| 62 |
-
st.markdown(
|
| 63 |
|
|
|
|
| 64 |
with st.chat_message("assistant"):
|
| 65 |
-
with st.spinner(
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
st.markdown(response)
|
| 68 |
|
|
|
|
| 69 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
# YOUR API KEY
|
| 5 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
|
|
|
| 6 |
|
| 7 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 8 |
|
| 9 |
+
# Knowledge base
|
| 10 |
+
KNOWLEDGE = """
|
|
|
|
|
|
|
| 11 |
SR University is located in Warangal, Telangana, India.
|
| 12 |
Computer Science program focuses on AI/ML, DSA, Java/Python, AWS/Azure, software engineering.
|
| 13 |
|
|
|
|
| 20 |
3. Apply startups: AngelList, Y Combinator
|
| 21 |
4. Practice system design, behavioral interviews
|
| 22 |
5. Target: Google, Microsoft, Hyderabad/Bangalore startups
|
| 23 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
@st.cache_resource
|
| 26 |
+
def init_chatbot():
|
| 27 |
+
return ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
| 28 |
|
| 29 |
+
st.title("🧠 RAG Chatbot")
|
| 30 |
+
st.markdown("💡 Ask about SR University, AI internships, projects...")
|
| 31 |
|
| 32 |
+
chatbot = init_chatbot()
|
| 33 |
if "messages" not in st.session_state:
|
| 34 |
st.session_state.messages = []
|
| 35 |
|
| 36 |
+
# Display chat messages
|
| 37 |
for message in st.session_state.messages:
|
| 38 |
with st.chat_message(message["role"]):
|
| 39 |
st.markdown(message["content"])
|
| 40 |
|
| 41 |
+
# Chat input
|
| 42 |
+
if query := st.chat_input("Type your question here..."):
|
| 43 |
+
# Add user message
|
| 44 |
+
st.session_state.messages.append({"role": "user", "content": query})
|
| 45 |
with st.chat_message("user"):
|
| 46 |
+
st.markdown(query)
|
| 47 |
|
| 48 |
+
# Generate response
|
| 49 |
with st.chat_message("assistant"):
|
| 50 |
+
with st.spinner('Thinking...'):
|
| 51 |
+
prompt = f"""Use ONLY this context to answer:
|
| 52 |
+
|
| 53 |
+
{KNOWLEDGE}
|
| 54 |
+
|
| 55 |
+
Question: {query}
|
| 56 |
+
|
| 57 |
+
Answer:"""
|
| 58 |
+
|
| 59 |
+
response = chatbot.invoke(prompt).content
|
| 60 |
st.markdown(response)
|
| 61 |
|
| 62 |
+
# Add assistant response to chat history
|
| 63 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 64 |
+
|
| 65 |
+
# Clear chat button
|
| 66 |
+
if st.button("🗑️ Clear Chat"):
|
| 67 |
+
st.session_state.messages = []
|
| 68 |
+
st.rerun()
|