-
Notifications
You must be signed in to change notification settings - Fork 17
/
hello_llamaindex.py
29 lines (22 loc) · 965 Bytes
/
hello_llamaindex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import streamlit as st
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.base.base_query_engine import BaseQueryEngine
from llama_index.core.base.response.schema import RESPONSE_TYPE
class HelloLlamaIndex:
_query_engine: BaseQueryEngine
def __init__(self):
if "query_engine" not in st.session_state:
index: VectorStoreIndex = VectorStoreIndex.from_documents(
SimpleDirectoryReader("./tmp").load_data()
)
st.session_state["query_engine"] = index.as_query_engine()
self._query_engine = st.session_state["query_engine"]
def run(self):
query: str = st.text_input("Query", placeholder="Enter your query here")
if query != "":
result: RESPONSE_TYPE = self._query_engine.query(query)
st.write(result.response)
def __call__(self):
self.run()
if __name__ == "__main__":
HelloLlamaIndex()()