SiamCafe · Blog
Midjourney Prompt Audit Trail Logging — ติดตาม
บทความ

Midjourney Prompt Audit Trail Logging — ติดตาม

เผยแพร่ 28 พฤษภาคม 2569

Midjourney Audit Trail

Midjourney Prompt Audit Trail Logging AI Image Generation Compliance Governance Cost Tracking Usage Analytics Structured Logging Elasticsearch Dashboard

AI Image ToolAPIAudit LogEnterpriseราคา
MidjourneyDiscord Botไม่มี Built-inไม่มี$10-60/mo
DALL-E 3OpenAI APIAPI Logsมี$0.04-0.08/img
Stable DiffusionSelf-hostedCustomCustomGPU Cost
Adobe FireflyAdobe APIมีมีEnterprise

Logging Architecture

# === AI Prompt Logging System ===

# Architecture:
# User -> Proxy API -> AI Service (Midjourney/DALL-E)
#                   -> Logger -> Elasticsearch
#                   -> Cost Tracker -> Database
#                   -> Policy Engine -> Filter/Block

# Python — Prompt Logger
# import json
# import datetime
# from elasticsearch import Elasticsearch
#
# es = Elasticsearch("http://elasticsearch:9200")
#
# class PromptLogger:
#     def __init__(self):
#         self.es = es
#
#     def log_prompt(self, user_id, prompt, params, result):
#         doc = {
#             "timestamp": datetime.datetime.utcnow().isoformat(),
#             "user_id": user_id,
#             "prompt": prompt,
#             "parameters": params,
#             "result_url": result.get("url"),
#             "model": params.get("model", "midjourney-v6"),
#             "cost_usd": self.calculate_cost(params),
#             "status": result.get("status", "success"),
#             "tokens_used": result.get("tokens", 0),
#             "generation_time_ms": result.get("time_ms", 0),
#             "content_flags": self.check_content(prompt),
#         }
#         self.es.index(index="ai-prompts", document=doc)
#         return doc
#
#     def calculate_cost(self, params):
#         base = 0.04  # per image
#         if params.get("quality") == "hd": base = 0.08
#         count = params.get("n", 1)
#         return base * count
#
#     def check_content(self, prompt):
#         blocked_terms = ["violence", "explicit", "harmful"]
#         flags = [t for t in blocked_terms if t in prompt.lower()]
#         return flags

from dataclasses import dataclass
from typing import List

@dataclass
class PromptLog:
    user: str
    prompt: str
    model: str
    cost: float
    status: str
    flags: int
    time_ms: int

logs = [
    PromptLog("alice", "futuristic city skyline, cyberpunk style", "midjourney-v6", 0.04, "success", 0, 12500),
    PromptLog("bob", "product photo, white background, minimal", "dall-e-3", 0.08, "success", 0, 8200),
    PromptLog("carol", "logo design, modern tech company", "midjourney-v6", 0.04, "success", 0, 15300),
    PromptLog("dave", "abstract art, colorful geometric", "stable-diffusion", 0.01, "success", 0, 5400),
    PromptLog("eve", "blocked content attempt", "midjourney-v6", 0.00, "blocked", 1, 50),
]

print("=== Prompt Audit Log ===")
for l in logs:
    flag_str = "FLAGGED" if l.flags > 0 else "OK"
    print(f"  [{l.status}] {l.user} — {l.model} ({flag_str})")
    print(f"    Prompt: {l.prompt[:50]}... | Cost:  | Time: {l.time_ms}ms")

Policy Engine

# === AI Usage Policy Engine ===

# Policy Configuration
# policies:
#   content_filter:
#     blocked_categories: [violence, explicit, hate_speech, deepfake]
#     action: block_and_alert
#   cost_limits:
#     per_user_daily: 5.00  # USD
#     per_team_monthly: 500.00
#     action: warn_at_80_block_at_100
#   approval_required:
#     for_external_use: true
#     for_marketing: true
#     approvers: [manager, legal]
#   retention:
#     prompts: 365  # days
#     images: 180
#     logs: 730

@dataclass
class PolicyRule:
    name: str
    type: str
    threshold: str
    action: str
    violations_30d: int

