-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathfraud-detection.py
More file actions
136 lines (119 loc) · 3.88 KB
/
fraud-detection.py
File metadata and controls
136 lines (119 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from praisonaiagents import Agent, Task, PraisonAIAgents
import time
from typing import Dict, List
import asyncio
def analyze_transaction():
"""Simulates transaction analysis"""
transactions = [
{"type": "credit_card", "amount": 5000, "location": "foreign", "risk": "high"},
{"type": "wire", "amount": 2000, "location": "domestic", "risk": "medium"},
{"type": "online", "amount": 500, "location": "domestic", "risk": "low"}
]
return transactions[int(time.time()) % 3]
def check_patterns(transaction: Dict):
"""Simulates pattern checking"""
patterns = {
"high": ["unusual_location", "large_amount"],
"medium": ["frequency_anomaly"],
"low": ["within_normal_limits"]
}
return patterns.get(transaction["risk"], ["unknown"])
def verify_identity():
"""Simulates identity verification"""
results = ["verified", "suspicious", "failed"]
return results[int(time.time()) % 3]
def generate_alert(verification: str, patterns: List[str]):
"""Simulates alert generation"""
if verification == "failed" or "unusual_location" in patterns:
return "high_priority_alert"
elif verification == "suspicious":
return "medium_priority_alert"
return "low_priority_alert"
# Create specialized agents
transaction_analyzer = Agent(
name="Transaction Analyzer",
role="Transaction Analysis",
goal="Analyze transactions for suspicious patterns",
instructions="Monitor and analyze financial transactions",
tools=[analyze_transaction]
)
pattern_checker = Agent(
name="Pattern Checker",
role="Pattern Detection",
goal="Identify suspicious patterns",
instructions="Check for known fraud patterns",
tools=[check_patterns]
)
identity_verifier = Agent(
name="Identity Verifier",
role="Identity Verification",
goal="Verify transaction identities",
instructions="Perform identity verification checks",
tools=[verify_identity]
)
alert_generator = Agent(
name="Alert Generator",
role="Alert Management",
goal="Generate appropriate alerts",
instructions="Create and prioritize alerts",
tools=[generate_alert]
)
# Create workflow tasks
analysis_task = Task(
name="analyze_transaction",
description="Analyze transaction details",
expected_output="Transaction analysis",
agent=transaction_analyzer,
is_start=True,
task_type="decision",
condition={
"high": ["check_patterns", "verify_identity"],
"medium": ["check_patterns"],
"low": ["check_patterns"]
}
)
pattern_task = Task(
name="check_patterns",
description="Check for suspicious patterns",
expected_output="Identified patterns",
agent=pattern_checker,
next_tasks=["generate_alert"],
async_execution=True
)
verification_task = Task(
name="verify_identity",
description="Verify transaction identity",
expected_output="Verification result",
agent=identity_verifier,
next_tasks=["generate_alert"],
async_execution=True
)
alert_task = Task(
name="generate_alert",
description="Generate fraud alert",
expected_output="Alert priority",
agent=alert_generator,
context=[pattern_task, verification_task]
)
# Create workflow
workflow = PraisonAIAgents(
agents=[transaction_analyzer, pattern_checker, identity_verifier, alert_generator],
tasks=[analysis_task, pattern_task, verification_task, alert_task],
process="workflow",
verbose=True
)
async def main():
print("\nStarting Fraud Detection Workflow...")
print("=" * 50)
# Run workflow
results = await workflow.astart()
# Print results
print("\nFraud Detection Results:")
print("=" * 50)
for task_id, result in results["task_results"].items():
if result:
print(f"\nTask: {task_id}")
print(f"Result: {result.raw}")
print("-" * 50)
if __name__ == "__main__":
asyncio.run(main())