Upload 2 files
Browse files- app.py +47 -23
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from llama_index.core import VectorStoreIndex, Settings, StorageContext
|
| 4 |
-
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
-
# This is the new, correct import for the Gemini model
|
| 7 |
from llama_index.llms.google import Gemini
|
| 8 |
|
| 9 |
# --- App Configuration ---
|
|
@@ -25,58 +24,83 @@ with st.sidebar:
|
|
| 25 |
"Get your free API key from [Google AI Studio](https://aistudio.google.com/)."
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@st.cache_resource(show_spinner="Loading embedding model...")
|
| 30 |
def load_embed_model():
|
| 31 |
-
"""Loads and caches the embedding model."""
|
| 32 |
return HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 33 |
|
| 34 |
-
@st.cache_resource(show_spinner="
|
| 35 |
-
def
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# --- Main
|
| 45 |
-
|
| 46 |
-
index = load_vector_store()
|
| 47 |
|
| 48 |
if "messages" not in st.session_state:
|
| 49 |
st.session_state.messages = [
|
| 50 |
{"role": "assistant", "content": "Hi! How can I help you build in the Miliastra Sandbox today?"}
|
| 51 |
]
|
| 52 |
|
| 53 |
-
# Display chat messages
|
| 54 |
for message in st.session_state.messages:
|
| 55 |
with st.chat_message(message["role"]):
|
| 56 |
st.markdown(message["content"])
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if prompt := st.chat_input("Your question"):
|
|
|
|
| 60 |
if not google_api_key:
|
| 61 |
st.info("Please add your Google API Key to continue.")
|
| 62 |
st.stop()
|
| 63 |
|
|
|
|
| 64 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 65 |
with st.chat_message("user"):
|
| 66 |
st.markdown(prompt)
|
| 67 |
|
|
|
|
| 68 |
with st.chat_message("assistant"):
|
| 69 |
message_placeholder = st.empty()
|
| 70 |
message_placeholder.markdown("Thinking...")
|
| 71 |
|
| 72 |
try:
|
| 73 |
-
#
|
| 74 |
Settings.llm = Gemini(model_name="models/gemini-1.5-pro-latest", api_key=google_api_key)
|
| 75 |
-
Settings.embed_model = embed_model
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
response = query_engine.query(prompt)
|
| 79 |
|
|
|
|
| 80 |
message_placeholder.markdown(str(response))
|
| 81 |
st.session_state.messages.append({"role": "assistant", "content": str(response)})
|
| 82 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from llama_index.core import VectorStoreIndex, Settings, SimpleDirectoryReader, StorageContext
|
|
|
|
| 4 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 5 |
+
# This is the new, correct import for the Gemini model from the modern library
|
| 6 |
from llama_index.llms.google import Gemini
|
| 7 |
|
| 8 |
# --- App Configuration ---
|
|
|
|
| 24 |
"Get your free API key from [Google AI Studio](https://aistudio.google.com/)."
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# --- Define paths for your raw data and the persistent storage ---
|
| 28 |
+
DATA_DIR = "./data"
|
| 29 |
+
STORAGE_DIR = "./storage"
|
| 30 |
+
|
| 31 |
+
# --- Caching Functions to improve performance ---
|
| 32 |
+
|
| 33 |
@st.cache_resource(show_spinner="Loading embedding model...")
|
| 34 |
def load_embed_model():
|
| 35 |
+
"""Loads and caches the embedding model from Hugging Face."""
|
| 36 |
return HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
| 37 |
|
| 38 |
+
@st.cache_resource(show_spinner="Loading knowledge base... (This may take a moment on first startup)")
|
| 39 |
+
def load_index():
|
| 40 |
+
"""
|
| 41 |
+
Loads or builds the vector index.
|
| 42 |
+
On the very first run, it will build the index from the documents in DATA_DIR.
|
| 43 |
+
On subsequent runs, it will load the pre-built index from STORAGE_DIR.
|
| 44 |
+
"""
|
| 45 |
+
Settings.embed_model = load_embed_model()
|
| 46 |
+
|
| 47 |
+
if not os.path.exists(STORAGE_DIR):
|
| 48 |
+
st.info("First time startup: Building the knowledge base from your documents. Please wait.")
|
| 49 |
+
# Load all your .txt files from the 'data' directory
|
| 50 |
+
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
| 51 |
+
|
| 52 |
+
# Create the index from the documents
|
| 53 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 54 |
+
|
| 55 |
+
# Save the newly built index to disk for future use
|
| 56 |
+
index.storage_context.persist(persist_dir=STORAGE_DIR)
|
| 57 |
+
st.success("Knowledge base built successfully!")
|
| 58 |
+
else:
|
| 59 |
+
# Load the existing index from the 'storage' directory
|
| 60 |
+
storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
|
| 61 |
+
index = VectorStoreIndex.from_storage(storage_context)
|
| 62 |
+
|
| 63 |
+
return index
|
| 64 |
|
| 65 |
+
# --- Main Application Logic ---
|
| 66 |
+
index = load_index()
|
|
|
|
| 67 |
|
| 68 |
if "messages" not in st.session_state:
|
| 69 |
st.session_state.messages = [
|
| 70 |
{"role": "assistant", "content": "Hi! How can I help you build in the Miliastra Sandbox today?"}
|
| 71 |
]
|
| 72 |
|
| 73 |
+
# Display existing chat messages
|
| 74 |
for message in st.session_state.messages:
|
| 75 |
with st.chat_message(message["role"]):
|
| 76 |
st.markdown(message["content"])
|
| 77 |
|
| 78 |
+
# Handle user input
|
| 79 |
if prompt := st.chat_input("Your question"):
|
| 80 |
+
# Ensure the user has entered their API key
|
| 81 |
if not google_api_key:
|
| 82 |
st.info("Please add your Google API Key to continue.")
|
| 83 |
st.stop()
|
| 84 |
|
| 85 |
+
# Add user message to chat history
|
| 86 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 87 |
with st.chat_message("user"):
|
| 88 |
st.markdown(prompt)
|
| 89 |
|
| 90 |
+
# Generate and display the assistant's response
|
| 91 |
with st.chat_message("assistant"):
|
| 92 |
message_placeholder = st.empty()
|
| 93 |
message_placeholder.markdown("Thinking...")
|
| 94 |
|
| 95 |
try:
|
| 96 |
+
# Configure the LLM with the API key
|
| 97 |
Settings.llm = Gemini(model_name="models/gemini-1.5-pro-latest", api_key=google_api_key)
|
|
|
|
| 98 |
|
| 99 |
+
# Create a query engine from the loaded index
|
| 100 |
+
query_engine = index.as_query_engine(similarity_top_k=3) # Get top 3 relevant text chunks
|
| 101 |
response = query_engine.query(prompt)
|
| 102 |
|
| 103 |
+
# Display the final answer
|
| 104 |
message_placeholder.markdown(str(response))
|
| 105 |
st.session_state.messages.append({"role": "assistant", "content": str(response)})
|
| 106 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
streamlit
|
| 2 |
llama-index
|
| 3 |
llama-index-embeddings-huggingface
|
| 4 |
-
llama-index-
|
| 5 |
-
llama-index-integrations-llms-google
|
| 6 |
-
chromadb
|
|
|
|
| 1 |
streamlit
|
| 2 |
llama-index
|
| 3 |
llama-index-embeddings-huggingface
|
| 4 |
+
llama-index-integrations-llms-google
|
|
|
|
|
|