Skip to content

Commit 31c1d1a

Browse files
committed
Implement graph
1 parent e3c0090 commit 31c1d1a

File tree

12 files changed

+307
-147
lines changed

12 files changed

+307
-147
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.pdf
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

DocsManager/app/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Settings(BaseSettings):
3030
minio_secret_key: str
3131
minio_bucket: str = "documents"
3232
minio_use_ssl: bool = True
33+
minio_folder: str = "rag-docs" # Folder within bucket for RAG documents
3334

3435
# Database Configuration (for SQLAlchemy)
3536
database_url: str = ""

DocsManager/app/models/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Models for the DocsManager application."""
2+
3+
from app.models.document import Document
4+
from app.models.document_chunks import DocumentChunk
5+
6+
__all__ = ["Document", "DocumentChunk"]
7+

DocsManager/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from app.api.routes import admin, base
55
from app.core.db_connection import init_db
6+
# Import models to ensure SQLAlchemy can resolve relationships
7+
from app.models import Document, DocumentChunk # noqa: F401
68

79
# Configure logging
810
logging.basicConfig(

RAGManager/.dockerignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.venv
2+
__pycache__
3+
*.pyc
4+
*.pyo
5+
*.pyd
6+
.Python
7+
*.so
8+
*.egg
9+
*.egg-info
10+
dist
11+
build
12+
.git
13+
.gitignore
14+
.idea
15+
.vscode
16+
*.swp
17+
*.swo
18+
*~
19+
.DS_Store
20+
.env
21+
.env.local
22+
*.log
23+

RAGManager/Dockerfile

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,31 @@ COPY pyproject.toml uv.lock* ./
1919

2020
RUN uv sync --no-dev --no-cache
2121

22-
# Run the Guardrails configure command to create a .guardrailsrc file
23-
# Only configure if GUARDRAILS_API_KEY is provided
24-
RUN uv run guardrails configure --enable-metrics --enable-remote-inferencing --token "$GUARDRAILS_API_KEY"
25-
26-
# Install required guardrails validators
27-
RUN uv run guardrails hub install hub://guardrails/detect_jailbreak --no-install-local-models
28-
RUN uv run guardrails hub install hub://guardrails/detect_pii --no-install-local-models
29-
RUN uv run guardrails hub install hub://guardrails/toxic_language>=0.0.2 --no-install-local-models
30-
31-
# The ToxicLanguage Validator uses the punkt tokenizer, so we need to download that to a known directory
32-
# Set the directory for nltk data
33-
ENV NLTK_DATA=/opt/nltk_data
34-
35-
# Download punkt data
36-
RUN uv run python -m nltk.downloader -d /opt/nltk_data punkt_tab
22+
# Guardrails disabled for now to speed up builds
23+
# # Run the Guardrails configure command to create a .guardrailsrc file
24+
# # Only configure if GUARDRAILS_API_KEY is provided
25+
# RUN if [ -n "$GUARDRAILS_API_KEY" ]; then \
26+
# uv run guardrails configure --enable-metrics --enable-remote-inferencing --token "$GUARDRAILS_API_KEY"; \
27+
# else \
28+
# echo "Warning: GUARDRAILS_API_KEY not provided, skipping guardrails configuration"; \
29+
# fi
30+
31+
# # Install required guardrails validators
32+
# # Note: Removing --no-install-local-models to ensure Python packages are installed
33+
# RUN uv run guardrails hub install hub://guardrails/detect_jailbreak
34+
# RUN uv run guardrails hub install hub://guardrails/detect_pii
35+
# RUN uv run guardrails hub install hub://guardrails/toxic_language>=0.0.2
36+
37+
# # Verify packages are accessible (this will fail the build if they're not found)
38+
# RUN uv run python -c "from guardrails.hub import DetectJailbreak, DetectPII, ToxicLanguage; print('Guardrails validators imported successfully')"
39+
40+
# NLTK disabled - only needed for guardrails ToxicLanguage validator
41+
# # The ToxicLanguage Validator uses the punkt tokenizer, so we need to download that to a known directory
42+
# # Set the directory for nltk data
43+
# ENV NLTK_DATA=/opt/nltk_data
44+
#
45+
# # Download punkt data
46+
# RUN uv run python -m nltk.downloader -d /opt/nltk_data punkt_tab
3747

3848
COPY . .
3949

RAGManager/app/agents/nodes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from app.agents.nodes.context_builder import context_builder
55
from app.agents.nodes.fallback_final import fallback_final
66
from app.agents.nodes.fallback_inicial import fallback_inicial
7-
from app.agents.nodes.generator import generator
87
from app.agents.nodes.guard_final import guard_final
98
from app.agents.nodes.guard_inicial import guard_inicial
109
from app.agents.nodes.parafraseo import parafraseo
@@ -15,6 +14,7 @@
1514
"guard_inicial",
1615
"guard_final",
1716
"fallback_inicial",
17+
"fallback_final",
1818
"parafraseo",
1919
"retriever",
2020
"context_builder",

RAGManager/app/agents/nodes/context_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def context_builder(state: AgentState) -> AgentState:
5555
logger.warning("No relevant chunks found for context building")
5656

5757
# Create enriched query combining paraphrased text and context
58-
enriched_query = f"""Pregunta del usuario: {paraphrased}
58+
enriched_query = f"""Pregunta del usuario: {paraphrased}
5959
6060
Contexto relevante de la base de conocimiento:
6161
{context_section}

RAGManager/app/agents/nodes/guard_final.py

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22

33
import logging
44

5-
from guardrails import Guard
6-
from guardrails.hub import DetectPII, ToxicLanguage
5+
# Guardrails disabled for now
6+
# from guardrails import Guard
7+
# from guardrails.hub import DetectPII, ToxicLanguage
78

89
from app.agents.state import AgentState
9-
from app.core.config import settings
10+
# from app.core.config import settings
1011

1112
logger = logging.getLogger(__name__)
1213

13-
# Initialize Guard with DetectPII and ToxicLanguage validators
14-
# Note: The validators must be installed via:
15-
# guardrails hub install hub://guardrails/detect_pii
16-
# guardrails hub install hub://guardrails/toxic_language
17-
_guard_final = Guard().use(
18-
DetectPII(
19-
pii_entities=settings.guardrails_pii_entities,
20-
on_fail="noop", # Don't raise exceptions, handle via state flags
21-
)
22-
).use(
23-
ToxicLanguage(
24-
on_fail="noop", # Don't raise exceptions, handle via state flags
25-
)
26-
)
14+
# Guardrails disabled for now - just pass through
15+
# # Initialize Guard with DetectPII and ToxicLanguage validators
16+
# # Note: The validators must be installed via:
17+
# # guardrails hub install hub://guardrails/detect_pii
18+
# # guardrails hub install hub://guardrails/toxic_language
19+
# _guard_final = Guard().use(
20+
# DetectPII(
21+
# pii_entities=settings.guardrails_pii_entities,
22+
# on_fail="noop", # Don't raise exceptions, handle via state flags
23+
# )
24+
# ).use(
25+
# ToxicLanguage(
26+
# on_fail="noop", # Don't raise exceptions, handle via state flags
27+
# )
28+
# )
2729

2830

2931
def guard_final(state: AgentState) -> AgentState:
@@ -50,31 +52,36 @@ def guard_final(state: AgentState) -> AgentState:
5052
updated_state["error_message"] = None
5153
return updated_state
5254

53-
try:
54-
# Validate the generated response using Guardrails
55-
validation_result = _guard_final.validate(generated_response)
56-
57-
# Check if validation passed
58-
# The validator returns ValidationResult with outcome
59-
# If validation fails, outcome will indicate failure
60-
if validation_result.validation_passed:
61-
updated_state["is_risky"] = False
62-
updated_state["error_message"] = None
63-
logger.debug("Generated response passed PII and toxic language detection")
64-
else:
65-
# PII or toxic language detected
66-
updated_state["is_risky"] = True
67-
updated_state["error_message"] = (
68-
"Contenido riesgoso detectado en la respuesta generada. La información solicitada está clasificada o no es de libre acceso."
69-
)
70-
logger.warning("Risky content detected in generated response. Response content not logged for security.")
71-
72-
except Exception as e:
73-
# If validation fails due to error, log it but don't block the request
74-
# This is a safety measure - if Guardrails fails, we allow the request
75-
# but log the error for monitoring
76-
logger.error(f"Error during PII detection: {e}")
77-
updated_state["is_risky"] = False
78-
updated_state["error_message"] = None
55+
# Guardrails disabled for now - just pass through
56+
updated_state["is_risky"] = False
57+
updated_state["error_message"] = None
58+
logger.debug("Guardrails disabled - response passed through without validation")
59+
60+
# try:
61+
# # Validate the generated response using Guardrails
62+
# validation_result = _guard_final.validate(generated_response)
63+
#
64+
# # Check if validation passed
65+
# # The validator returns ValidationResult with outcome
66+
# # If validation fails, outcome will indicate failure
67+
# if validation_result.validation_passed:
68+
# updated_state["is_risky"] = False
69+
# updated_state["error_message"] = None
70+
# logger.debug("Generated response passed PII and toxic language detection")
71+
# else:
72+
# # PII or toxic language detected
73+
# updated_state["is_risky"] = True
74+
# updated_state["error_message"] = (
75+
# "Contenido riesgoso detectado en la respuesta generada. La información solicitada está clasificada o no es de libre acceso."
76+
# )
77+
# logger.warning("Risky content detected in generated response. Response content not logged for security.")
78+
#
79+
# except Exception as e:
80+
# # If validation fails due to error, log it but don't block the request
81+
# # This is a safety measure - if Guardrails fails, we allow the request
82+
# # but log the error for monitoring
83+
# logger.error(f"Error during PII detection: {e}")
84+
# updated_state["is_risky"] = False
85+
# updated_state["error_message"] = None
7986

8087
return updated_state

RAGManager/app/agents/nodes/guard_inicial.py

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
import logging
44

5-
from guardrails import Guard
6-
from guardrails.hub import DetectJailbreak, ToxicLanguage
5+
# Guardrails disabled for now
6+
# from guardrails import Guard
7+
# from guardrails.hub import DetectJailbreak, ToxicLanguage
78

89
from app.agents.state import AgentState
9-
from app.core.config import settings
10+
# from app.core.config import settings
1011

1112
logger = logging.getLogger(__name__)
1213

13-
# Initialize Guard with DetectJailbreak and ToxicLanguage validators
14-
# Note: The validators must be installed via:
15-
# guardrails hub install hub://guardrails/detect_jailbreak
16-
# guardrails hub install hub://guardrails/toxic_language
17-
_guard_inicial = Guard().use(
18-
DetectJailbreak(
19-
threshold=settings.guardrails_jailbreak_threshold,
20-
device=settings.guardrails_device,
21-
on_fail="noop", # Don't raise exceptions, handle via state flags
22-
)
23-
).use(
24-
ToxicLanguage(
25-
on_fail="noop", # Don't raise exceptions, handle via state flags
26-
)
27-
)
14+
# Guardrails disabled for now - just pass through
15+
# # Initialize Guard with DetectJailbreak and ToxicLanguage validators
16+
# # Note: The validators must be installed via:
17+
# # guardrails hub install hub://guardrails/detect_jailbreak
18+
# # guardrails hub install hub://guardrails/toxic_language
19+
# _guard_inicial = Guard().use(
20+
# DetectJailbreak(
21+
# threshold=settings.guardrails_jailbreak_threshold,
22+
# device=settings.guardrails_device,
23+
# on_fail="noop", # Don't raise exceptions, handle via state flags
24+
# )
25+
# ).use(
26+
# ToxicLanguage(
27+
# on_fail="noop", # Don't raise exceptions, handle via state flags
28+
# )
29+
# )
2830

2931

3032
def guard_inicial(state: AgentState) -> AgentState:
@@ -53,31 +55,36 @@ def guard_inicial(state: AgentState) -> AgentState:
5355
updated_state["error_message"] = None
5456
return updated_state
5557

56-
try:
57-
# Validate the prompt using Guardrails
58-
validation_result = _guard_inicial.validate(prompt)
59-
60-
# Check if validation passed
61-
# The validator returns ValidationResult with outcome
62-
# If validation fails, outcome will indicate failure
63-
if validation_result.validation_passed:
64-
updated_state["is_malicious"] = False
65-
updated_state["error_message"] = None
66-
logger.debug("Prompt passed jailbreak and toxic language detection")
67-
else:
68-
# Jailbreak or toxic language detected
69-
updated_state["is_malicious"] = True
70-
updated_state["error_message"] = (
71-
"Contenido malicioso detectado. Tu solicitud contiene contenido que viola las políticas de seguridad."
72-
)
73-
logger.warning("Malicious content detected. Prompt content not logged for security.")
74-
75-
except Exception as e:
76-
# If validation fails due to error, log it but don't block the request
77-
# This is a safety measure - if Guardrails fails, we allow the request
78-
# but log the error for monitoring
79-
logger.error(f"Error during jailbreak detection: {e}")
80-
updated_state["is_malicious"] = False
81-
updated_state["error_message"] = None
58+
# Guardrails disabled for now - just pass through
59+
updated_state["is_malicious"] = False
60+
updated_state["error_message"] = None
61+
logger.debug("Guardrails disabled - prompt passed through without validation")
62+
63+
# try:
64+
# # Validate the prompt using Guardrails
65+
# validation_result = _guard_inicial.validate(prompt)
66+
#
67+
# # Check if validation passed
68+
# # The validator returns ValidationResult with outcome
69+
# # If validation fails, outcome will indicate failure
70+
# if validation_result.validation_passed:
71+
# updated_state["is_malicious"] = False
72+
# updated_state["error_message"] = None
73+
# logger.debug("Prompt passed jailbreak and toxic language detection")
74+
# else:
75+
# # Jailbreak or toxic language detected
76+
# updated_state["is_malicious"] = True
77+
# updated_state["error_message"] = (
78+
# "Contenido malicioso detectado. Tu solicitud contiene contenido que viola las políticas de seguridad."
79+
# )
80+
# logger.warning("Malicious content detected. Prompt content not logged for security.")
81+
#
82+
# except Exception as e:
83+
# # If validation fails due to error, log it but don't block the request
84+
# # This is a safety measure - if Guardrails fails, we allow the request
85+
# # but log the error for monitoring
86+
# logger.error(f"Error during jailbreak detection: {e}")
87+
# updated_state["is_malicious"] = False
88+
# updated_state["error_message"] = None
8289

8390
return updated_state

0 commit comments

Comments
 (0)