Skip to content

Commit bca7127

Browse files
committed
resolved comments
1 parent 85b9a94 commit bca7127

File tree

12 files changed

+102
-18
lines changed

12 files changed

+102
-18
lines changed

backend/app/alembic/versions/001_added_request_log.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def upgrade() -> None:
2323
op.create_table(
2424
"request_log",
2525
sa.Column("id", sa.Uuid(), nullable=False),
26+
sa.Column("organization_id", sa.Integer(), nullable=False),
27+
sa.Column("project_id", sa.Integer(), nullable=False),
2628
sa.Column("request_id", sa.Uuid(), nullable=False),
2729
sa.Column("response_id", sa.Uuid(), nullable=True),
2830
sa.Column(

backend/app/alembic/versions/002_added_validator_log.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def upgrade() -> None:
2323
op.create_table(
2424
"validator_log",
2525
sa.Column("id", sa.Uuid(), nullable=False),
26+
sa.Column("organization_id", sa.Integer(), nullable=False),
27+
sa.Column("project_id", sa.Integer(), nullable=False),
2628
sa.Column("request_id", sa.Uuid(), nullable=False),
2729
sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
2830
sa.Column("input", sqlmodel.sql.sqltypes.AutoString(), nullable=False),

backend/app/alembic/versions/004_added_log_indexes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def upgrade() -> None:
2121
op.create_index("idx_request_log_request_id", "request_log", ["request_id"])
2222
op.create_index("idx_request_log_status", "request_log", ["status"])
2323
op.create_index("idx_request_log_inserted_at", "request_log", ["inserted_at"])
24+
op.create_index(
25+
"ix_request_log_organization_id", "request_log", ["organization_id"]
26+
)
27+
op.create_index("ix_request_log_project_id", "request_log", ["project_id"])
2428

2529
op.create_index("idx_validator_log_request_id", "validator_log", ["request_id"])
2630
op.create_index("idx_validator_log_inserted_at", "validator_log", ["inserted_at"])
@@ -37,3 +41,5 @@ def downgrade() -> None:
3741
op.drop_index("idx_request_log_inserted_at", table_name="request_log")
3842
op.drop_index("idx_request_log_status", table_name="request_log")
3943
op.drop_index("idx_request_log_request_id", table_name="request_log")
44+
op.drop_index("idx_request_log_organization_id", table_name="request_log")
45+
op.drop_index("idx_request_log_project_id", table_name="request_log")

backend/app/alembic/versions/004_added_banlist_config.py renamed to backend/app/alembic/versions/005_added_banlist_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Added ban_list table
22
3-
Revision ID: 004
4-
Revises: 003
3+
Revision ID: 005
4+
Revises: 004
55
Create Date: 2026-02-05 09:42:54.128852
66
77
"""
@@ -12,8 +12,8 @@
1212
import sqlalchemy as sa
1313

1414
# revision identifiers, used by Alembic.
15-
revision: str = '004'
16-
down_revision = '003'
15+
revision: str = "005"
16+
down_revision = "004"
1717
branch_labels = None
1818
depends_on = None
1919

backend/app/api/routes/guardrails.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ def run_guardrails(
3737
except ValueError:
3838
return APIResponse.failure_response(error="Invalid request_id")
3939

40-
request_log = request_log_crud.create(request_id, input_text=payload.input)
40+
request_log = request_log_crud.create(request_id, payload)
4141
return _validate_with_guard(
42-
payload.input,
43-
payload.validators,
42+
payload,
4443
request_log_crud,
4544
request_log.id,
4645
validator_log_crud,
@@ -79,8 +78,7 @@ def list_validators(_: AuthDep):
7978

8079

8180
def _validate_with_guard(
82-
data: str,
83-
validators: list,
81+
payload: GuardrailRequest,
8482
request_log_crud: RequestLogCrud,
8583
request_log_id: UUID,
8684
validator_log_crud: ValidatorLogCrud,
@@ -94,6 +92,8 @@ def _validate_with_guard(
9492
while still safely handling unexpected runtime errors.
9593
"""
9694
response_id = uuid.uuid4()
95+
data = payload.input
96+
validators = payload.validators
9797
guard: Guard | None = None
9898

9999
def _finalize(
@@ -125,7 +125,7 @@ def _finalize(
125125

126126
if guard is not None:
127127
add_validator_logs(
128-
guard, request_log_id, validator_log_crud, suppress_pass_logs
128+
guard, request_log_id, validator_log_crud, payload, suppress_pass_logs
129129
)
130130

131131
rephrase_needed = validated_output is not None and validated_output.startswith(
@@ -175,6 +175,7 @@ def add_validator_logs(
175175
guard: Guard,
176176
request_log_id: UUID,
177177
validator_log_crud: ValidatorLogCrud,
178+
payload: GuardrailRequest,
178179
suppress_pass_logs: bool = False,
179180
):
180181
history = getattr(guard, "history", None)
@@ -202,6 +203,8 @@ def add_validator_logs(
202203

203204
validator_log = ValidatorLog(
204205
request_id=request_log_id,
206+
organization_id=payload.organization_id,
207+
project_id=payload.project_id,
205208
name=log.validator_name,
206209
input=str(log.value_before_validation),
207210
output=log.value_after_validation,

backend/app/crud/request_log.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
from sqlmodel import Session
44

55
from app.models.logging.request_log import RequestLog, RequestLogUpdate, RequestStatus
6+
from app.schemas.guardrail_config import GuardrailRequest, GuardrailResponse
67
from app.utils import now
78

89

910
class RequestLogCrud:
1011
def __init__(self, session: Session):
1112
self.session = session
1213

13-
def create(self, request_id: UUID, input_text: str) -> RequestLog:
14+
def create(self, request_id: UUID, payload: GuardrailRequest) -> RequestLog:
1415
create_request_log = RequestLog(
1516
request_id=request_id,
16-
request_text=input_text,
17+
request_text=payload.input,
18+
organization_id=payload.organization_id,
19+
project_id=payload.project_id,
1720
)
1821
self.session.add(create_request_log)
1922
self.session.commit()

backend/app/models/logging/request_log.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class RequestLog(SQLModel, table=True):
2424
sa_column_kwargs={"comment": "Unique identifier for the request log entry"},
2525
)
2626

27+
organization_id: int = Field(
28+
index=True,
29+
sa_column_kwargs={"comment": "Identifier for the organization"},
30+
)
31+
32+
project_id: int = Field(
33+
nullable=False,
34+
sa_column_kwargs={"comment": "Identifier for the project"},
35+
)
36+
2737
request_id: UUID = Field(
2838
nullable=False,
2939
sa_column_kwargs={"comment": "Identifier for the request"},

backend/app/models/logging/validator_log.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class ValidatorLog(SQLModel, table=True):
2121
sa_column_kwargs={"comment": "Unique identifier for the validator log entry"},
2222
)
2323

24+
organization_id: int = Field(
25+
index=True,
26+
sa_column_kwargs={"comment": "Identifier for the organization"},
27+
)
28+
29+
project_id: int = Field(
30+
nullable=False,
31+
sa_column_kwargs={"comment": "Identifier for the project"},
32+
)
33+
2434
request_id: UUID = Field(
2535
foreign_key="request_log.id",
2636
nullable=False,

backend/app/schemas/guardrail_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
class GuardrailRequest(SQLModel):
3535
model_config = ConfigDict(extra="forbid")
3636
request_id: str
37+
organization_id: int
38+
project_id: int
3739
input: str
3840
validators: List[ValidatorConfigItem]
3941

backend/app/tests/test_guardrails_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import pytest
44

55
from app.tests.guardrails_mocks import MockResult
6+
from app.tests.seed_data import (
7+
VALIDATOR_TEST_ORGANIZATION_ID,
8+
VALIDATOR_TEST_PROJECT_ID,
9+
)
610
from app.tests.utils.constants import SAFE_TEXT_FIELD, VALIDATE_API_PATH
711

812
build_guard_path = "app.api.routes.guardrails.build_guard"
913
crud_path = "app.api.routes.guardrails.RequestLogCrud"
1014

1115
request_id = "123e4567-e89b-12d3-a456-426614174000"
16+
organization_id = VALIDATOR_TEST_ORGANIZATION_ID
17+
project_id = VALIDATOR_TEST_PROJECT_ID
1218

1319

1420
@pytest.fixture
@@ -34,6 +40,8 @@ def validate(self, data):
3440
VALIDATE_API_PATH,
3541
json={
3642
"request_id": request_id,
43+
"organization_id": organization_id,
44+
"project_id": project_id,
3745
"input": "hello world",
3846
"validators": [],
3947
},
@@ -57,6 +65,8 @@ def validate(self, data):
5765
VALIDATE_API_PATH,
5866
json={
5967
"request_id": request_id,
68+
"organization_id": organization_id,
69+
"project_id": project_id,
6070
"input": "my phone is 999999",
6171
"validators": [],
6272
},
@@ -76,6 +86,8 @@ def test_guardrails_internal_error(client, mock_crud):
7686
VALIDATE_API_PATH,
7787
json={
7888
"request_id": request_id,
89+
"organization_id": organization_id,
90+
"project_id": project_id,
7991
"input": "text",
8092
"validators": [],
8193
},

0 commit comments

Comments
 (0)