Message Safety Filter API

This API provides services to check messages for safety using an AI-powered safety agent and to manage system prompts.

Filter Message

POST /filter_message

Request

Content-Type: application/json

{
    "message": "string",
    "prompt_type": "string",
    "prompt_version": "string"
}
        

Response

{
    "is_safe": boolean,
    "safety_reason": "string",
    "prompt_used": {
        "type": "string",
        "version": "string"
    }
}
        

Example Request

curl -X POST http://localhost:5000/filter_message \
    -H "Content-Type: application/json" \
    -d '{"message": "Hello, how are you today?", "prompt_type": "safety", "prompt_version": "1.0"}'
        

Example Responses

Safe Message:

{
    "is_safe": true,
    "safety_reason": "The message appears to be a friendly greeting without any concerning content.",
    "prompt_used": {
        "type": "safety",
        "version": "1.0"
    }
}
        

Unsafe Message:

{
    "is_safe": false,
    "safety_reason": "The message contains inappropriate language or harmful content.",
    "prompt_used": {
        "type": "safety",
        "version": "1.0"
    }
}
        

Error Response

{
    "error": "Missing required field: message"
}
        

Get System Prompt

GET /get_system_prompt

Parameters

Response

{
    "id": integer,
    "content": "string",
    "type": "string",
    "version": "string",
    "created_at": "datetime",
    "updated_at": "datetime"
}
        

Example Request

curl -X GET "http://localhost:5000/get_system_prompt?type=system&version=1.0"
        

Example Response

{
    "id": 1,
    "content": "You are a helpful and professional seller assistant...",
    "type": "system",
    "version": "1.0",
    "created_at": "2023-06-15T10:30:00.000Z",
    "updated_at": "2023-06-15T10:30:00.000Z"
}
        

Error Response

{
    "error": "No prompt found for type: system and version: 1.0"
}
        

Save System Prompt

POST /save_system_prompt

Request

Content-Type: application/json

{
    "content": "string",
    "type": "string" (optional, defaults to "safety"),
    "version": "string" (optional)
}
        

Response

{
    "id": integer,
    "content": "string",
    "type": "string",
    "version": "string",
    "created_at": "datetime",
    "updated_at": "datetime",
    "message": "Prompt saved successfully"
}
        

Example Request

curl -X POST http://localhost:5000/save_system_prompt \
    -H "Content-Type: application/json" \
    -d '{
        "content": "You are a helpful and professional seller assistant...",
        "type": "system",
        "version": "1.0"
    }'
        

Example Response

{
    "id": 1,
    "content": "You are a helpful and professional seller assistant...",
    "type": "system",
    "version": "1.0",
    "created_at": "2023-06-15T10:30:00.000Z",
    "updated_at": "2023-06-15T10:30:00.000Z",
    "message": "Prompt saved successfully"
}
        

Error Response

{
    "error": "Missing required field: content"
}
        

List Prompts

GET /list_prompts

Parameters

Response

When types_only=false (default):

{
    "prompts": [
        {
            "id": integer,
            "type": "string",
            "version": "string",
            "created_at": "datetime",
            "updated_at": "datetime"
        },
        ...
    ]
}
        

When types_only=true:

{
    "prompt_types": [
        "string",
        ...
    ]
}
        

Example Requests

List all prompts:

curl -X GET "http://localhost:5000/list_prompts"
        

List prompts of a specific type:

curl -X GET "http://localhost:5000/list_prompts?type=system"
        

List only prompt types:

curl -X GET "http://localhost:5000/list_prompts?types_only=true"
        

Example Responses

List all prompts:

{
    "prompts": [
        {
            "id": 1,
            "type": "system",
            "version": "1.0",
            "created_at": "2023-06-15T10:30:00.000Z",
            "updated_at": "2023-06-15T10:30:00.000Z"
        },
        {
            "id": 2,
            "type": "system",
            "version": "2.0",
            "created_at": "2023-06-16T14:45:00.000Z",
            "updated_at": "2023-06-16T14:45:00.000Z"
        },
        {
            "id": 3,
            "type": "safety",
            "version": "v20230615103000",
            "created_at": "2023-06-15T10:30:00.000Z",
            "updated_at": "2023-06-15T10:30:00.000Z"
        }
    ]
}
        

List only prompt types:

{
    "prompt_types": [
        "system",
        "safety"
    ]
}
        

Error Response

{
    "error": "Internal server error"
}
        

Get Violation Count

GET /get_violation_count/{user_id}

Parameters

Response

{
    "user_id": integer,
    "violation_count": integer
}
        

Example Request

curl -X GET "http://localhost:5000/get_violation_count/1001"
        

Example Response

{
    "user_id": 1001,
    "violation_count": 5
}
        

Error Response

{
    "error": "Internal server error"
}
        

Testing the API

You can test the API using tools like:

Python Example

import requests

# Filter a message
filter_url = 'http://localhost:5000/filter_message'
filter_data = {
    'message': 'Hello, how are you today?'
}
filter_response = requests.post(filter_url, json=filter_data)
print("Filter Response:", filter_response.json())

# Get violation count for a user
violation_url = 'http://localhost:5000/get_violation_count/1001'
violation_response = requests.get(violation_url)
print("Violation Count Response:", violation_response.json())

# Get a system prompt
get_prompt_url = 'http://localhost:5000/get_system_prompt'
get_prompt_params = {
    'type': 'system',
    'version': '1.0'
}
get_prompt_response = requests.get(get_prompt_url, params=get_prompt_params)
print("Get Prompt Response:", get_prompt_response.json())

# List all prompts
list_prompts_url = 'http://localhost:5000/list_prompts'
list_prompts_response = requests.get(list_prompts_url)
print("List Prompts Response:", list_prompts_response.json())