Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from llama_index import VectorStoreIndex, ServiceContext, Document
|
3 |
+
from llama_index.llms import OpenAI
|
4 |
+
import openai
|
5 |
+
from llama_index import SimpleDirectoryReader
|
6 |
+
|
7 |
+
st.set_page_config(page_title="Chat with the Streamlit docs, powered by LlamaIndex", page_icon="🦙", layout="centered", initial_sidebar_state="auto", menu_items=None)
|
8 |
+
openai.api_key = st.secrets.openai_key
|
9 |
+
st.title("Chat with the Streamlit docs, powered by LlamaIndex 💬🦙")
|
10 |
+
st.info("Check out the full tutorial to build this app in our [blog post](https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/)", icon="📃")
|
11 |
+
|
12 |
+
if "messages" not in st.session_state.keys(): # Initialize the chat messages history
|
13 |
+
st.session_state.messages = [
|
14 |
+
{"role": "assistant", "content": "Ask me a question about Streamlit's open-source Python library!"}
|
15 |
+
]
|
16 |
+
|
17 |
+
@st.cache_resource(show_spinner=False)
|
18 |
+
def load_data():
|
19 |
+
with st.spinner(text="Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."):
|
20 |
+
reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
|
21 |
+
docs = reader.load_data()
|
22 |
+
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."))
|
23 |
+
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
|
24 |
+
return index
|
25 |
+
|
26 |
+
index = load_data()
|
27 |
+
# chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True, 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.")
|
28 |
+
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
|
29 |
+
|
30 |
+
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
|
31 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
32 |
+
|
33 |
+
for message in st.session_state.messages: # Display the prior chat messages
|
34 |
+
with st.chat_message(message["role"]):
|
35 |
+
st.write(message["content"])
|
36 |
+
|
37 |
+
# If last message is not from assistant, generate a new response
|
38 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
39 |
+
with st.chat_message("assistant"):
|
40 |
+
with st.spinner("Thinking..."):
|
41 |
+
response = chat_engine.chat(prompt)
|
42 |
+
st.write(response.response)
|
43 |
+
message = {"role": "assistant", "content": response.response}
|
44 |
+
st.session_state.messages.append(message) # Add response to message history
|