-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
132 lines (109 loc) · 4.64 KB
/
Copy pathapp.py
File metadata and controls
132 lines (109 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
from dotenv import load_dotenv
import streamlit as st
from neo4j import GraphDatabase
from sentence_transformers import SentenceTransformer
import numpy as np
import faiss
from langchain_community.llms import Ollama
# Initialize the local Llama3.1 model
llm = Ollama(model="llama3.1")
# Neo4j connection details
NEO4J_URI = os.getenv('NEO4J_URI')
NEO4J_USERNAME = os.getenv('NEO4J_USERNAME')
NEO4J_PASSWORD = os.getenv('NEO4J_PASSWORD')
# Initialize Neo4j driver
@st.cache_resource
def init_neo4j_driver():
return GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USERNAME, NEO4J_PASSWORD))
# Initialize sentence transformer model for semantic search
@st.cache_resource
def init_sentence_transformer():
return SentenceTransformer('all-MiniLM-L6-v2')
@st.cache_data
def load_pdf_content():
"""Load the content of the PDF from the text file."""
try:
with open('extracted_text.txt', 'r', encoding='utf-8') as file:
return file.read()
except FileNotFoundError:
st.error("Error: 'extracted_text.txt' file not found. Please ensure the file exists in the same directory as this script.")
return None
@st.cache_data
def preprocess_and_index(_pdf_content):
"""Preprocess the PDF content, compute embeddings, and create a FAISS index."""
content_chunks = _pdf_content.split('\n\n') # Split into paragraphs
# Compute embeddings
model = init_sentence_transformer()
embeddings = model.encode(content_chunks)
# Create FAISS index
dimension = embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(embeddings.astype('float32'))
return index, content_chunks
@st.cache_data
def load_or_create_index(_pdf_content):
"""Load existing index or create a new one if it doesn't exist."""
return preprocess_and_index(_pdf_content)
def semantic_search(user_query, index, content_chunks, top_k=3):
"""Perform semantic search using FAISS."""
model = init_sentence_transformer()
query_vector = model.encode([user_query]).astype('float32')
distances, indices = index.search(query_vector, top_k)
return [content_chunks[i] for i in indices[0]]
def query_knowledge_graph(user_query):
"""Query the knowledge graph for relevant information."""
driver = init_neo4j_driver()
with driver.session() as session:
result = session.run(
"""
MATCH (e:Entity)-[r:RELATED_TO]->(related:Entity)
WHERE e.name CONTAINS $user_query OR related.name CONTAINS $user_query
RETURN e.name AS entity, type(r) AS relation, related.name AS related_entity
LIMIT 5
""",
user_query=user_query
)
return [f"{record['entity']} {record['relation']} {record['related_entity']}"
for record in result]
def generate_response(user_query, pdf_excerpts, kg_info):
"""Generate a response using the local Llama3.1 model based on the query, PDF excerpts, and knowledge graph info."""
# Prepare context
context = "\n".join(pdf_excerpts) + "\n" + "\n".join(kg_info)
prompt = f"Context:\n{context}\n\nQuestion: {user_query}\n\nAnswer:"
# Generate response using Llama3.1 model
try:
response = llm(prompt)
return response
except Exception as e:
return f"Error generating response: {str(e)}"
def main():
st.title("RAG Chatbot with Knowledge Graph")
# Initialize session state for chat history
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
# Load PDF content and create index (with loading indicator)
with st.spinner("Initializing chatbot..."):
pdf_content = load_pdf_content()
if pdf_content is None:
st.error("Failed to load PDF content. Please check the 'extracted_text.txt' file.")
return
index, content_chunks = load_or_create_index(pdf_content)
# Chat interface
user_input = st.text_input("You:", key="user_input")
if user_input:
with st.spinner("Generating response..."):
relevant_excerpts = semantic_search(user_input, index, content_chunks)
kg_info = query_knowledge_graph(user_input)
response = generate_response(user_input, relevant_excerpts, kg_info)
# Add user input and bot response to chat history
st.session_state.chat_history.append(("You", user_input))
st.session_state.chat_history.append(("Bot", response))
# Display chat history
for role, message in st.session_state.chat_history:
if role == "You":
st.write(f"**You:** {message}")
else:
st.write(f"**Bot:** {message}")
if __name__ == "__main__":
main()