cheesyFishes commited on
Commit
8291b7d
β€’
1 Parent(s): 775ab85

update to llama-index v0.6.13

Browse files
app.py CHANGED
@@ -1,40 +1,57 @@
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
21
 
22
 
23
  @st.cache_data(max_entries=200, persist=True)
24
  def query_index(_index, query_text):
25
- response = _index.query(query_text)
26
  return str(response)
27
 
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")
35
  if api_key:
36
- os.environ['OPENAI_API_KEY'] = api_key
37
- index = initialize_index(index_name, documents_folder)
38
 
39
 
40
  if index is None:
@@ -45,11 +62,14 @@ text = st.text_input("Query text:", value="What did the author do growing up?")
45
  if st.button("Run Query") and text is not None:
46
  response = query_index(index, text)
47
  st.markdown(response)
48
-
49
  llm_col, embed_col = st.columns(2)
50
  with llm_col:
51
- st.markdown(f"LLM Tokens Used: {index.service_context.llm_predictor._last_token_usage}")
52
-
53
- with embed_col:
54
- st.markdown(f"Embedding Tokens Used: {index.service_context.embed_model._last_token_usage}")
55
 
 
 
 
 
 
1
  import os
2
  import streamlit as st
3
+ from llama_index import (
4
+ GPTVectorStoreIndex,
5
+ SimpleDirectoryReader,
6
+ ServiceContext,
7
+ StorageContext,
8
+ LLMPredictor,
9
+ load_index_from_storage,
10
+ )
11
+ from langchain.chat_models import ChatOpenAI
12
 
13
+ index_name = "./saved_index"
14
  documents_folder = "./documents"
15
 
16
+
17
  @st.cache_resource
18
  def initialize_index(index_name, documents_folder):
19
+ llm_predictor = LLMPredictor(
20
+ llm=ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
21
+ )
22
  service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
23
  if os.path.exists(index_name):
24
+ index = load_index_from_storage(
25
+ StorageContext.from_defaults(persist_dir=index_name),
26
+ service_context=service_context,
27
+ )
28
  else:
29
  documents = SimpleDirectoryReader(documents_folder).load_data()
30
+ index = GPTVectorStoreIndex.from_documents(
31
+ documents, service_context=service_context
32
+ )
33
+ index.storage_context.persist(persist_dir=index_name)
34
 
35
  return index
36
 
37
 
38
  @st.cache_data(max_entries=200, persist=True)
39
  def query_index(_index, query_text):
40
+ response = _index.as_query_engine().query(query_text)
41
  return str(response)
42
 
43
 
44
  st.title("πŸ¦™ Llama Index Demo πŸ¦™")
45
  st.header("Welcome to the Llama Index Streamlit Demo")
46
+ st.write(
47
+ "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 gpt-3.5-turbo. You can read more about Llama Index and how this works in [our docs!](https://gpt-index.readthedocs.io/en/latest/index.html)"
48
+ )
49
 
50
  index = None
51
  api_key = st.text_input("Enter your OpenAI API key here:", type="password")
52
  if api_key:
53
+ os.environ["OPENAI_API_KEY"] = api_key
54
+ index = initialize_index(index_name, documents_folder)
55
 
56
 
57
  if index is None:
 
62
  if st.button("Run Query") and text is not None:
63
  response = query_index(index, text)
64
  st.markdown(response)
65
+
66
  llm_col, embed_col = st.columns(2)
67
  with llm_col:
68
+ st.markdown(
69
+ f"LLM Tokens Used: {index.service_context.llm_predictor._last_token_usage}"
70
+ )
 
71
 
72
+ with embed_col:
73
+ st.markdown(
74
+ f"Embedding Tokens Used: {index.service_context.embed_model._last_token_usage}"
75
+ )
index.json DELETED
The diff for this file is too large to render. See raw diff
 
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
- langchain==0.0.123
2
- llama-index==0.5.1
3
  streamlit==1.19.0
 
1
+ langchain==0.0.154
2
+ llama-index==0.6.13
3
  streamlit==1.19.0
saved_index/docstore.json ADDED
The diff for this file is too large to render. See raw diff
 
