Update utils/database.py
Browse files- utils/database.py +62 -8
utils/database.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
# utils/database.py
|
| 2 |
from langchain.memory import ConversationBufferWindowMemory
|
| 3 |
from langchain_core.messages import (
|
| 4 |
-
HumanMessage,
|
| 5 |
-
AIMessage,
|
| 6 |
SystemMessage,
|
| 7 |
BaseMessage # Added this import
|
| 8 |
)
|
|
@@ -15,9 +15,62 @@ from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputP
|
|
| 15 |
from langchain_core.runnables import RunnablePassthrough
|
| 16 |
import os
|
| 17 |
import streamlit as st
|
|
|
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def initialize_qa_system(vector_store):
|
| 20 |
-
"""Initialize QA system with proper chat handling"""
|
| 21 |
try:
|
| 22 |
llm = ChatOpenAI(
|
| 23 |
temperature=0.5,
|
|
@@ -77,9 +130,10 @@ def initialize_qa_system(vector_store):
|
|
| 77 |
except Exception as e:
|
| 78 |
st.error(f"Error initializing QA system: {e}")
|
| 79 |
return None
|
| 80 |
-
|
|
|
|
| 81 |
def initialize_faiss(embeddings, documents, document_names):
|
| 82 |
-
"""Initialize FAISS vector store"""
|
| 83 |
try:
|
| 84 |
from langchain.vectorstores import FAISS
|
| 85 |
|
|
@@ -93,10 +147,10 @@ def initialize_faiss(embeddings, documents, document_names):
|
|
| 93 |
st.error(f"Error initializing FAISS: {e}")
|
| 94 |
return None
|
| 95 |
|
| 96 |
-
|
| 97 |
@st.cache_resource
|
| 98 |
def get_embeddings_model():
|
| 99 |
-
"""Get the embeddings model"""
|
| 100 |
try:
|
| 101 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 102 |
|
|
@@ -105,4 +159,4 @@ def get_embeddings_model():
|
|
| 105 |
return embeddings
|
| 106 |
except Exception as e:
|
| 107 |
st.error(f"Error loading embeddings model: {e}")
|
| 108 |
-
return None
|
|
|
|
| 1 |
# utils/database.py
|
| 2 |
from langchain.memory import ConversationBufferWindowMemory
|
| 3 |
from langchain_core.messages import (
|
| 4 |
+
HumanMessage,
|
| 5 |
+
AIMessage,
|
| 6 |
SystemMessage,
|
| 7 |
BaseMessage # Added this import
|
| 8 |
)
|
|
|
|
| 15 |
from langchain_core.runnables import RunnablePassthrough
|
| 16 |
import os
|
| 17 |
import streamlit as st
|
| 18 |
+
import sqlite3
|
| 19 |
+
from sqlite3 import Error
|
| 20 |
|
| 21 |
+
# Original SQL-related functions preserved
|
| 22 |
+
def create_connection(db_file):
|
| 23 |
+
"""Create a database connection to the SQLite database."""
|
| 24 |
+
try:
|
| 25 |
+
conn = sqlite3.connect(db_file)
|
| 26 |
+
return conn
|
| 27 |
+
except Error as e:
|
| 28 |
+
st.error(f"Error: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
def create_tables(conn):
|
| 32 |
+
"""Create necessary tables in the database."""
|
| 33 |
+
try:
|
| 34 |
+
sql_create_documents_table = '''
|
| 35 |
+
CREATE TABLE IF NOT EXISTS documents (
|
| 36 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 37 |
+
name TEXT NOT NULL,
|
| 38 |
+
content TEXT NOT NULL,
|
| 39 |
+
upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 40 |
+
);
|
| 41 |
+
'''
|
| 42 |
+
|
| 43 |
+
sql_create_queries_table = '''
|
| 44 |
+
CREATE TABLE IF NOT EXISTS queries (
|
| 45 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 46 |
+
query TEXT NOT NULL,
|
| 47 |
+
response TEXT NOT NULL,
|
| 48 |
+
document_id INTEGER,
|
| 49 |
+
query_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 50 |
+
FOREIGN KEY (document_id) REFERENCES documents (id)
|
| 51 |
+
);
|
| 52 |
+
'''
|
| 53 |
+
|
| 54 |
+
sql_create_annotations_table = '''
|
| 55 |
+
CREATE TABLE IF NOT EXISTS annotations (
|
| 56 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 57 |
+
document_id INTEGER NOT NULL,
|
| 58 |
+
annotation TEXT NOT NULL,
|
| 59 |
+
page_number INTEGER,
|
| 60 |
+
annotation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 61 |
+
FOREIGN KEY (document_id) REFERENCES documents (id)
|
| 62 |
+
);
|
| 63 |
+
'''
|
| 64 |
+
|
| 65 |
+
conn.execute(sql_create_documents_table)
|
| 66 |
+
conn.execute(sql_create_queries_table)
|
| 67 |
+
conn.execute(sql_create_annotations_table)
|
| 68 |
+
except Error as e:
|
| 69 |
+
st.error(f"Error: {e}")
|
| 70 |
+
|
| 71 |
+
# QA system initialization function with necessary updates
|
| 72 |
def initialize_qa_system(vector_store):
|
| 73 |
+
"""Initialize QA system with proper chat handling."""
|
| 74 |
try:
|
| 75 |
llm = ChatOpenAI(
|
| 76 |
temperature=0.5,
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
st.error(f"Error initializing QA system: {e}")
|
| 132 |
return None
|
| 133 |
+
|
| 134 |
+
# FAISS vector store initialization
|
| 135 |
def initialize_faiss(embeddings, documents, document_names):
|
| 136 |
+
"""Initialize FAISS vector store."""
|
| 137 |
try:
|
| 138 |
from langchain.vectorstores import FAISS
|
| 139 |
|
|
|
|
| 147 |
st.error(f"Error initializing FAISS: {e}")
|
| 148 |
return None
|
| 149 |
|
| 150 |
+
# Embeddings model retrieval
|
| 151 |
@st.cache_resource
|
| 152 |
def get_embeddings_model():
|
| 153 |
+
"""Get the embeddings model."""
|
| 154 |
try:
|
| 155 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 156 |
|
|
|
|
| 159 |
return embeddings
|
| 160 |
except Exception as e:
|
| 161 |
st.error(f"Error loading embeddings model: {e}")
|
| 162 |
+
return None
|