Spaces:
Build error
Build error
Started setting up custom chat bot
Browse files- .gitignore +4 -0
- app.py +51 -0
- requirements.txt +5 -0
- src/__init__.py +0 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
secrets.toml
|
2 |
+
venv/
|
3 |
+
data/
|
4 |
+
.DS_Store
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from llama_index import VectorStoreIndex, ServiceContext
|
3 |
+
from llama_index.llms import OpenAI
|
4 |
+
import openai
|
5 |
+
from llama_index import SimpleDirectoryReader
|
6 |
+
|
7 |
+
|
8 |
+
# Setup OpenAI key and initialize message history
|
9 |
+
openai.api_key = st.secrets.openai_key
|
10 |
+
st.header("Chat with the Streamlit docs 💬 📚")
|
11 |
+
|
12 |
+
if "messages" not in st.session_state.keys(): # Initialize the chat message history
|
13 |
+
st.session_state.messages = [
|
14 |
+
{"role": "assistant", "content": "Ask me a question about Streamlit's open-source Python library!"}
|
15 |
+
]
|
16 |
+
|
17 |
+
|
18 |
+
@st.cache_resource(show_spinner=False)
|
19 |
+
def load_data():
|
20 |
+
with st.spinner(text="Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."):
|
21 |
+
reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
|
22 |
+
docs = reader.load_data()
|
23 |
+
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features."))
|
24 |
+
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
|
25 |
+
return index
|
26 |
+
|
27 |
+
|
28 |
+
def main():
|
29 |
+
index = load_data()
|
30 |
+
|
31 |
+
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
|
32 |
+
|
33 |
+
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
|
34 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
35 |
+
|
36 |
+
for message in st.session_state.messages: # Display the prior chat messages
|
37 |
+
with st.chat_message(message["role"]):
|
38 |
+
st.write(message["content"])
|
39 |
+
|
40 |
+
# If last message is not from assistant, generate a new response
|
41 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
42 |
+
with st.chat_message("assistant"):
|
43 |
+
with st.spinner("Thinking..."):
|
44 |
+
response = chat_engine.chat(prompt)
|
45 |
+
st.write(response.response)
|
46 |
+
message = {"role": "assistant", "content": response.response}
|
47 |
+
st.session_state.messages.append(message) # Add response to message history
|
48 |
+
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|
3 |
+
llama-index
|
4 |
+
nltk
|
5 |
+
pypdf
|
src/__init__.py
ADDED
File without changes
|