Kushal commited on
Commit
42ae809
·
1 Parent(s): 6fc5402

Enhance: Persistent DB, RAG Policy Chunks, and UI Cleanup

Browse files
app/database/connection.py CHANGED
@@ -7,10 +7,14 @@ import uuid
7
  from app.config.settings import settings
8
 
9
  # Create SQLAlchemy engine
10
- engine = create_engine(
11
- settings.DATABASE_URL,
12
- connect_args={"check_same_thread": False} # Needed for SQLite
13
- )
 
 
 
 
14
 
15
  # Session factory
16
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
 
7
  from app.config.settings import settings
8
 
9
  # Create SQLAlchemy engine
10
+ # Check if using SQLite (special handling for threads)
11
+ if settings.DATABASE_URL.startswith("sqlite"):
12
+ engine = create_engine(
13
+ settings.DATABASE_URL,
14
+ connect_args={"check_same_thread": False}
15
+ )
16
+ else:
17
+ engine = create_engine(settings.DATABASE_URL)
18
 
19
  # Session factory
20
  SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
app/database/models.py CHANGED
@@ -97,6 +97,23 @@ class OfficialPolicy(Base):
97
  is_active = Column(Integer, default=1) # 0 = inactive, 1 = active
98
  created_at = Column(DateTime, default=datetime.utcnow)
99
  updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
 
102
  class UserSettings(Base):
 
97
  is_active = Column(Integer, default=1) # 0 = inactive, 1 = active
98
  created_at = Column(DateTime, default=datetime.utcnow)
99
  updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
100
+
101
+ # Relationships
102
+ chunks = relationship("PolicyChunk", back_populates="policy", cascade="all, delete-orphan")
103
+
104
+
105
+ class PolicyChunk(Base):
106
+ """Chunks of official policy text for database-backed retrieval."""
107
+ __tablename__ = "policy_chunks"
108
+
109
+ id = Column(String, primary_key=True, default=generate_id)
110
+ policy_id = Column(String, ForeignKey("official_policies.id", ondelete="CASCADE"), nullable=False, index=True)
111
+ chunk_index = Column(Integer, nullable=False)
112
+ content = Column(Text, nullable=False)
113
+ created_at = Column(DateTime, default=datetime.utcnow)
114
+
115
+ # Relationships
116
+ policy = relationship("OfficialPolicy", back_populates="chunks")
117
 
118
 
119
  class UserSettings(Base):
app/services/policy_service.py CHANGED
@@ -118,7 +118,8 @@ class PolicyService:
118
  document_id=policy.id,
119
  filename=f"[POLICY] {policy.title}",
120
  content=content,
121
- user_id="official_policies" # Special ID for policies
 
122
  )
123
 
124
  return num_chunks
 
118
  document_id=policy.id,
119
  filename=f"[POLICY] {policy.title}",
120
  content=content,
121
+ user_id="official_policies", # Special ID for policies
122
+ db=db
123
  )
124
 
125
  return num_chunks
app/services/rag_service.py CHANGED
@@ -1,4 +1,5 @@
1
  from typing import List, Dict, Optional
 
2
  import chromadb
3
  import os
4
 
@@ -45,7 +46,8 @@ class RAGService:
45
  document_id: str,
46
  filename: str,
47
  content: str,
48
- user_id: str
 
49
  ) -> int:
50
  """
51
  Process document by chunking and storing in vector database.
@@ -90,6 +92,26 @@ class RAGService:
90
  ids=ids
91
  )
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  return len(chunks)
94
 
95
  def semantic_search(
 
1
  from typing import List, Dict, Optional
2
+ from sqlalchemy.orm import Session
3
  import chromadb
4
  import os
5
 
 
46
  document_id: str,
47
  filename: str,
48
  content: str,
49
+ user_id: str,
50
+ db: Optional[Session] = None
51
  ) -> int:
52
  """
53
  Process document by chunking and storing in vector database.
 
92
  ids=ids
93
  )
94
 
95
+ # Also store in structured database if it's an official policy and DB session is provided
96
+ if user_id == "official_policies" and db:
97
+ from app.database.models import PolicyChunk
98
+
99
+ # Delete existing chunks for this policy first to avoid duplicates
100
+ db.query(PolicyChunk).filter(PolicyChunk.policy_id == document_id).delete()
101
+
102
+ # Create new chunks
103
+ for i, chunk_text_content in enumerate(chunks):
104
+ db_chunk = PolicyChunk(
105
+ id=f"{document_id}_{i}",
106
+ policy_id=document_id,
107
+ chunk_index=i,
108
+ content=chunk_text_content
109
+ )
110
+ db.add(db_chunk)
111
+
112
+ db.commit()
113
+ print(f"[RAG Service] {len(chunks)} chunks also stored in structured database for policy {document_id}")
114
+
115
  return len(chunks)
116
 
117
  def semantic_search(