saved_index/index_store.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"index_store/data": {"cab2dd05-1512-4926-a828-be9d7324dc45": {"__type__": "vector_store", "__data__": {"index_id": "cab2dd05-1512-4926-a828-be9d7324dc45", "summary": null, "nodes_dict": {"f3bd7381-b54c-41f3-8291-a08e68673084": "f3bd7381-b54c-41f3-8291-a08e68673084", "a44b225d-992a-4778-a942-4e49093869a1": "a44b225d-992a-4778-a942-4e49093869a1", "63bf8e58-048d-4a3d-9f57-ade12c9a1a2d": "63bf8e58-048d-4a3d-9f57-ade12c9a1a2d", "4bb75a66-54eb-48fc-b7d3-3419865620a8": "4bb75a66-54eb-48fc-b7d3-3419865620a8", "960a0805-66b8-4294-a222-d65b68af53ed": "960a0805-66b8-4294-a222-d65b68af53ed", "44246830-1b06-4370-9ed5-32d5e81f296d": "44246830-1b06-4370-9ed5-32d5e81f296d", "d1445983-9afd-4032-bd25-2dfa42c3f1f6": "d1445983-9afd-4032-bd25-2dfa42c3f1f6", "fa8fea99-ec02-466d-a0db-9918823f6d1f": "fa8fea99-ec02-466d-a0db-9918823f6d1f", "5384bc5c-c5b8-4151-8473-36edcfbecae6": "5384bc5c-c5b8-4151-8473-36edcfbecae6", "23374f34-97ba-42b0-9abb-9d3e784417a8": "23374f34-97ba-42b0-9abb-9d3e784417a8", "eadcdd6e-1590-42af-bbd2-b9c00bd6e057": "eadcdd6e-1590-42af-bbd2-b9c00bd6e057", "d8400f95-51f1-4282-aaec-92dbbb635acf": "d8400f95-51f1-4282-aaec-92dbbb635acf", "75b2baab-761e-438f-9fd8-39ccb855c8e4": "75b2baab-761e-438f-9fd8-39ccb855c8e4", "d4a45a65-3de2-45b8-b236-ec2f3ac9b21b": "d4a45a65-3de2-45b8-b236-ec2f3ac9b21b", "7ece5730-d2e6-44a8-baca-f9cc0f5afe26": "7ece5730-d2e6-44a8-baca-f9cc0f5afe26", "c1aaf2ab-5466-4424-ad44-27c58368b2e7": "c1aaf2ab-5466-4424-ad44-27c58368b2e7", "d0b1c9a3-5d8b-4f74-a17c-9a5c21d343da": "d0b1c9a3-5d8b-4f74-a17c-9a5c21d343da", "23a7c0a0-eecd-4d0c-a2fd-d7e9d1d0d0f5": "23a7c0a0-eecd-4d0c-a2fd-d7e9d1d0d0f5", "bc3f54c4-bf8d-46bb-adbd-0f692b2b215a": "bc3f54c4-bf8d-46bb-adbd-0f692b2b215a", "b87356e4-99b4-4bd2-86e9-4233e662cc62": "b87356e4-99b4-4bd2-86e9-4233e662cc62", "88937b63-2017-458e-be11-e351b5a8af99": "88937b63-2017-458e-be11-e351b5a8af99", "c00da86c-923e-4b66-92ad-6edd4034f37a": "c00da86c-923e-4b66-92ad-6edd4034f37a", "8af21ed2-e832-4019-accb-79ca6d05b5bb": "8af21ed2-e832-4019-accb-79ca6d05b5bb", "7f61ffbd-83d9-49ea-8093-1fb1532b00a1": "7f61ffbd-83d9-49ea-8093-1fb1532b00a1"}, "doc_id_dict": {"563fad24-4559-490f-9eb5-ed29ffdc9402": ["f3bd7381-b54c-41f3-8291-a08e68673084", "a44b225d-992a-4778-a942-4e49093869a1", "63bf8e58-048d-4a3d-9f57-ade12c9a1a2d", "4bb75a66-54eb-48fc-b7d3-3419865620a8", "960a0805-66b8-4294-a222-d65b68af53ed", "44246830-1b06-4370-9ed5-32d5e81f296d", "d1445983-9afd-4032-bd25-2dfa42c3f1f6", "fa8fea99-ec02-466d-a0db-9918823f6d1f", "5384bc5c-c5b8-4151-8473-36edcfbecae6", "23374f34-97ba-42b0-9abb-9d3e784417a8", "eadcdd6e-1590-42af-bbd2-b9c00bd6e057", "d8400f95-51f1-4282-aaec-92dbbb635acf", "75b2baab-761e-438f-9fd8-39ccb855c8e4", "d4a45a65-3de2-45b8-b236-ec2f3ac9b21b", "7ece5730-d2e6-44a8-baca-f9cc0f5afe26", "c1aaf2ab-5466-4424-ad44-27c58368b2e7", "d0b1c9a3-5d8b-4f74-a17c-9a5c21d343da", "23a7c0a0-eecd-4d0c-a2fd-d7e9d1d0d0f5", "bc3f54c4-bf8d-46bb-adbd-0f692b2b215a", "b87356e4-99b4-4bd2-86e9-4233e662cc62", "88937b63-2017-458e-be11-e351b5a8af99", "c00da86c-923e-4b66-92ad-6edd4034f37a", "8af21ed2-e832-4019-accb-79ca6d05b5bb", "7f61ffbd-83d9-49ea-8093-1fb1532b00a1"]}, "embeddings_dict": {}}}}}
saved_index/vector_store.json ADDED
The diff for this file is too large to render. See raw diff