Spaces:
Build error
Build error
Update utils/database.py
Browse files- 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 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 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 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
}
|
| 152 |
-
| chat_prompt
|
| 153 |
-
| llm
|
| 154 |
-
)
|
| 155 |
|
| 156 |
-
return
|
| 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}")
|