policies = [
    PolicyRule("Content Filter", "Block", "Blocked categories", "Block + Alert Admin", 3),
    PolicyRule("Daily Cost Limit", "Budget", "$5/user/day", "Warn 80%, Block 100%", 12),
    PolicyRule("Monthly Team Budget", "Budget", "$500/team/month", "Alert Manager", 2),
    PolicyRule("External Use Approval", "Workflow", "Marketing/External", "Require Approval", 8),
    PolicyRule("Prompt Length", "Limit", "Max 500 chars", "Truncate + Warn", 5),
    PolicyRule("Rate Limit", "Throttle", "20 images/hour", "Queue + Wait", 15),
]

print("\n=== Policy Rules ===")
for p in policies:
    print(f"  [{p.type}] {p.name}")
    print(f"    Threshold: {p.threshold} | Action: {p.action} | Violations: {p.violations_30d}")

# Cost Dashboard
cost_data = {
    "Total Spend (30d)": "$1,245",
    "Images Generated": "8,500",
    "Avg Cost/Image": "$0.15",
    "Top User": "alice ($180)",
    "Top Team": "Marketing ($450)",
    "Blocked Prompts": "15 (0.18%)",
    "Approval Pending": "3",
    "Budget Remaining": "$755 (60.6%)",
}

print(f"\nCost Dashboard:")
for k, v in cost_data.items():
    print(f"  {k}: {v}")

Compliance และ Dashboard

# === Compliance Dashboard ===

# Grafana Dashboard Queries (Elasticsearch)
# Total prompts: count(ai-prompts) where timestamp > now-30d
# Blocked rate: count(status=blocked) / count(*) * 100
# Cost by team: sum(cost_usd) group by team
# Top models: count(*) group by model
# Hourly usage: count(*) group by date_histogram(1h)

# Content Provenance — C2PA Standard
# C2PA (Coalition for Content Provenance and Authenticity)
# Embed metadata in generated images:
# - Generator: Midjourney v6 / DALL-E 3
# - Prompt: (hashed or full)
# - Timestamp
# - User/Organization
# - Digital Signature

# Kibana Saved Searches
# - All blocked prompts (last 7 days)
# - Cost anomalies (> 2x average)
# - New users first generation
# - Failed generations
# - Policy violations by user

compliance = {
    "EU AI Act": "Transparency ต้องระบุว่าสร้างด้วย AI",
    "Copyright": "ไม่ใช้ Prompt ที่ละเมิดลิขสิทธิ์ artist name",
    "Data Privacy": "ไม่ใส่ PII ใน Prompt เช่น ชื่อจริง ใบหน้า",
    "Content Safety": "กรอง Content ที่ไม่เหมาะสม อัตโนมัติ",
    "Audit Trail": "เก็บ Log ทุกการใช้งาน 2 ปี ขั้นต่ำ",
    "Access Control": "RBAC จำกัดสิทธิ์ตาม Role",
    "C2PA Metadata": "ฝัง Provenance ข้อมูลในภาพที่สร้าง",
}

print("Compliance Requirements:")
for req, desc in compliance.items():
    print(f"  [{req}]: {desc}")

# Implementation Checklist
checklist = [
    "ตั้ง Proxy API หน้า AI Service ทุกตัว",
    "Log ทุก Request/Response เข้า Elasticsearch",
    "สร้าง Content Filter ด้วย Keyword + ML Model",
    "ตั้ง Budget Limit per User/Team",
    "สร้าง Grafana Dashboard สำหรับ Usage + Cost",
    "ตั้ง Alert เมื่อ Policy Violation",
    "กำหนด Retention Policy ตาม Compliance",
    "ฝัง C2PA Metadata ในภาพที่สร้าง",
]

print(f"\n\nImplementation Checklist:")
for i, item in enumerate(checklist, 1):
    print(f"  {i}. {item}")

เคล็ดลับ

  • Proxy: ตั้ง Proxy API หน้า AI Service ทุกตัว Log ได้ครบ
  • Filter: กรอง Prompt อัตโนมัติ ก่อนส่งไป AI
  • Budget: ตั้ง Budget Limit ป้องกันค่าใช้จ่ายบานปลาย
  • C2PA: ฝัง Provenance Metadata ในภาพทุกรูป
  • Review: ตรวจสอบ Audit Log เป็นประจำ ทุกสัปดาห์

Midjourney Audit Trail คืออะไร

บันทึกทุกการใช้ AI Image Prompt ผู้ใช้ เวลา ผลลัพธ์ Parameters Compliance Governance Cost Usage Pattern Legal