kshitijk
commited on
Commit
•
7d5df89
1
Parent(s):
cc1974f
Add large data context index
Browse files- .gitattributes +1 -1
- .gitignore +1 -0
- app.py +41 -51
- storage/.gitattributes +1 -0
- storage/open_ai_embedding_data/default__vector_store.json +3 -0
- storage/open_ai_embedding_data/docstore.json +3 -0
- storage/open_ai_embedding_data/graph_store.json +3 -0
- storage/open_ai_embedding_data/image__vector_store.json +3 -0
- storage/open_ai_embedding_data/index_store.json +3 -0
- storage/open_ai_embedding_data_large/default__vector_store.json +3 -0
- storage/open_ai_embedding_data_large/docstore.json +3 -0
- storage/open_ai_embedding_data_large/graph_store.json +3 -0
- storage/open_ai_embedding_data_large/image__vector_store.json +3 -0
- storage/open_ai_embedding_data_large/index_store.json +3 -0
.gitattributes
CHANGED
@@ -33,4 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
CHANGED
@@ -1,13 +1,19 @@
|
|
1 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
from llama_index.core import(SimpleDirectoryReader,
|
4 |
VectorStoreIndex, StorageContext,
|
5 |
Settings,set_global_tokenizer)
|
6 |
-
|
7 |
-
from llama_index.llms.llama_cpp.llama_utils import (
|
8 |
-
messages_to_prompt,
|
9 |
-
completion_to_prompt,
|
10 |
-
)
|
11 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
12 |
from transformers import AutoTokenizer, BitsAndBytesConfig
|
13 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
@@ -16,17 +22,15 @@ import logging
|
|
16 |
import sys
|
17 |
import streamlit as st
|
18 |
from llama_index.core import load_index_from_storage
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
25 |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
|
26 |
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
27 |
-
set_global_tokenizer(
|
28 |
-
AutoTokenizer.from_pretrained("NousResearch/Llama-2-13b-chat-hf").encode
|
29 |
-
)
|
30 |
|
31 |
|
32 |
def getDocs(doc_path="./data/"):
|
@@ -34,46 +38,29 @@ def getDocs(doc_path="./data/"):
|
|
34 |
return documents
|
35 |
|
36 |
|
37 |
-
def getVectorIndex(
|
38 |
Settings.chunk_size = 512
|
39 |
index_set = {}
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
return cur_index
|
45 |
|
46 |
-
|
47 |
-
def getLLM():
|
48 |
-
|
49 |
-
model_path = "NousResearch/Llama-2-13b-chat-hf"
|
50 |
-
# model_path = "NousResearch/Llama-2-7b-chat-hf"
|
51 |
-
|
52 |
-
llm = HuggingFaceLLM(
|
53 |
-
context_window=3900,
|
54 |
-
max_new_tokens=256,
|
55 |
-
# generate_kwargs={"temperature": 0.25, "do_sample": False},
|
56 |
-
tokenizer_name=model_path,
|
57 |
-
model_name=model_path,
|
58 |
-
device_map=0,
|
59 |
-
tokenizer_kwargs={"max_length": 2048},
|
60 |
-
# uncomment this if using CUDA to reduce memory usage
|
61 |
-
model_kwargs={"torch_dtype": torch.float16,
|
62 |
-
"quantization_config": default_bnb_config,
|
63 |
-
}
|
64 |
-
)
|
65 |
-
return llm
|
66 |
-
|
67 |
-
|
68 |
def getQueryEngine(index):
|
69 |
-
query_engine = index.as_chat_engine(
|
70 |
return query_engine
|
71 |
|
72 |
-
def getEmbedModel():
|
73 |
-
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
74 |
-
return embed_model
|
75 |
-
|
76 |
-
|
77 |
|
78 |
|
79 |
|
@@ -94,14 +81,17 @@ if "messages" not in st.session_state.keys(): # Initialize the chat messages his
|
|
94 |
|
95 |
@st.cache_resource(show_spinner=False)
|
96 |
def load_data():
|
97 |
-
index = getVectorIndex(
|
98 |
return index
|
99 |
-
|
100 |
-
|
101 |
index = load_data()
|
|
|
|
|
|
|
102 |
|
103 |
if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
|
104 |
-
st.session_state.chat_engine = index.as_chat_engine(
|
105 |
|
106 |
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
|
107 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
1 |
from pathlib import Path
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
openai.api_key = os.getenv("OAI_KEY")
|
5 |
+
from llama_index.llms.openai import OpenAI
|
6 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
7 |
+
import nest_asyncio
|
8 |
+
|
9 |
+
nest_asyncio.apply()
|
10 |
+
|
11 |
+
|
12 |
|
13 |
from llama_index.core import(SimpleDirectoryReader,
|
14 |
VectorStoreIndex, StorageContext,
|
15 |
Settings,set_global_tokenizer)
|
16 |
+
|
|
|
|
|
|
|
|
|
17 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
18 |
from transformers import AutoTokenizer, BitsAndBytesConfig
|
19 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
|
|
22 |
import sys
|
23 |
import streamlit as st
|
24 |
from llama_index.core import load_index_from_storage
|
25 |
+
|
26 |
+
|
27 |
+
Settings.llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0.2)
|
28 |
+
Settings.embed_model = OpenAIEmbedding(
|
29 |
+
model="text-embedding-3-large", embed_batch_size=100
|
30 |
+
)
|
31 |
+
|
32 |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
|
33 |
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
def getDocs(doc_path="./data/"):
|
|
|
38 |
return documents
|
39 |
|
40 |
|
41 |
+
def getVectorIndex():
|
42 |
Settings.chunk_size = 512
|
43 |
index_set = {}
|
44 |
+
if os.path.isdir(f"./storage/open_ai_embedding_data_large"):
|
45 |
+
print("Index already exists")
|
46 |
+
storage_context = StorageContext.from_defaults(
|
47 |
+
persist_dir=f"./storage/open_ai_embedding_data_large"
|
48 |
+
)
|
49 |
+
cur_index = load_index_from_storage(
|
50 |
+
storage_context,
|
51 |
+
)
|
52 |
+
else:
|
53 |
+
print("Index does not exist, creating new index")
|
54 |
+
docs = getDocs()
|
55 |
+
storage_context = StorageContext.from_defaults()
|
56 |
+
cur_index = VectorStoreIndex.from_documents(docs, storage_context=storage_context)
|
57 |
+
storage_context.persist(persist_dir=f"./storage/open_ai_embedding_data_large")
|
58 |
return cur_index
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
def getQueryEngine(index):
|
61 |
+
query_engine = index.as_chat_engine()
|
62 |
return query_engine
|
63 |
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
66 |
|
|
|
81 |
|
82 |
@st.cache_resource(show_spinner=False)
|
83 |
def load_data():
|
84 |
+
index = getVectorIndex()
|
85 |
return index
|
86 |
+
import time
|
87 |
+
s_time = time.time()
|
88 |
index = load_data()
|
89 |
+
e_time = time.time()
|
90 |
+
|
91 |
+
print(f"It took {e_time - s_time} to load index")
|
92 |
|
93 |
if "chat_engine" not in st.session_state.keys(): # Initialize the chat engine
|
94 |
+
st.session_state.chat_engine = index.as_chat_engine(chat_mode="condense_plus_context", verbose=True)
|
95 |
|
96 |
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
|
97 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
storage/.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*json filter=lfs diff=lfs merge=lfs -text
|
storage/open_ai_embedding_data/default__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:87956943cb8e0633d9df6a98d31a12c9528901114a79b39c179734999cee7163
|
3 |
+
size 244449202
|
storage/open_ai_embedding_data/docstore.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5a7d9222ba808d2bf326098e84b7b959ba0104c923ce7f87c782ecfd93404325
|
3 |
+
size 29962555
|
storage/open_ai_embedding_data/graph_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e0a77744010862225c69da83c585f4f8a42fd551b044ce530dbb1eb6e16742c
|
3 |
+
size 18
|
storage/open_ai_embedding_data/image__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d17ed74c1649a438e518a8dc56a7772913dfe1ea7a7605bce069c63872431455
|
3 |
+
size 72
|
storage/open_ai_embedding_data/index_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cd52cfd6aba4fb5c0774b7e3d38ddcda21e0cf5344a86c8eaf7c8690bb451bcd
|
3 |
+
size 589927
|
storage/open_ai_embedding_data_large/default__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4ad49abac9bff5c529bb2446b985e7ae14a74328d6d2293f6d421326b3851538
|
3 |
+
size 487945734
|
storage/open_ai_embedding_data_large/docstore.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3983b645d0aefc3d92158d573cb9bc4d3f79077a77066e140d8e725dd7e085b5
|
3 |
+
size 29962555
|
storage/open_ai_embedding_data_large/graph_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e0a77744010862225c69da83c585f4f8a42fd551b044ce530dbb1eb6e16742c
|
3 |
+
size 18
|
storage/open_ai_embedding_data_large/image__vector_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d17ed74c1649a438e518a8dc56a7772913dfe1ea7a7605bce069c63872431455
|
3 |
+
size 72
|
storage/open_ai_embedding_data_large/index_store.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d6a7b6dad9b8f418dfd26132e54203b8dca1374dc8e8c3199d5e9d001816f3cf
|
3 |
+
size 589927
|