Update utils/database.py
Browse files- utils/database.py +33 -18
utils/database.py
CHANGED
|
@@ -10,8 +10,9 @@ from langchain.memory import ConversationBufferWindowMemory
|
|
| 10 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
|
| 11 |
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
| 13 |
-
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder,
|
| 14 |
from langchain.agents import initialize_agent
|
|
|
|
| 15 |
|
| 16 |
def create_connection(db_file):
|
| 17 |
try:
|
|
@@ -106,6 +107,8 @@ def format_chat_history(messages: list[BaseMessage]) -> list[dict]:
|
|
| 106 |
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
| 109 |
def initialize_qa_system(vector_store):
|
| 110 |
"""Initialize QA system with proper chat handling"""
|
| 111 |
try:
|
|
@@ -122,36 +125,48 @@ def initialize_qa_system(vector_store):
|
|
| 122 |
k=5
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Create
|
| 126 |
qa = ConversationalRetrievalChain.from_llm(
|
| 127 |
llm=llm,
|
| 128 |
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 129 |
chain_type="stuff",
|
| 130 |
)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
memory=memory,
|
|
|
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
-
return
|
| 151 |
|
| 152 |
except Exception as e:
|
| 153 |
st.error(f"Error initializing QA system: {e}")
|
| 154 |
return None
|
|
|
|
| 155 |
def initialize_faiss(embeddings, documents, document_names):
|
| 156 |
"""Initialize FAISS vector store"""
|
| 157 |
try:
|
|
|
|
| 10 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
|
| 11 |
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
| 13 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder,
|
| 14 |
from langchain.agents import initialize_agent
|
| 15 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate
|
| 16 |
|
| 17 |
def create_connection(db_file):
|
| 18 |
try:
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
+
|
| 111 |
+
|
| 112 |
def initialize_qa_system(vector_store):
|
| 113 |
"""Initialize QA system with proper chat handling"""
|
| 114 |
try:
|
|
|
|
| 125 |
k=5
|
| 126 |
)
|
| 127 |
|
| 128 |
+
# Create the base QA chain
|
| 129 |
qa = ConversationalRetrievalChain.from_llm(
|
| 130 |
llm=llm,
|
| 131 |
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 132 |
chain_type="stuff",
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Define the tools
|
| 136 |
+
tools = [
|
| 137 |
+
Tool(
|
| 138 |
+
name="RFP_Knowledge_Base",
|
| 139 |
+
func=qa.run,
|
| 140 |
+
description="Use this tool to analyze RFP documents and answer questions about their content."
|
| 141 |
+
)
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
# Create the prompt template
|
| 145 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 146 |
+
("system", "You are a helpful assistant analyzing RFP documents."),
|
| 147 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
| 148 |
+
("human", "{input}"),
|
| 149 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 150 |
+
])
|
| 151 |
+
|
| 152 |
+
# Create the agent
|
| 153 |
+
agent = create_openai_tools_agent(llm, tools, prompt)
|
| 154 |
+
|
| 155 |
+
# Create the agent executor
|
| 156 |
+
agent_executor = AgentExecutor(
|
| 157 |
+
agent=agent,
|
| 158 |
+
tools=tools,
|
| 159 |
memory=memory,
|
| 160 |
+
verbose=True,
|
| 161 |
+
handle_parsing_errors=True
|
| 162 |
)
|
| 163 |
|
| 164 |
+
return agent_executor
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
st.error(f"Error initializing QA system: {e}")
|
| 168 |
return None
|
| 169 |
+
|
| 170 |
def initialize_faiss(embeddings, documents, document_names):
|
| 171 |
"""Initialize FAISS vector store"""
|
| 172 |
try:
|