import os
import logging
import re
import json
from fastapi import FastAPI, HTTPException, Depends, Query
from pydantic import BaseModel, Field
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
Adding advanced NLP packages for better policy text analysis
import spacy
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader
from langchain.text_splitters import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain.tools.retriever.retriever_tool import create_retriever_tool
from langchain_community.agent_toolkits import SQLDatabaseToolkit
from langchain.tools import tool # Importing tool decorator
folder_path = os.getenv("DOCS_PATH", "./docs") # Use env var or default to ./docs
--- Initialize FastAPI ---
** Added FastAPI initialization **
app = FastAPI(
title="Policy Compliance API",
description="API for checking policy compliance using NLP and database analysis",
version="1.0.0"
)
--- Initialize NLP components ---
nlp = None
try:
# Initialize NLP components from code 2
nltk.download('punkt', quiet=True)
if not spacy.util.is_package('en_core_web_sm'):
logger.info("Downloading Spacy model...")
spacy.cli.download('en_core_web_sm')
nlp = spacy.load("en_core_web_sm")
except Exception as e:
logger.error(f"Error initializing NLP components: {e}")
--- Define Pydantic Models ---
** Added Pydantic models for API requests/responses **
class ComplianceQuery(BaseModel):
query_text: str = Field(..., description="Natural language query about policy compliance")
department: Optional[str] = Field(None, description="Optional department filter")
class ComplianceResponse(BaseModel):
query: str
violations: List[PolicyViolation]
total_count: int
relevant_policies: List[str]
class PolicySearchQuery(BaseModel):
search_text: str = Field(..., description="Text to search for in policy documents")
class PolicySearchResult(BaseModel):
document_name: str
content: str
relevance_score: float
class PolicySearchResponse(BaseModel):
query: str
results: List[PolicySearchResult]
--- Define SQL Query Templates ---
** Modified to prevent SQL injection **
SQL_QUERY_TEMPLATES = {
"violation_count": "SELECT COUNT() FROM employees WHERE ?;",
"salary_violation": "SELECT * FROM employees WHERE salary < ?;",
"policy_breach": "SELECT employee_name FROM violations WHERE policy_id IN (SELECT id FROM policies WHERE criteria = ?);",
"detailed_violations": "SELECT employee_id, first_name, last_name FROM employees WHERE ?;",
"department_summary": "SELECT department, COUNT() as violation_count FROM employees WHERE ? GROUP BY department;"
}
--- Load and Split Documents ---
def load_documents(folder_path: str) -> List:
"""Loads PDF and DOCX files from the specified folder."""
documents = []
if not os.path.exists(folder_path):
logger.warning(f"Folder does not exist: {folder_path}")
return documents
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if filename.endswith('.pdf'):
documents.extend(PyPDFLoader(file_path).load())
elif filename.endswith('.docx'):
documents.extend(Docx2txtLoader(file_path).load())
else:
logger.info(f"Unsupported file type: {filename}")
except Exception as e:
logger.error(f"Error loading {filename}: {e}")
return documents
** Enhanced error handling for document loading **
if not documents:
logger.warning("No documents loaded! Vector store may be empty.")
# ** Added fallback for empty document set **
documents = [{"page_content": "No policy documents found. Please add documents to the docs folder.", "metadata": {"source": "system"}}]
** Enhanced database connection with better error handling **
logger.info(f"Connecting to database {db_name}@{db_host}")
db = None
db_tools = []
try:
connection_string = f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}"
db = SQLDatabase.from_uri(connection_string)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0, openai_api_key=openai_api_key)
db_toolkit = SQLDatabaseToolkit(db=db, llm=llm)
db_tools = db_toolkit.get_tools()
logger.info("Database connection successful")
except Exception as e:
logger.error(f"Database connection failed: {e}")
# ** Added more specific error handling **
if "Access denied" in str(e):
logger.error("Database credentials are incorrect")
elif "Unknown database" in str(e):
logger.error(f"Database '{db_name}' does not exist")
elif "Can't connect" in str(e):
logger.error(f"Cannot connect to host '{db_host}'")
--- Helper function for enhanced criteria mapping ---
def extract_criteria_from_text(text: str) -> Dict[str, Any]:
"""
Enhanced criteria extraction using regex, heuristics, and NLP.
Combines pattern matching with NLP capabilities.
code
** Completed implementation with SQL injection protection **
"""
criteria = {}
# Basic regex patterns
hire_date_match = re.search(r"employees hired (before|after) (\d{4})", text, re.IGNORECASE)
if hire_date_match:
operator = "<" if hire_date_match.group(1).lower() == "before" else ">"
year = hire_date_match.group(2)
criteria["hire_date"] = {"operator": operator, "value": f"{year}-01-01"}
dept_match = re.search(r"department[s]? (is|are|in) ([\w\s,]+)", text, re.IGNORECASE)
if dept_match:
depts = [d.strip() for d in dept_match.group(2).split(',')]
criteria["department"] = {"operator": "IN", "values": depts}
# Enhanced NLP-based extraction
if nlp:
doc = nlp(text)
# Extract salary information
salary_pattern = re.search(r"salary (below|under|less than|above|over|more than) (\d+[k]?)", text, re.IGNORECASE)
if salary_pattern:
comparison = salary_pattern.group(1).lower()
amount = salary_pattern.group(2)
# Convert k to thousands
if amount.endswith('k'):
amount = int(amount[:-1]) * 1000
else:
amount = int(amount)
operator = "<" if comparison in ["below", "under", "less than"] else ">"
criteria["salary"] = {"operator": operator, "value": amount}
# Extract date ranges
for ent in doc.ents:
if ent.label_ == "DATE":
date_text = ent.text
if "since" in text[:ent.start].lower() or "after" in text[:ent.start].lower():
criteria["date_range"] = {"operator": ">", "value": date_text}
elif "before" in text[:ent.start].lower() or "until" in text[:ent.start].lower():
criteria["date_range"] = {"operator": "<", "value": date_text}
return criteria
** Added function to safely build SQL queries **
def build_safe_sql_query(template: str, criteria: Dict[str, Any]) -> tuple:
"""
Builds a safe SQL query using parameterized queries to prevent SQL injection.
Returns the query string and parameters tuple.
"""
params = []
conditions = []
code
for key, value in criteria.items():
if key == "hire_date":
conditions.append(f"hire_date {value['operator']} %s")
params.append(value['value'])
elif key == "department":
placeholders = ", ".join(["%s"] * len(value['values']))
conditions.append(f"department IN ({placeholders})")
params.extend(value['values'])
elif key == "salary":
conditions.append(f"salary {value['operator']} %s")
params.append(value['value'])
elif key == "date_range":
conditions.append(f"event_date {value['operator']} %s")
params.append(value['value'])
where_clause = " AND ".join(conditions) if conditions else "1=1"
query = template.replace("?", where_clause)
return query, tuple(params)
--- CrewAI Implementation ---
** Added CrewAI implementation **
def setup_crew():
"""
Sets up the CrewAI agents and tasks for policy analysis.
"""
# Define the language model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0, openai_api_key=openai_api_key)
code
# Create agents
policy_analyst = Agent(
role="Policy Analyst",
goal="Analyze policy documents and extract key compliance requirements",
backstory="You are an expert in corporate policy analysis with years of experience in compliance",
verbose=True,
llm=llm,
tools=[policy_retriever_tool]
)
data_analyst = Agent(
role="Data Analyst",
goal="Query databases to find compliance violations based on policy requirements",
backstory="You are a skilled data analyst who specializes in finding patterns in employee data",
verbose=True,
llm=llm,
tools=db_tools
)
compliance_officer = Agent(
role="Compliance Officer",
goal="Evaluate policy violations and provide recommendations",
backstory="You are a senior compliance officer responsible for ensuring company-wide policy adherence",
verbose=True,
llm=llm
)
# Create tasks
policy_analysis_task = Task(
description="Analyze policy documents to extract compliance requirements",
agent=policy_analyst,
expected_output="A list of key compliance requirements from policy documents"
)
data_analysis_task = Task(
description="Query the database to find potential policy violations",
agent=data_analyst,
expected_output="A list of potential policy violations with employee details"
)
compliance_evaluation_task = Task(
description="Evaluate violations and provide recommendations for compliance improvement",
agent=compliance_officer,
expected_output="A comprehensive compliance report with recommendations"
)
# Create the crew
crew = Crew(
agents=[policy_analyst, data_analyst, compliance_officer],
tasks=[policy_analysis_task, data_analysis_task, compliance_evaluation_task],
verbose=2,
process=Process.sequential
)
return crew
@app.post("/compliance/check", response_model=ComplianceResponse)
async def check_compliance(query: ComplianceQuery):
"""
Checks policy compliance based on natural language query.
Uses CrewAI to analyze policies and find violations.
"""
if not db:
raise HTTPException(status_code=503, detail="Database connection not available")
code
try:
# Extract criteria from query
criteria = extract_criteria_from_text(query.query_text)
# Add department filter if provided
if query.department:
criteria["department"] = {"operator": "=", "values": [query.department]}
# Build safe SQL query
template = SQL_QUERY_TEMPLATES["detailed_violations"]
sql_query, params = build_safe_sql_query(template, criteria)
# Execute query using database tools
# This is a simplified version - in production, you'd use the CrewAI setup
# crew = setup_crew()
# result = crew.kickoff(inputs={"query": query.query_text, "criteria": criteria})
# For demonstration, we'll mock the response
violations = [
PolicyViolation(
employee_id="E001",
first_name="John",
last_name="Doe",
department="Sales",
violation_type="Salary Policy",
policy_reference="Section 3.2 of Compensation Policy"
),
PolicyViolation(
employee_id="E002",
first_name="Jane",
last_name="Smith",
department="Marketing",
violation_type="Work Hours",
policy_reference="Section 2.1 of Employee Handbook"
)
]
# Get relevant policies
policy_docs = retriever.get_relevant_documents(query.query_text)
relevant_policies = [doc.metadata.get("source", "Unknown") for doc in policy_docs[:3]]
return ComplianceResponse(
query=query.query_text,
violations=violations,
total_count=len(violations),
relevant_policies=relevant_policies
)
except Exception as e:
logger.error(f"Error processing compliance check: {e}")
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
results = []
for i, doc in enumerate(docs[:5]): # Return top 5 results
results.append(
PolicySearchResult(
document_name=doc.metadata.get("source", "Unknown"),
content=doc.page_content[:500] + "..." if len(doc.page_content) > 500 else doc.page_content,
relevance_score=1.0 - (i * 0.1) # Mock relevance score
)
)
return PolicySearchResponse(
query=query.search_text,
results=results
)
except Exception as e:
logger.error(f"Error searching policy documents: {e}")
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
--- Main execution ---
if name == "main":
import uvicorn
code
# Setup CrewAI if needed
if db:
logger.info("Setting up CrewAI...")
crew = setup_crew()
# Run the FastAPI app
logger.info("Starting API server...")
uvicorn.run(app, host="0.0.0.0", port=8000)
To run the provided FastAPI application code, follow these steps:
1. Prerequisites
Python 3.8 or newer (recommended Python 3.9+)
Install required Python packages (see below)
You should have environment variables configured (especially OPENAI_API_KEY and your DB credentials)
Ensure you have the MongoDB/MySQL database ready (as per your connection settings)
2. Save the Code
Save the entire code into a file, e.g., main.py.
3. Create .env file
In the same directory, create a .env file to store environment variables:
DOCS_PATH is the path where your policy documents (.pdf or .docx) are stored.
4. Install Python Dependencies
Because your code uses many packages including FastAPI, Uvicorn, Spacy, NLTK, LangChain, Chroma, dotenv, CrewAI, and database connectors, install them via pip.
You can create a requirements.txt file with these:
code
fastapi
uvicorn
python-dotenv
spacy
nltk
langchain
chromadb
pymysql
mysqlclient
crew-ai # If that's the package for crewai (verify the correct package name)
pydantic
Note:
Some modules like langchain_community and crewai might need to be installed separately or from GitHub repo if not on PyPI.
For Spacy, download the en_core_web_sm model too.
Run these commands:
bash
pip install fastapi uvicorn python-dotenv spacy nltk pymysql pydantic
pip install langchain chromadb
# For langchain_community and crewai, try:
pip install langchain-community # if available
pip install crew-ai # or equivalent; check package name or repo
# Download Spacy English model:
python -m spacy download en_core_web_sm
# Download NLTK punkt tokenizer:
python -m nltk.downloader punkt
If any package is missing or unavailable, check the documentation for alternative installation instructions.
5. Prepare Your Documents Folder
Make sure the folder you set as DOCS_PATH exists and contains your .pdf and .docx policy documents.
The log output shows server started at http://0.0.0.0:8000
--reload option is useful for development (automatic code reload)
7. Test the API
Open your browser or use curl/Postman to access:
code
GET http://localhost:8000/
You should see the API info JSON.
Example POST request to check compliance:
code
POST http://localhost:8000/compliance/check
Content-Type: application/json
{
"query_text": "Are there any employees hired before 2020 violating salary policies?",
"department": "Sales"
}
Example POST to search policies:
code
POST http://localhost:8000/policy/search
Content-Type: application/json
{
"search_text": "compensation"
}
Important Notes
The code uses a MySQL database (you must have access credentials, MySQL server running, and correct DB).
The LLM integration (OpenAI GPT-4o-mini) requires the OpenAI API key.
langchain_community and crewai packages are less standard; make sure you have them installed as required.
The document loaders expect PDFs and DOCX files in your docs folder.
The CrewAI setup is configured but is not fully integrated in the /compliance/check endpoint (mocked response given).
Troubleshooting
Missing package errors: verify package names and install.
Errors in loading Spacy model: run python -m spacy download en_core_web_sm.
Database connection errors: check credentials and network access.
OpenAI errors: ensure your API key is valid and has sufficient quota.
This should get your FastAPI server running with all core features.
If you want me to help creating a requirements.txt file or dockerizing this app, just ask!