Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ from llama_index.core import Settings, VectorStoreIndex
|
|
| 8 |
from llama_index.llms.openai import OpenAI
|
| 9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 10 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
| 11 |
-
from llama_index.core.ingestion import IngestionPipeline
|
| 12 |
from langchain_community.vectorstores import FAISS as LangChainFAISS
|
| 13 |
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 14 |
from langchain.chains import create_retrieval_chain
|
|
@@ -18,7 +17,6 @@ from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
|
| 18 |
from langchain_core.documents import Document
|
| 19 |
import faiss
|
| 20 |
import tempfile
|
| 21 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 22 |
|
| 23 |
# Load environment variables
|
| 24 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
@@ -62,17 +60,14 @@ if uploaded_file:
|
|
| 62 |
st.subheader("LangChain Query")
|
| 63 |
|
| 64 |
try:
|
| 65 |
-
# β
|
| 66 |
st.write("Processing CSV with a custom loader...")
|
| 67 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=90)
|
| 68 |
documents = []
|
| 69 |
|
| 70 |
for _, row in data.iterrows():
|
| 71 |
-
content = "
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
doc = Document(page_content=chunk)
|
| 75 |
-
documents.append(doc)
|
| 76 |
|
| 77 |
# β
Create FAISS VectorStore
|
| 78 |
st.write(f"β
Initializing FAISS with dimension: {faiss_dimension}")
|
|
@@ -96,31 +91,21 @@ if uploaded_file:
|
|
| 96 |
st.error(f"Error adding documents to FAISS: {e}")
|
| 97 |
|
| 98 |
# β
Limit number of retrieved documents
|
| 99 |
-
retriever = langchain_vector_store.as_retriever(search_kwargs={"k":
|
| 100 |
-
|
| 101 |
-
# β
Create LangChain Query Execution Pipeline
|
| 102 |
-
system_prompt = (
|
| 103 |
-
"You are an assistant for question-answering tasks. "
|
| 104 |
-
"Use the following pieces of retrieved context to answer "
|
| 105 |
-
"the question. Keep the answer concise.\n\n{context}"
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
prompt = ChatPromptTemplate.from_messages(
|
| 109 |
-
[("system", system_prompt), ("human", "{input}")]
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
question_answer_chain = create_stuff_documents_chain(ChatOpenAI(model="gpt-4o"), prompt)
|
| 113 |
-
langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 114 |
|
| 115 |
# β
Query Processing
|
| 116 |
query = st.text_input("Ask a question about your data (LangChain):")
|
| 117 |
|
| 118 |
if query:
|
| 119 |
try:
|
| 120 |
-
|
|
|
|
| 121 |
retrieved_context = retrieved_context[:3000]
|
| 122 |
|
| 123 |
-
# β
|
|
|
|
|
|
|
|
|
|
| 124 |
system_prompt = (
|
| 125 |
"You are an assistant for question-answering tasks. "
|
| 126 |
"Use the following pieces of retrieved context to answer "
|
|
@@ -133,13 +118,9 @@ if uploaded_file:
|
|
| 133 |
except Exception as e:
|
| 134 |
error_message = traceback.format_exc()
|
| 135 |
st.error(f"Error processing query: {e}")
|
| 136 |
-
st.text(error_message)
|
| 137 |
|
| 138 |
except Exception as e:
|
| 139 |
error_message = traceback.format_exc()
|
| 140 |
st.error(f"Error processing with LangChain: {e}")
|
| 141 |
-
st.text(error_message)
|
| 142 |
-
except Exception as e:
|
| 143 |
-
error_message = traceback.format_exc()
|
| 144 |
-
st.error(f"Error reading uploaded file: {e}")
|
| 145 |
-
st.text(error_message) #
|
|
|
|
| 8 |
from llama_index.llms.openai import OpenAI
|
| 9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 10 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
|
|
|
| 11 |
from langchain_community.vectorstores import FAISS as LangChainFAISS
|
| 12 |
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 13 |
from langchain.chains import create_retrieval_chain
|
|
|
|
| 17 |
from langchain_core.documents import Document
|
| 18 |
import faiss
|
| 19 |
import tempfile
|
|
|
|
| 20 |
|
| 21 |
# Load environment variables
|
| 22 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 60 |
st.subheader("LangChain Query")
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# β
Store each row as a single document
|
| 64 |
st.write("Processing CSV with a custom loader...")
|
|
|
|
| 65 |
documents = []
|
| 66 |
|
| 67 |
for _, row in data.iterrows():
|
| 68 |
+
content = " | ".join([f"{col}: {row[col]}" for col in data.columns]) # β
Store entire row as a document
|
| 69 |
+
doc = Document(page_content=content)
|
| 70 |
+
documents.append(doc)
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# β
Create FAISS VectorStore
|
| 73 |
st.write(f"β
Initializing FAISS with dimension: {faiss_dimension}")
|
|
|
|
| 91 |
st.error(f"Error adding documents to FAISS: {e}")
|
| 92 |
|
| 93 |
# β
Limit number of retrieved documents
|
| 94 |
+
retriever = langchain_vector_store.as_retriever(search_kwargs={"k": 15})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# β
Query Processing
|
| 97 |
query = st.text_input("Ask a question about your data (LangChain):")
|
| 98 |
|
| 99 |
if query:
|
| 100 |
try:
|
| 101 |
+
retrieved_docs = retriever.get_relevant_documents(query)
|
| 102 |
+
retrieved_context = "\n\n".join([doc.page_content for doc in retrieved_docs])
|
| 103 |
retrieved_context = retrieved_context[:3000]
|
| 104 |
|
| 105 |
+
# β
Show retrieved context for debugging
|
| 106 |
+
st.write("π **Retrieved Context Preview:**")
|
| 107 |
+
st.text(retrieved_context)
|
| 108 |
+
|
| 109 |
system_prompt = (
|
| 110 |
"You are an assistant for question-answering tasks. "
|
| 111 |
"Use the following pieces of retrieved context to answer "
|
|
|
|
| 118 |
except Exception as e:
|
| 119 |
error_message = traceback.format_exc()
|
| 120 |
st.error(f"Error processing query: {e}")
|
| 121 |
+
st.text(error_message)
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
error_message = traceback.format_exc()
|
| 125 |
st.error(f"Error processing with LangChain: {e}")
|
| 126 |
+
st.text(error_message)
|
|
|
|
|
|
|
|
|
|
|
|