AI Agent Ethics: Developing AI Responsibly

๐Ÿ“˜ Tutorials 2026-07-19 2 min read

AI Agent Ethics: Developing AI Responsibly

💡 What You Will Learn

AI Agent Ethics: Developing AI Responsibly

class EthicalAgent:
    """AI Agent with Ethical Constraints"""

    def __init__(self):
        self.disclosure_given = False

    def generate_response(self, user_input: str) -> str:
        if not self.disclosure_given:
            self.disclosure_given = True
            disclosure = "AI[]\n\n"
            response = llm.invoke(user_input)
            return disclosure + response
        return llm.invoke(user_input)
HIGH_RISK_ACTIONS = ["refund", "cancel_order", "modify_price", "delete_data", "send_coupon"]

def human_in_the_loop(action_type: str, params: dict) -> dict:
    if action_type not in HIGH_RISK_ACTIONS:
        return execute_action(action_type, params)

    approval_request = f"""
Requires human confirmation
{action_type}
{json.dumps(params, ensure_ascii=False)}
AI Agent
5
"""
    send_to_approval_queue(approval_request)
    result = wait_for_approval(action_type, params, timeout=300)
    return result
import hashlib
from datetime import datetime

class AuditLogger:
    """Logging"""
    def __init__(self, log_file="audit.log"):
        self.log_file = log_file

    def log(self, event_type: str, details: dict):
        entry = {
            "timestamp": datetime.utcnow().isoformat(),
            "event_type": event_type,
            "details": details,
            "hash": ""
        }
        last_hash = self._get_last_hash()
        entry["prev_hash"] = last_hash
        entry["hash"] = hashlib.sha256(json.dumps(entry, sort_keys=True).encode()).hexdigest()
        self._append(entry)

    def _get_last_hash(self) -> str:
        try:
            with open(self.log_file, 'r') as f:
                for line in f:
                    pass
                return json.loads(line)["hash"]
        except (FileNotFoundError, json.JSONDecodeError):
            return "0" * 64

    def audit(self, start_date: str, end_date: str) -> list:
        results = []
        with open(self.log_file, 'r') as f:
            for line in f:
                entry = json.loads(line)
                if start_date <= entry['timestamp'][:10] <= end_date:
                    results.append(entry)
        return results

    def _append(self, entry: dict):
        with open(self.log_file, 'a') as f:
            f.write(json.dumps(entry, ensure_ascii=False) + '\n')
SENSITIVE_ATTRIBUTES = ["", "", "", "", ""]

def bias_check(prompt: str, response: str) -> dict:
    check_prompt = f"""AI{SENSITIVE_ATTRIBUTES}

{prompt}
AI{response}

JSON
{{
  "has_bias": true/false,
  "bias_type": "/////",
  "confidence": 0-1,
  "suggestion": ""
}}"""
    result = llm.invoke(check_prompt)
    return json.loads(result)
Related Articles
2026-07-19
AI Agent Persona Design: Giving Your Agent a Consistent Personality
2026-08-12
AI RAG Framework 2026: 8 Open Source Stacks for Retrieval-Augmented Generation
2026-08-02
FastAPI for LLM Apps 2026: Build a Production AI Backend in One Afternoon (101k Stars)

Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only โ€” no paid placements.

๐Ÿ’ฌ Comments (0)

No comments yet. Be the first!

Login to comment