DrishtiSharma commited on
Commit
8d99061
Β·
verified Β·
1 Parent(s): 253df02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -15
app.py CHANGED
@@ -20,17 +20,20 @@ import faiss
20
  import tempfile
21
 
22
  # Load environment variables
23
-
24
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
25
 
26
- # Check OpenAI API Key
27
  if not os.getenv("OPENAI_API_KEY"):
28
  st.error("⚠️ OpenAI API Key is missing! Please check your .env file or environment variables.")
29
 
30
- # Global settings for LlamaIndex
31
- EMBED_DIMENSION = 512
 
 
 
 
32
  Settings.llm = OpenAI(model="gpt-4o")
33
- Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small", dimensions=EMBED_DIMENSION)
34
 
35
  # Streamlit app
36
  st.title("Chat with CSV Files - LangChain vs LlamaIndex")
@@ -70,25 +73,49 @@ if uploaded_file:
70
  documents = []
71
  for _, row in data.iterrows():
72
  content = "\n".join([f"{col}: {row[col]}" for col in data.columns])
73
- doc = Document(page_content=content)
74
  documents.append(doc)
75
 
 
 
 
 
 
 
 
 
76
 
77
- # βœ… Create FAISS VectorStore
78
- langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
79
  docstore = InMemoryDocstore()
80
  index_to_docstore_id = {}
81
 
82
  langchain_vector_store = LangChainFAISS(
83
- embedding_function=OpenAIEmbeddings(),
84
  index=langchain_index,
85
  docstore=docstore,
86
  index_to_docstore_id=index_to_docstore_id,
87
  )
88
 
89
- # βœ… Add properly formatted documents to FAISS
90
- langchain_vector_store.add_documents(documents)
91
- st.write("Documents successfully added to FAISS VectorStore.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  # βœ… Query Processing
94
  query = st.text_input("Ask a question about your data (LangChain):")
@@ -101,14 +128,14 @@ if uploaded_file:
101
  except Exception as e:
102
  error_message = traceback.format_exc()
103
  st.error(f"Error processing query: {e}")
104
- st.text(error_message)
105
 
106
  except Exception as e:
107
  error_message = traceback.format_exc()
108
  st.error(f"Error processing with LangChain: {e}")
109
- st.text(error_message)
110
 
111
  except Exception as e:
112
  error_message = traceback.format_exc()
113
  st.error(f"Error reading uploaded file: {e}")
114
- st.text(error_message)
 
20
  import tempfile
21
 
22
  # Load environment variables
 
23
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
24
 
25
+ # βœ… Check OpenAI API Key
26
  if not os.getenv("OPENAI_API_KEY"):
27
  st.error("⚠️ OpenAI API Key is missing! Please check your .env file or environment variables.")
28
 
29
+ # βœ… Ensure OpenAI Embeddings match FAISS dimensions
30
+ embedding_function = OpenAIEmbeddings()
31
+ test_vector = embedding_function.embed_query("test") # Sample embedding
32
+ faiss_dimension = len(test_vector) # βœ… Dynamically detect correct dimension
33
+
34
+ # βœ… Update global settings for LlamaIndex
35
  Settings.llm = OpenAI(model="gpt-4o")
36
+ Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small", dimensions=faiss_dimension)
37
 
38
  # Streamlit app
39
  st.title("Chat with CSV Files - LangChain vs LlamaIndex")
 
73
  documents = []
74
  for _, row in data.iterrows():
75
  content = "\n".join([f"{col}: {row[col]}" for col in data.columns])
76
+ doc = Document(page_content=content)
77
  documents.append(doc)
78
 
79
+ # βœ… Debugging: Display a sample processed document
80
+ if documents:
81
+ st.write("Sample processed document (LangChain):")
82
+ st.text(documents[0].page_content)
83
+
84
+ # βœ… Create FAISS VectorStore with Correct Dimensions
85
+ st.write(f"βœ… Initializing FAISS with dimension: {faiss_dimension}")
86
+ langchain_index = faiss.IndexFlatL2(faiss_dimension)
87
 
 
 
88
  docstore = InMemoryDocstore()
89
  index_to_docstore_id = {}
90
 
91
  langchain_vector_store = LangChainFAISS(
92
+ embedding_function=embedding_function,
93
  index=langchain_index,
94
  docstore=docstore,
95
  index_to_docstore_id=index_to_docstore_id,
96
  )
97
 
98
+ # βœ… Ensure documents are added correctly
99
+ try:
100
+ langchain_vector_store.add_documents(documents)
101
+ st.write("βœ… Documents successfully added to FAISS VectorStore.")
102
+ except Exception as e:
103
+ st.error(f"Error adding documents to FAISS: {e}")
104
+
105
+ # βœ… Create LangChain Query Execution Pipeline
106
+ retriever = langchain_vector_store.as_retriever()
107
+ system_prompt = (
108
+ "You are an assistant for question-answering tasks. "
109
+ "Use the following pieces of retrieved context to answer "
110
+ "the question. If you don't know the answer, say that you "
111
+ "don't know. Use three sentences maximum and keep the "
112
+ "answer concise.\n\n{context}"
113
+ )
114
+ prompt = ChatPromptTemplate.from_messages(
115
+ [("system", system_prompt), ("human", "{input}")]
116
+ )
117
+ question_answer_chain = create_stuff_documents_chain(ChatOpenAI(model="gpt-4o"), prompt)
118
+ langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
119
 
120
  # βœ… Query Processing
121
  query = st.text_input("Ask a question about your data (LangChain):")
 
128
  except Exception as e:
129
  error_message = traceback.format_exc()
130
  st.error(f"Error processing query: {e}")
131
+ st.text(error_message)
132
 
133
  except Exception as e:
134
  error_message = traceback.format_exc()
135
  st.error(f"Error processing with LangChain: {e}")
136
+ st.text(error_message)
137
 
138
  except Exception as e:
139
  error_message = traceback.format_exc()
140
  st.error(f"Error reading uploaded file: {e}")
141
+ st.text(error_message)