Skip to content

Commit 3820c92

Browse files
Migrate to Llama Index 0.5.X (#9)
* Migrate to llama_index 0.5.X, update apps * Update README with spaces links
1 parent 5627a3d commit 3820c92

File tree

9 files changed

+69
-54
lines changed

9 files changed

+69
-54
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ There are two main example folders
4040
- loads text from the documents folder (using `st.cache_resource`, so it only loads once)
4141
- provides an input text-box and a button to run the query
4242
- the string response is displayed after it finishes
43-
- want to see this example in action? Check it out [here](https://llama-index.streamlit.app/)
43+
- want to see this example in action? Check it out [here](https://huggingface.co/spaces/llamaindex/llama_index_vector_demo)
4444

4545
- streamlit_sql_sandbox (runs on localhost:8501)
4646
- `streamlit run streamlit_demo.py`
4747
- creates a streamlit app using a local SQL database about restaurant inspections in San Francisco ([data sample](https://docs.google.com/spreadsheets/d/1Ag5DBIviYiuRrt2yr3nXmbPFV-FOg5fDH5SM3ZEDnpw/edit#gid=780513932))
4848
- The "Setup" tab allows you to configure various LLM and LLama Index settings
4949
- The "Llama Index" tab demos some basic Text2SQL capabilities using only Llama Index
5050
- The "Langchain+Llama Index" tab uses a custom langchain agent, and uses the SQL index from Llama Index as a tool during conversations.
51+
- Check out the huggingface space [here!](https://huggingface.co/spaces/llamaindex/llama_index_sql_sandbox)
52+
5153

5254
## Docker
5355
Each example contains a `Dockerfile`. You can run `docker build -t my_tag_name .` to build a python3.11-slim docker image inside your desired folder. It ends up being about 600MB-900MB depending on the example.

flask_react/index_server.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from multiprocessing import Lock
88
from multiprocessing.managers import BaseManager
9-
from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, Document
9+
from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, Document, ServiceContext
1010

1111
index = None
1212
stored_docs = {}
@@ -19,12 +19,13 @@
1919
def initialize_index():
2020
"""Create a new global index, or load one from the pre-set path."""
2121
global index, stored_docs
22-
22+
23+
service_context = ServiceContext.from_defaults(chunk_size_limit=512)
2324
with lock:
2425
if os.path.exists(index_name):
25-
index = GPTSimpleVectorIndex.load_from_disk(index_name)
26+
index = GPTSimpleVectorIndex.load_from_disk(index_name, service_context=service_context)
2627
else:
27-
index = GPTSimpleVectorIndex([])
28+
index = GPTSimpleVectorIndex([], service_context=service_context)
2829
index.save_to_disk(index_name)
2930
if os.path.exists(pkl_name):
3031
with open(pkl_name, "rb") as f:
@@ -44,11 +45,11 @@ def insert_into_index(doc_file_path, doc_id=None):
4445
document = SimpleDirectoryReader(input_files=[doc_file_path]).load_data()[0]
4546
if doc_id is not None:
4647
document.doc_id = doc_id
47-
48-
# Keep track of stored docs -- llama_index doesn't make this easy
49-
stored_docs[document.doc_id] = document.text[0:200] # only take the first 200 chars
5048

5149
with lock:
50+
# Keep track of stored docs -- llama_index doesn't make this easy
51+
stored_docs[document.doc_id] = document.text[0:200] # only take the first 200 chars
52+
5253
index.insert(document)
5354
index.save_to_disk(index_name)
5455

flask_react/launch_app.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ sleep 60
1010
# start the flask server
1111
python ./flask_demo.py &
1212

13+
# assumes you've ran npm install already (dockerfile does this during build)
1314
cd react_frontend && npm run build && serve -s build

flask_react/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Flask==2.2.3
22
Flask-Cors==3.0.10
3-
langchain==0.0.123
4-
llama-index==0.4.39
3+
langchain==0.0.128
4+
llama-index==0.5.4
55
PyPDF2==3.0.1
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
langchain==0.0.123
2-
llama-index==0.4.39
1+
langchain==0.0.128
2+
llama-index==0.5.4
33
streamlit==1.19.0
44
streamlit-chat==0.0.2.2

streamlit_sql_sandbox/streamlit_demo.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from langchain.agents import Tool, initialize_agent
77
from langchain.chains.conversation.memory import ConversationBufferMemory
88

9-
from llama_index import GPTSQLStructStoreIndex, LLMPredictor
9+
from llama_index import GPTSQLStructStoreIndex, LLMPredictor, ServiceContext
1010
from llama_index import SQLDatabase as llama_SQLDatabase
1111
from llama_index.indices.struct_store import SQLContextContainerBuilder
1212

@@ -19,16 +19,11 @@
1919
)
2020
from utils import get_sql_index_tool, get_llm
2121

22-
# NOTE: for local testing only, do NOT deploy with your key hardcoded
23-
# to use this for yourself, create a file called .streamlit/secrets.toml with your api key
24-
# Learn more about Streamlit on the docs: https://docs.streamlit.io/
25-
os.environ["OPENAI_API_KEY"] = st.secrets["openai_api_key"]
26-
2722

2823
@st.cache_resource
29-
def initialize_index(llm_name, model_temperature, table_context_dict, sql_path=DEFAULT_SQL_PATH):
24+
def initialize_index(llm_name, model_temperature, table_context_dict, api_key, sql_path=DEFAULT_SQL_PATH):
3025
"""Create the GPTSQLStructStoreIndex object."""
31-
llm = get_llm(llm_name, model_temperature)
26+
llm = get_llm(llm_name, model_temperature, api_key)
3227

3328
engine = create_engine(sql_path)
3429
sql_database = llama_SQLDatabase(engine)
@@ -37,23 +32,24 @@ def initialize_index(llm_name, model_temperature, table_context_dict, sql_path=D
3732
if table_context_dict is not None:
3833
context_builder = SQLContextContainerBuilder(sql_database, context_dict=table_context_dict)
3934
context_container = context_builder.build_context_container()
40-
35+
36+
service_context = ServiceContext.from_defaults(llm_predictor=LLMPredictor(llm=llm))
4137
index = GPTSQLStructStoreIndex([],
4238
sql_database=sql_database,
4339
sql_context_container=context_container,
44-
llm_predictor=LLMPredictor(llm=llm))
40+
service_context=service_context)
4541

4642
return index
4743

4844

4945
@st.cache_resource
50-
def initialize_chain(llm_name, model_temperature, lc_descrp, _sql_index):
46+
def initialize_chain(llm_name, model_temperature, lc_descrp, api_key, _sql_index):
5147
"""Create a (rather hacky) custom agent and sql_index tool."""
5248
sql_tool = Tool(name="SQL Index",
5349
func=get_sql_index_tool(_sql_index, _sql_index.sql_context_container.context_dict),
5450
description=lc_descrp)
5551

56-
llm = get_llm(llm_name, model_temperature)
52+
llm = get_llm(llm_name, model_temperature, api_key=api_key)
5753

5854
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
5955

@@ -64,19 +60,23 @@ def initialize_chain(llm_name, model_temperature, lc_descrp, _sql_index):
6460

6561
st.title("🦙 Llama Index SQL Sandbox 🦙")
6662
st.markdown((
67-
"This sandbox uses a sqlite database by default, containing information on health violations and inspections at restaurants in San Francisco.\n\n"
68-
"The database contains three tables - businesses, inspections, and violations.\n\n"
63+
"This sandbox uses a sqlite database by default, powered by [Llama Index](https://gpt-index.readthedocs.io/en/latest/index.html) ChatGPT, and LangChain.\n\n"
64+
"The database contains information on health violations and inspections at restaurants in San Francisco."
65+
"This data is spread across three tables - businesses, inspections, and violations.\n\n"
6966
"Using the setup page, you can adjust LLM settings, change the context for the SQL tables, and change the tool description for Langchain."
67+
"The other tabs will perform chatbot and text2sql operations.\n\n"
68+
"Read more about LlamaIndexes structured data support [here!](https://gpt-index.readthedocs.io/en/latest/guides/tutorials/sql_guide.html)"
7069
))
7170

7271

7372
setup_tab, llama_tab, lc_tab = st.tabs(["Setup", "Llama Index", "Langchain+Llama Index"])
7473

7574
with setup_tab:
7675
st.subheader("LLM Setup")
77-
model_temperature = st.slider("LLM Temperature", min_value=0.0, max_value=1.0, step=0.1)
76+
api_key = st.text_input("Enter your OpenAI API key here", type="password")
7877
llm_name = st.selectbox('Which LLM?', ["text-davinci-003", "gpt-3.5-turbo", "gpt-4"])
79-
78+
model_temperature = st.slider("LLM Temperature", min_value=0.0, max_value=1.0, step=0.1)
79+
8080
st.subheader("Table Setup")
8181
business_table_descrp = st.text_area("Business table description", value=DEFAULT_BUSINESS_TABLE_DESCRP)
8282
violations_table_descrp = st.text_area("Business table description", value=DEFAULT_VIOLATIONS_TABLE_DESCRP)
@@ -92,10 +92,10 @@ def initialize_chain(llm_name, model_temperature, lc_descrp, _sql_index):
9292
with llama_tab:
9393
st.subheader("Text2SQL with Llama Index")
9494
if st.button("Initialize Index", key="init_index_1"):
95-
st.session_state['llama_index'] = initialize_index(llm_name, model_temperature, table_context_dict if use_table_descrp else None)
95+
st.session_state['llama_index'] = initialize_index(llm_name, model_temperature, table_context_dict if use_table_descrp else None, api_key)
9696

9797
if "llama_index" in st.session_state:
98-
query_text = st.text_input("Query:")
98+
query_text = st.text_input("Query:", value="Which restaurant has the most violations?")
9999
if st.button("Run Query") and query_text:
100100
with st.spinner("Getting response..."):
101101
try:
@@ -119,11 +119,11 @@ def initialize_chain(llm_name, model_temperature, lc_descrp, _sql_index):
119119
st.subheader("Langchain + Llama Index SQL Demo")
120120

121121
if st.button("Initialize Agent"):
122-
st.session_state['llama_index'] = initialize_index(llm_name, model_temperature, table_context_dict if use_table_descrp else None)
123-
st.session_state['lc_agent'] = initialize_chain(llm_name, model_temperature, lc_descrp, st.session_state['llama_index'])
122+
st.session_state['llama_index'] = initialize_index(llm_name, model_temperature, table_context_dict if use_table_descrp else None, api_key)
123+
st.session_state['lc_agent'] = initialize_chain(llm_name, model_temperature, lc_descrp, api_key, st.session_state['llama_index'])
124124
st.session_state['chat_history'] = []
125125

126-
model_input = st.text_input("Message:")
126+
model_input = st.text_input("Message:", value="Which restaurant has the most violations?")
127127
if 'lc_agent' in st.session_state and st.button("Send"):
128128
model_input = "User: " + model_input
129129
st.session_state['chat_history'].append(model_input)
@@ -134,4 +134,4 @@ def initialize_chain(llm_name, model_temperature, lc_descrp, _sql_index):
134134
if 'chat_history' in st.session_state:
135135
for msg in st.session_state['chat_history']:
136136
st_message(msg.split("User: ")[-1], is_user="User: " in msg)
137-
137+

streamlit_sql_sandbox/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from langchain import OpenAI
23
from langchain.chat_models import ChatOpenAI
34

@@ -16,9 +17,9 @@ def run_sql_index_query(query_text):
1617
return run_sql_index_query
1718

1819

19-
20-
def get_llm(llm_name, model_temperature):
20+
def get_llm(llm_name, model_temperature, api_key):
21+
os.environ['OPENAI_API_KEY'] = api_key
2122
if llm_name == "text-davinci-003":
2223
return OpenAI(temperature=model_temperature, model_name=llm_name)
2324
else:
24-
return ChatOpenAI(temperature=model_temperature, model_name=llm_name)
25+
return ChatOpenAI(temperature=model_temperature, model_name=llm_name)

streamlit_vector/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
langchain==0.0.123
2-
llama-index==0.4.39
1+
langchain==0.0.128
2+
llama-index==0.5.4
33
streamlit==1.19.0

streamlit_vector/streamlit_demo.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import os
2-
32
import streamlit as st
4-
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader
5-
6-
# NOTE: for local testing only, do NOT deploy with your key hardcoded
7-
# to use this for yourself, create a file called .streamlit/secrets.toml with your api key
8-
# Learn more about Streamlit on the docs: https://docs.streamlit.io/
9-
os.environ["OPENAI_API_KEY"] = st.secrets["openai_api_key"]
3+
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, ServiceContext
4+
from llama_index.llm_predictor.chatgpt import ChatGPTLLMPredictor
105

116

127
index_name = "./index.json"
@@ -15,11 +10,13 @@
1510

1611
@st.cache_resource
1712
def initialize_index(index_name, documents_folder):
13+
llm_predictor = ChatGPTLLMPredictor()
14+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
1815
if os.path.exists(index_name):
19-
index = GPTSimpleVectorIndex.load_from_disk(index_name)
16+
index = GPTSimpleVectorIndex.load_from_disk(index_name, service_context=service_context)
2017
else:
2118
documents = SimpleDirectoryReader(documents_folder).load_data()
22-
index = GPTSimpleVectorIndex(documents)
19+
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
2320
index.save_to_disk(index_name)
2421

2522
return index
@@ -31,16 +28,29 @@ def query_index(_index, query_text):
3128
return str(response)
3229

3330

34-
# This should be cached and only fully runs once
35-
index = initialize_index(index_name, documents_folder)
36-
37-
3831
st.title("🦙 Llama Index Demo 🦙")
3932
st.header("Welcome to the Llama Index Streamlit Demo")
40-
st.text("Please enter a query about Paul Graham's essays")
33+
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)")
34+
35+
index = None
36+
api_key = st.text_input("Enter your OpenAI API key here:", type="password")
37+
if api_key:
38+
os.environ['OPENAI_API_KEY'] = api_key
39+
index = initialize_index(index_name, documents_folder)
40+
41+
42+
if index is None:
43+
st.warning("Please enter your api key first.")
4144

42-
text = st.text_input("Query text:")
45+
text = st.text_input("Query text:", value="What did the author do growing up?")
4346

4447
if st.button("Run Query") and text is not None:
4548
response = query_index(index, text)
4649
st.markdown(response)
50+
51+
llm_col, embed_col = st.columns(2)
52+
with llm_col:
53+
st.markdown(f"LLM Tokens Used: {index.service_context.llm_predictor._last_token_usage}")
54+
55+
with embed_col:
56+
st.markdown(f"Embedding Tokens Used: {index.service_context.embed_model._last_token_usage}")

0 commit comments

Comments
 (0)