andreped commited on
Commit
08606a2
·
1 Parent(s): 9040eb5

Developed working app

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. README.md +18 -1
  3. app.py +35 -11
.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  secrets.toml
2
  venv/
3
  data/
4
- .DS_Store
 
 
1
  secrets.toml
2
  venv/
3
  data/
4
+ .DS_Store
5
+ config.json
README.md CHANGED
@@ -1 +1,18 @@
1
- # custom-chatbot-streamlit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # custom-chatbot-streamlit
2
+
3
+ This application demonstrates how to setup a simple ChatBot with Azure OpenAI and StreamLit.
4
+ The ChatBot enables you to talk with your own data - in this case, to learn about André's research.
5
+
6
+ ## Getting Started
7
+
8
+ 1.Setup virtual environment and install dependencies:
9
+ ```
10
+ python -m venv venv/
11
+ source venv/bin/activate
12
+ pip install -r requirements.txt
13
+ ```
14
+
15
+ 2. Download test data
16
+
17
+ ##
18
+
app.py CHANGED
@@ -1,27 +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
 
 
1
  import streamlit as st
2
+ from llama_index import VectorStoreIndex, ServiceContext, set_global_service_context
3
+ from llama_index.llms import AzureOpenAI
4
+ from llama_index.embeddings import OpenAIEmbedding
5
+ import json
6
+ import os
7
  from llama_index import SimpleDirectoryReader
8
 
9
 
10
+ # Load config values
11
+ with open(r'config.json') as config_file:
12
+ config_details = json.load(config_file)
13
+
14
+ # Initialize message history
15
+ st.header("Chat with André's research 💬 📚")
16
 
17
  if "messages" not in st.session_state.keys(): # Initialize the chat message history
18
  st.session_state.messages = [
19
+ {"role": "assistant", "content": "Ask me a question about André's research!"}
20
  ]
21
 
22
 
23
  @st.cache_resource(show_spinner=False)
24
  def load_data():
25
  with st.spinner(text="Loading and indexing the Streamlit docs – hang tight! This should take 1-2 minutes."):
26
+ documents = SimpleDirectoryReader(input_dir="./data", recursive=True).load_data()
27
+ llm = AzureOpenAI(
28
+ model="gpt-3.5-turbo",
29
+ engine="chatbot-streamlit",
30
+ temperature=0.5,
31
+ api_key=os.getenv("OPENAI_API_KEY"),
32
+ api_base=config_details['OPENAI_API_BASE'],
33
+ api_type="azure",
34
+ api_version=config_details['OPENAI_API_VERSION'],
35
+ 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."
36
+ )
37
+ # You need to deploy your own embedding model as well as your own chat completion model
38
+ embed_model = OpenAIEmbedding(
39
+ model="text-embedding-ada-002",
40
+ deployment_name="chatbot-streamlit-embedding",
41
+ api_key=os.getenv("OPENAI_API_KEY"),
42
+ api_base=config_details['OPENAI_API_BASE'],
43
+ api_type="azure",
44
+ api_version=config_details['OPENAI_API_VERSION'],
45
+ )
46
+ service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
47
+ set_global_service_context(service_context)
48
+ index = VectorStoreIndex.from_documents(documents) #, service_context=service_context)
49
  return index
50
 
51