Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +14 -30
utils/database.py
CHANGED
|
@@ -130,46 +130,30 @@ def initialize_qa_system(vector_store):
|
|
| 130 |
api_key=os.environ.get("OPENAI_API_KEY")
|
| 131 |
)
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
])
|
| 140 |
|
| 141 |
# Create retriever function
|
| 142 |
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
| 143 |
|
| 144 |
-
# Create the
|
| 145 |
-
|
| 146 |
{
|
| 147 |
"context": lambda x: retriever.get_relevant_documents(x["input"]),
|
| 148 |
-
"input": lambda x: x["input"]
|
| 149 |
-
"chat_history": lambda x: x.get("chat_history", []),
|
| 150 |
-
"agent_scratchpad": lambda x: format_to_tool_messages(x["intermediate_steps"])
|
| 151 |
}
|
| 152 |
-
|
|
| 153 |
-
| llm
|
| 154 |
-
| OpenAIToolsAgentOutputParser()
|
| 155 |
)
|
| 156 |
|
| 157 |
-
|
| 158 |
-
agent_executor = AgentExecutor(
|
| 159 |
-
agent=rag_pipe,
|
| 160 |
-
tools=[
|
| 161 |
-
Tool(
|
| 162 |
-
name="RFP_Knowledge_Base",
|
| 163 |
-
func=lambda x: retriever.get_relevant_documents(x),
|
| 164 |
-
description="Use this tool to analyze RFP documents and answer questions about their content."
|
| 165 |
-
)
|
| 166 |
-
],
|
| 167 |
-
verbose=True,
|
| 168 |
-
handle_parsing_errors=True,
|
| 169 |
-
return_intermediate_steps=True
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
return agent_executor
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
st.error(f"Error initializing QA system: {e}")
|
|
|
|
| 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}")
|