eagle0504 commited on
Commit
fe476d6
Β·
verified Β·
1 Parent(s): 008131c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from llama_index.llms.openai import OpenAI
4
+ from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
5
+
6
+ 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)
7
+ openai.api_key = st.secrets.openai_key
8
+ st.title("Chat with the Streamlit docs, powered by LlamaIndex πŸ’¬πŸ¦™")
9
+ 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="πŸ“ƒ")
10
+
11
+ if "messages" not in st.session_state.keys(): # Initialize the chat messages history
12
+ st.session_state.messages = [
13
+ {
14
+ "role": "assistant",
15
+ "content": "Ask me a question about Streamlit's open-source Python library!",
16
+ }
17
+ ]
18
+
19
+ @st.cache_resource(show_spinner=False)
20
+ def load_data():
21
+ reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
22
+ docs = reader.load_data()
23
+ Settings.llm = OpenAI(
24
+ model="gpt-3.5-turbo",
25
+ temperature=0.2,
26
+ system_prompt="""You are an expert on
27
+ the Streamlit Python library and your
28
+ job is to answer technical questions.
29
+ Assume that all questions are related
30
+ to the Streamlit Python library. Keep
31
+ your answers technical and based on
32
+ facts – do not hallucinate features.""",
33
+ )
34
+ index = VectorStoreIndex.from_documents(docs)
35
+ return index
36
+
37
+
38
+ index = load_data()
39
+
40
+ if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
41
+ st.session_state.chat_engine = index.as_chat_engine(
42
+ chat_mode="condense_question", verbose=True, streaming=True
43
+ )
44
+
45
+ if prompt := st.chat_input(
46
+ "Ask a question"
47
+ ): # Prompt for user input and save to chat history
48
+ st.session_state.messages.append({"role": "user", "content": prompt})
49
+
50
+ for message in st.session_state.messages: # Write message history to UI
51
+ with st.chat_message(message["role"]):
52
+ st.write(message["content"])
53
+
54
+ # If last message is not from assistant, generate a new response
55
+ if st.session_state.messages[-1]["role"] != "assistant":
56
+ with st.chat_message("assistant"):
57
+ response_stream = st.session_state.chat_engine.stream_chat(prompt)
58
+ st.write_stream(response_stream.response_gen)
59
+ message = {"role": "assistant", "content": response_stream.response}
60
+ # Add response to message history
61
+ st.session_state.messages.append(message)