cryogenic22 commited on
Commit
4571b33
·
verified ·
1 Parent(s): 00f99d1

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +11 -18
utils/database.py CHANGED
@@ -130,30 +130,23 @@ def initialize_qa_system(vector_store):
130
  api_key=os.environ.get("OPENAI_API_KEY")
131
  )
132
 
133
- # Define the template strings
134
- system_template = "You are a helpful assistant analyzing RFP documents. Use the provided context to answer questions."
135
- human_template = "Question: {input}\nContext: {context}"
136
-
137
- # Create the prompt template using the simpler format
138
- chat_prompt = ChatPromptTemplate.from_messages([
139
- ("system", system_template),
140
- ("human", human_template)
141
  ])
142
 
143
  # Create retriever function
144
  retriever = vector_store.as_retriever(search_kwargs={"k": 2})
145
 
146
- # Create the chain
147
- qa_chain = (
148
- {
149
- "context": lambda x: retriever.get_relevant_documents(x["input"]),
150
- "input": lambda x: x["input"]
151
- }
152
- | chat_prompt
153
- | llm
154
- )
155
 
156
- return qa_chain
157
 
158
  except Exception as e:
159
  st.error(f"Error initializing QA system: {e}")
 
130
  api_key=os.environ.get("OPENAI_API_KEY")
131
  )
132
 
133
+ # Create the prompt template
134
+ prompt = ChatPromptTemplate.from_messages([
135
+ ("system", "You are a helpful assistant analyzing RFP documents."),
136
+ MessagesPlaceholder(variable_name="chat_history"),
137
+ ("human", "{input}\nContext: {context}")
 
 
 
138
  ])
139
 
140
  # Create retriever function
141
  retriever = vector_store.as_retriever(search_kwargs={"k": 2})
142
 
143
+ # Create the chain with proper chat history handling
144
+ chain = RunnablePassthrough.assign(
145
+ context=lambda x: "\n".join(doc.page_content for doc in retriever.get_relevant_documents(x["input"])),
146
+ chat_history=lambda x: x.get("chat_history", [])
147
+ ) | prompt | llm
 
 
 
 
148
 
149
+ return chain
150
 
151
  except Exception as e:
152
  st.error(f"Error initializing QA system: {e}")