DrishtiSharma commited on
Commit
af6e981
Β·
verified Β·
1 Parent(s): a756b7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -98,6 +98,20 @@ if uploaded_file:
98
  # βœ… Limit number of retrieved documents
99
  retriever = langchain_vector_store.as_retriever(search_kwargs={"k": 5})
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # βœ… Query Processing
102
  query = st.text_input("Ask a question about your data (LangChain):")
103
 
@@ -106,6 +120,7 @@ if uploaded_file:
106
  retrieved_context = "\n\n".join([doc.page_content for doc in retriever.get_relevant_documents(query)])
107
  retrieved_context = retrieved_context[:3000]
108
 
 
109
  system_prompt = (
110
  "You are an assistant for question-answering tasks. "
111
  "Use the following pieces of retrieved context to answer "
@@ -118,9 +133,13 @@ if uploaded_file:
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)
 
 
 
 
 
98
  # βœ… Limit number of retrieved documents
99
  retriever = langchain_vector_store.as_retriever(search_kwargs={"k": 5})
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
 
 
120
  retrieved_context = "\n\n".join([doc.page_content for doc in retriever.get_relevant_documents(query)])
121
  retrieved_context = retrieved_context[:3000]
122
 
123
+ # βœ… Ensure that we use the retrieved context
124
  system_prompt = (
125
  "You are an assistant for question-answering tasks. "
126
  "Use the following pieces of retrieved context to answer "
 
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) #