cheesyFishes commited on
Commit
e947786
β€’
1 Parent(s): f9d2f98

Use chatgpt

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -1,18 +1,20 @@
1
  import os
2
  import streamlit as st
3
- from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader
 
4
 
5
  index_name = "./index.json"
6
  documents_folder = "./documents"
7
 
8
-
9
  @st.cache_resource
10
  def initialize_index(index_name, documents_folder):
 
 
11
  if os.path.exists(index_name):
12
- index = GPTSimpleVectorIndex.load_from_disk(index_name)
13
  else:
14
  documents = SimpleDirectoryReader(documents_folder).load_data()
15
- index = GPTSimpleVectorIndex.from_documents(documents)
16
  index.save_to_disk(index_name)
17
 
18
  return index
@@ -26,7 +28,7 @@ def query_index(_index, query_text):
26
 
27
  st.title("πŸ¦™ Llama Index Demo πŸ¦™")
28
  st.header("Welcome to the Llama Index Streamlit Demo")
29
- st.write("Enter a query about Paul Graham's essays. You can check out the original essay [here](https://raw.githubusercontent.com/jerryjliu/llama_index/main/examples/paul_graham_essay/data/paul_graham_essay.txt)")
30
 
31
  index = None
32
  api_key = st.text_input("Enter your OpenAI API key here:", type="password")
 
1
  import os
2
  import streamlit as st
3
+ from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, ServiceContext
4
+ from llama_index.llm_predictor.chatgpt import ChatGPTLLMPredictor
5
 
6
  index_name = "./index.json"
7
  documents_folder = "./documents"
8
 
 
9
  @st.cache_resource
10
  def initialize_index(index_name, documents_folder):
11
+ llm_predictor = ChatGPTLLMPredictor()
12
+ service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
13
  if os.path.exists(index_name):
14
+ index = GPTSimpleVectorIndex.load_from_disk(index_name, service_context=service_context)
15
  else:
16
  documents = SimpleDirectoryReader(documents_folder).load_data()
17
+ index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
18
  index.save_to_disk(index_name)
19
 
20
  return index
 
28
 
29
  st.title("πŸ¦™ Llama Index Demo πŸ¦™")
30
  st.header("Welcome to the Llama Index Streamlit Demo")
31
+ st.write("Enter a query about Paul Graham's essays. You can check out the original essay [here](https://raw.githubusercontent.com/jerryjliu/llama_index/main/examples/paul_graham_essay/data/paul_graham_essay.txt). Your query will be answered using the essay as context, using embeddings from text-ada-002 and LLM completions from ChatGPT. You can read more about Llama Index and how this works in [our docs!](https://gpt-index.readthedocs.io/en/latest/index.html)")
32
 
33
  index = None
34
  api_key = st.text_input("Enter your OpenAI API key here:", type="password")