-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.py
More file actions
1198 lines (979 loc) · 42.4 KB
/
Copy pathagents.py
File metadata and controls
1198 lines (979 loc) · 42.4 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from abc import ABC, abstractmethod
from typing import Dict, Any, List
import google.generativeai as genai
import requests
import json
from datetime import datetime
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
# Configure OpenAI API (PipeShift)
client = OpenAI(
api_key=os.getenv('OPENAI_API_KEY'),
base_url=os.getenv('OPENAI_BASE_URL', 'https://api.pipeshift.com/api/v0/')
)
AI_MODEL = os.getenv('OPENAI_MODEL', 'neysa-qwen3-vl-30b-a3b')
def get_ai_response(prompt: str, system_message: str = "You are a helpful AI assistant.") -> str:
"""Helper function to get AI response using OpenAI"""
try:
response = client.chat.completions.create(
model=AI_MODEL,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": prompt}
],
max_tokens=2000,
temperature=0.7
)
return response.choices[0].message.content
except Exception as e:
return f"Error getting AI response: {str(e)}"
class BaseAgent(ABC):
"""Base class for all agent types"""
def __init__(self, agent_id: str, name: str):
self.agent_id = agent_id
self.name = name
self.created_at = datetime.utcnow()
self.task_count = 0
self.success_rate = 0.0
@abstractmethod
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process a task and return results"""
pass
@abstractmethod
def get_capabilities(self) -> List[str]:
"""Return list of agent capabilities"""
pass
def update_performance(self, success: bool):
"""Update agent performance metrics"""
self.task_count += 1
if self.task_count == 1:
self.success_rate = 1.0 if success else 0.0
else:
self.success_rate = (self.success_rate * (self.task_count - 1) + (1.0 if success else 0.0)) / self.task_count
class FinancialAgent(BaseAgent):
"""Financial coaching and analysis agent"""
def __init__(self):
super().__init__("financial_agent", "Financial Coach")
self.specializations = [
"spending_analysis", "income_variability", "budget_planning",
"investment_guidance", "debt_management", "emergency_fund_optimization"
]
def get_capabilities(self) -> List[str]:
return [
"Analyze spending patterns and identify trends",
"Assess income stability and variability",
"Create personalized budgets",
"Provide investment recommendations",
"Develop debt reduction strategies",
"Optimize emergency fund planning",
"Generate financial health reports",
"Predict financial trends"
]
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process financial analysis tasks"""
try:
task_type = task_data.get('task_type', 'general')
if task_type == 'spending_analysis':
return await self.analyze_spending_patterns(task_data)
elif task_type == 'income_analysis':
return await self.analyze_income_patterns(task_data)
elif task_type == 'budget_planning':
return await self.create_budget_plan(task_data)
elif task_type == 'investment_guidance':
return await self.provide_investment_advice(task_data)
elif task_type == 'debt_management':
return await self.analyze_debt_strategy(task_data)
elif task_type == 'comprehensive_analysis':
return await self.comprehensive_financial_analysis(task_data)
else:
return await self.general_financial_advice(task_data)
except Exception as e:
self.update_performance(False)
return {
'success': False,
'error': str(e),
'agent_type': 'financial',
'task_type': task_type
}
async def analyze_spending_patterns(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze spending patterns and provide insights"""
transactions = task_data.get('transactions', [])
if not transactions:
return {
'success': True,
'message': 'No transaction data available',
'recommendations': ['Start adding transactions to see spending patterns']
}
# Analyze spending by category
expense_transactions = [t for t in transactions if t.get('type') == 'expense']
category_spending = {}
for transaction in expense_transactions:
category = transaction.get('category', 'uncategorized')
amount = transaction.get('amount', 0)
category_spending[category] = category_spending.get(category, 0) + abs(amount)
# Generate insights
total_spending = sum(category_spending.values())
insights = []
# Top spending category
if category_spending:
top_category = max(category_spending, key=category_spending.get)
top_percentage = (category_spending[top_category] / total_spending) * 100
if top_percentage > 40:
insights.append(f"{top_percentage:.1f}% of spending is in {top_category}")
# Generate AI-powered advice
context = f"Spending by category: {category_spending}"
advice = await self.generate_ai_advice(context, "spending_patterns")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'spending_analysis',
'category_spending': category_spending,
'total_spending': total_spending,
'insights': insights,
'recommendations': advice
}
async def analyze_income_patterns(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze income patterns for stability and trends"""
transactions = task_data.get('transactions', [])
income_transactions = [t for t in transactions if t.get('type') == 'income']
if not income_transactions:
return {
'success': True,
'message': 'No income data available',
'recommendations': ['Add income transactions to see income patterns']
}
# Group income by month
monthly_income = {}
for transaction in income_transactions:
date_str = transaction.get('date', '')
if date_str:
try:
date = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
month_key = date.strftime('%Y-%m')
amount = transaction.get('amount', 0)
monthly_income[month_key] = monthly_income.get(month_key, 0) + amount
except:
continue
# Calculate stability metrics
if len(monthly_income) >= 2:
income_values = list(monthly_income.values())
mean_income = sum(income_values) / len(income_values)
variance = sum((x - mean_income) ** 2 for x in income_values) / len(income_values)
std_dev = variance ** 0.5
volatility = (std_dev / mean_income) if mean_income > 0 else 0
else:
volatility = 0
# Generate AI advice
context = f"Monthly income: {monthly_income}, Volatility: {volatility:.2f}"
advice = await self.generate_ai_advice(context, "income_stability")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'income_analysis',
'monthly_income': monthly_income,
'volatility': volatility,
'stability_score': max(0, 1 - volatility),
'recommendations': advice
}
async def create_budget_plan(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Create personalized budget plan"""
transactions = task_data.get('transactions', [])
monthly_income = task_data.get('monthly_income', 0)
# Calculate average monthly spending by category
expense_transactions = [t for t in transactions if t.get('type') == 'expense']
category_spending = {}
for transaction in expense_transactions:
category = transaction.get('category', 'uncategorized')
amount = transaction.get('amount', 0)
category_spending[category] = category_spending.get(category, 0) + abs(amount)
# Create budget recommendations
total_expenses = sum(category_spending.values())
budget_recommendations = {}
# Standard budgeting rules (50/30/20 rule)
if monthly_income > 0:
budget_recommendations = {
'needs': monthly_income * 0.5, # 50% for needs
'wants': monthly_income * 0.3, # 30% for wants
'savings': monthly_income * 0.2 # 20% for savings
}
# Category-specific recommendations
category_budgets = {}
for category, amount in category_spending.items():
# Recommend reducing overspending categories by 10-20%
if amount > (monthly_income * 0.1): # More than 10% of income
category_budgets[category] = amount * 0.85 # Reduce by 15%
else:
category_budgets[category] = amount
# Generate AI advice
context = f"Current spending: {category_spending}, Monthly income: {monthly_income}"
advice = await self.generate_ai_advice(context, "budget_planning")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'budget_planning',
'budget_recommendations': budget_recommendations,
'category_budgets': category_budgets,
'total_expenses': total_expenses,
'recommendations': advice
}
async def provide_investment_advice(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Provide investment guidance based on financial profile"""
risk_tolerance = task_data.get('risk_tolerance', 'moderate')
monthly_income = task_data.get('monthly_income', 0)
investment_goals = task_data.get('investment_goals', [])
# Risk-based investment recommendations
investment_strategies = {
'conservative': {
'allocation': {'bonds': 60, 'stocks': 30, 'cash': 10},
'recommended_funds': ['Index funds', 'Government bonds', 'High-yield savings'],
'expected_return': '4-6% annually'
},
'moderate': {
'allocation': {'bonds': 40, 'stocks': 50, 'cash': 10},
'recommended_funds': ['Balanced index funds', 'ETFs', 'Some growth stocks'],
'expected_return': '6-8% annually'
},
'aggressive': {
'allocation': {'bonds': 20, 'stocks': 70, 'cash': 10},
'recommended_funds': ['Growth stocks', 'Tech ETFs', 'Emerging markets'],
'expected_return': '8-12% annually'
}
}
strategy = investment_strategies.get(risk_tolerance, investment_strategies['moderate'])
# Calculate investment amount (20% of income recommendation)
investment_amount = monthly_income * 0.2
# Generate AI advice
context = f"Risk tolerance: {risk_tolerance}, Monthly income: {monthly_income}, Goals: {investment_goals}"
advice = await self.generate_ai_advice(context, "investment_guidance")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'investment_guidance',
'strategy': strategy,
'recommended_monthly_investment': investment_amount,
'recommendations': advice
}
async def analyze_debt_strategy(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze and recommend debt management strategies"""
debts = task_data.get('debts', [])
monthly_income = task_data.get('monthly_income', 0)
if not debts:
return {
'success': True,
'message': 'No debt data available',
'recommendations': ['Continue debt-free status']
}
total_debt = sum(debt.get('amount', 0) for debt in debts)
total_monthly_payments = sum(debt.get('monthly_payment', 0) for debt in debts)
# Calculate debt-to-income ratio
debt_to_income = (total_monthly_payments / monthly_income) if monthly_income > 0 else 0
# Prioritize debts (highest interest rate first)
prioritized_debts = sorted(debts, key=lambda x: x.get('interest_rate', 0), reverse=True)
# Generate payoff strategies
strategies = {
'avalanche': {
'description': 'Pay highest interest debt first',
'total_interest_saved': 'Maximum',
'time_to_debt_free': 'Fastest for high-interest debt'
},
'snowball': {
'description': 'Pay smallest debt first',
'total_interest_saved': 'More than minimum payments',
'time_to_debt_free': 'Psychological wins'
}
}
# Generate AI advice
context = f"Total debt: {total_debt}, Debt-to-income: {debt_to_income:.2f}, Debts: {debts}"
advice = await self.generate_ai_advice(context, "debt_management")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'debt_management',
'total_debt': total_debt,
'debt_to_income_ratio': debt_to_income,
'prioritized_debts': prioritized_debts,
'strategies': strategies,
'recommendations': advice
}
async def comprehensive_financial_analysis(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Perform comprehensive financial analysis"""
transactions = task_data.get('transactions', [])
profile = task_data.get('profile', {})
# Run multiple analyses
spending_result = await self.analyze_spending_patterns(task_data)
income_result = await self.analyze_income_patterns(task_data)
budget_result = await self.create_budget_plan(task_data)
# Calculate overall financial health
income_transactions = [t for t in transactions if t.get('type') == 'income']
expense_transactions = [t for t in transactions if t.get('type') == 'expense']
total_income = sum(t.get('amount', 0) for t in income_transactions)
total_expenses = sum(t.get('amount', 0) for t in expense_transactions)
savings_rate = (total_income - total_expenses) / total_income if total_income > 0 else 0
# Calculate health score
health_score = self.calculate_health_score(savings_rate, income_result.get('volatility', 0), profile)
# Generate comprehensive AI advice
context = f"""
Financial Profile:
- Total Income: ${total_income:.2f}
- Total Expenses: ${total_expenses:.2f}
- Savings Rate: {savings_rate:.2%}
- Income Volatility: {income_result.get('volatility', 0):.2f}
- Employment Type: {profile.get('employment_type', 'unknown')}
"""
comprehensive_advice = await self.generate_ai_advice(context, "comprehensive_analysis")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'comprehensive_analysis',
'health_score': health_score,
'savings_rate': savings_rate,
'spending_analysis': spending_result,
'income_analysis': income_result,
'budget_plan': budget_result,
'comprehensive_recommendations': comprehensive_advice
}
async def general_financial_advice(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Provide general financial advice"""
context = json.dumps(task_data, indent=2)
advice = await self.generate_ai_advice(context, "general_advice")
self.update_performance(True)
return {
'success': True,
'agent_type': 'financial',
'task_type': 'general_advice',
'recommendations': advice
}
async def generate_ai_advice(self, context: str, advice_type: str) -> List[str]:
"""Generate AI-powered financial advice using Gemini"""
try:
prompt = f"""
As a financial coach, provide specific, actionable advice based on this context:
Context: {context}
Advice Type: {advice_type}
Provide 3-5 specific recommendations. Each recommendation should be:
- Actionable and specific
- Tailored to the financial situation
- Include expected outcomes
- Be realistic and practical
Format as a JSON array of strings.
"""
response_text = get_ai_response(prompt, "You are an expert financial coach.")
# Parse the response
try:
advice_list = json.loads(response_text)
if isinstance(advice_list, list):
return advice_list
except:
# Fallback: extract advice from text
advice_text = response_text
recommendations = []
lines = advice_text.split('\n')
for line in lines:
if line.strip() and (line.startswith('-') or line.startswith('•') or line.startswith('*')):
recommendations.append(line.strip('-•* ').strip())
return recommendations[:5] # Limit to 5 recommendations
return ["Focus on increasing your savings rate", "Review and reduce unnecessary expenses", "Build an emergency fund"]
except Exception as e:
# Fallback recommendations
return [
"Track your spending to identify areas for improvement",
"Aim to save at least 20% of your income",
"Build an emergency fund covering 3-6 months of expenses"
]
def calculate_health_score(self, savings_rate: float, income_volatility: float, profile: Dict) -> int:
"""Calculate financial health score (0-100)"""
score = 50 # Base score
# Savings rate impact
if savings_rate >= 0.2:
score += 30
elif savings_rate >= 0.1:
score += 20
elif savings_rate >= 0.05:
score += 10
# Income volatility impact
if income_volatility <= 0.1:
score += 15
elif income_volatility <= 0.2:
score += 10
elif income_volatility <= 0.3:
score += 5
# Employment type adjustment
if profile.get('employment_type') in ['gig', 'informal']:
score -= 10
return max(0, min(100, score))
return [
"financial_analysis",
"budget_planning",
"investment_advice",
"spending_pattern_analysis",
"income_optimization",
"debt_management",
"savings_recommendations"
]
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process financial-related tasks"""
task_type = task_data.get('task_type', 'general_advice')
try:
if task_type == 'spending_analysis':
return await self.analyze_spending_patterns(task_data)
elif task_type == 'budget_planning':
return await self.create_budget_plan(task_data)
elif task_type == 'investment_advice':
return await self.provide_investment_advice(task_data)
elif task_type == 'debt_management':
return await self.analyze_debt_situation(task_data)
else:
return await self.general_financial_advice(task_data)
except Exception as e:
return {
'success': False,
'error': str(e),
'agent_id': self.agent_id
}
async def analyze_spending_patterns(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze user spending patterns and provide insights"""
transactions = data.get('transactions', [])
user_profile = data.get('user_profile', {})
prompt = f"""
As a financial analyst, analyze these spending patterns:
User Profile: {user_profile}
Recent Transactions: {transactions}
Provide:
1. Spending category breakdown
2. Unusual spending patterns
3. Cost-saving opportunities
4. Budget recommendations
"""
response_text = get_ai_response(prompt, "You are a financial analyst.")
return {
'success': True,
'analysis': response_text,
'agent_id': self.agent_id,
'task_type': 'spending_analysis'
}
async def create_budget_plan(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Create personalized budget plan"""
income = data.get('income', {})
expenses = data.get('expenses', {})
goals = data.get('financial_goals', '')
prompt = f"""
Create a comprehensive budget plan based on:
Monthly Income: {income}
Current Expenses: {expenses}
Financial Goals: {goals}
Provide:
1. Recommended budget allocations
2. Savings targets
3. Expense reduction strategies
4. Emergency fund recommendations
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'budget_plan': response_text,
'agent_id': self.agent_id,
'task_type': 'budget_planning'
}
async def provide_investment_advice(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Provide investment recommendations"""
risk_tolerance = data.get('risk_tolerance', 'medium')
investment_amount = data.get('amount', 0)
time_horizon = data.get('time_horizon', 'medium_term')
prompt = f"""
Provide investment advice for:
Risk Tolerance: {risk_tolerance}
Investment Amount: ${investment_amount}
Time Horizon: {time_horizon}
Consider:
1. Diversification strategies
2. Risk-appropriate investments
3. Long-term growth potential
4. Market conditions
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'investment_advice': response_text,
'agent_id': self.agent_id,
'task_type': 'investment_advice'
}
async def analyze_debt_situation(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze debt and provide repayment strategies"""
debts = data.get('debts', [])
income = data.get('income', {})
prompt = f"""
Analyze this debt situation and provide repayment strategies:
Debts: {debts}
Monthly Income: {income}
Provide:
1. Debt prioritization strategy
2. Repayment timeline
3. Consolidation options
4. Interest-saving strategies
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'debt_analysis': response_text,
'agent_id': self.agent_id,
'task_type': 'debt_management'
}
async def general_financial_advice(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Provide general financial advice"""
context = data.get('context', '')
user_profile = data.get('user_profile', {})
prompt = f"""
Provide financial advice for:
User Situation: {user_profile}
Specific Context: {context}
Give practical, actionable advice considering:
1. Current financial situation
2. Short-term and long-term goals
3. Risk tolerance
4. Market conditions
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'advice': response_text,
'agent_id': self.agent_id,
'task_type': 'general_advice'
}
class ResearchAgent(BaseAgent):
"""Research and information gathering agent"""
def __init__(self):
super().__init__("research_agent", "Research Assistant")
def get_capabilities(self) -> List[str]:
return [
"web_research",
"data_analysis",
"market_research",
"competitive_analysis",
"trend_analysis",
"report_generation",
"fact_checking"
]
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process research-related tasks"""
task_type = task_data.get('task_type', 'general_research')
try:
if task_type == 'market_research':
return await self.conduct_market_research(task_data)
elif task_type == 'competitive_analysis':
return await self.analyze_competition(task_data)
elif task_type == 'trend_analysis':
return await self.analyze_trends(task_data)
else:
return await self.general_research(task_data)
except Exception as e:
return {
'success': False,
'error': str(e),
'agent_id': self.agent_id
}
async def conduct_market_research(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Conduct market research on given topic"""
topic = data.get('topic', '')
focus_areas = data.get('focus_areas', [])
prompt = f"""
Conduct comprehensive market research on: {topic}
Focus Areas: {focus_areas}
Provide:
1. Market size and growth potential
2. Key players and competitors
3. Market trends and opportunities
4. Challenges and risks
5. Recommendations
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'research': response_text,
'agent_id': self.agent_id,
'task_type': 'market_research'
}
async def analyze_competition(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze competitive landscape"""
competitors = data.get('competitors', [])
industry = data.get('industry', '')
prompt = f"""
Analyze competitive landscape for:
Industry: {industry}
Competitors: {competitors}
Provide:
1. Competitive positioning
2. Strengths and weaknesses analysis
3. Market share analysis
4. Strategic recommendations
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'competitive_analysis': response_text,
'agent_id': self.agent_id,
'task_type': 'competitive_analysis'
}
async def analyze_trends(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze industry or market trends"""
domain = data.get('domain', '')
time_period = data.get('time_period', 'current')
prompt = f"""
Analyze trends in {domain} for {time_period} period:
Provide:
1. Current trends
2. Emerging patterns
3. Future predictions
4. Impact assessment
5. Opportunities and threats
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'trend_analysis': response_text,
'agent_id': self.agent_id,
'task_type': 'trend_analysis'
}
async def general_research(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Conduct general research on any topic"""
query = data.get('query', '')
depth = data.get('depth', 'comprehensive')
prompt = f"""
Research the following topic: {query}
Depth level: {depth}
Provide comprehensive information including:
1. Key facts and figures
2. Historical context
3. Current status
4. Future outlook
5. Sources and references
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'research': response_text,
'agent_id': self.agent_id,
'task_type': 'general_research'
}
class ProductivityAgent(BaseAgent):
"""Productivity and task management agent"""
def __init__(self):
super().__init__("productivity_agent", "Productivity Coach")
def get_capabilities(self) -> List[str]:
return [
"task_prioritization",
"time_management",
"workflow_optimization",
"habit_tracking",
"goal_setting",
"schedule_optimization",
"productivity_analysis"
]
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process productivity-related tasks"""
task_type = task_data.get('task_type', 'general_productivity')
try:
if task_type == 'task_prioritization':
return await self.prioritize_tasks(task_data)
elif task_type == 'schedule_optimization':
return await self.optimize_schedule(task_data)
elif task_type == 'workflow_analysis':
return await self.analyze_workflow(task_data)
else:
return await self.general_productivity_advice(task_data)
except Exception as e:
return {
'success': False,
'error': str(e),
'agent_id': self.agent_id
}
async def prioritize_tasks(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Prioritize tasks based on various criteria"""
tasks = data.get('tasks', [])
criteria = data.get('criteria', ['urgency', 'importance', 'effort'])
prompt = f"""
Prioritize these tasks based on {criteria}:
Tasks: {tasks}
Provide:
1. Prioritized task list
2. Rationale for prioritization
3. Recommended order of execution
4. Time estimates
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'prioritization': response_text,
'agent_id': self.agent_id,
'task_type': 'task_prioritization'
}
async def optimize_schedule(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Optimize user's schedule"""
current_schedule = data.get('schedule', {})
constraints = data.get('constraints', {})
goals = data.get('goals', '')
prompt = f"""
Optimize this schedule:
Current Schedule: {current_schedule}
Constraints: {constraints}
Goals: {goals}
Provide:
1. Optimized schedule
2. Time blocking recommendations
3. Break suggestions
4. Productivity improvements
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'optimized_schedule': response_text,
'agent_id': self.agent_id,
'task_type': 'schedule_optimization'
}
async def analyze_workflow(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze and improve workflow"""
workflow = data.get('workflow', {})
pain_points = data.get('pain_points', [])
prompt = f"""
Analyze and improve this workflow:
Current Workflow: {workflow}
Pain Points: {pain_points}
Provide:
1. Workflow analysis
2. Bottleneck identification
3. Improvement recommendations
4. Automation opportunities
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'workflow_analysis': response_text,
'agent_id': self.agent_id,
'task_type': 'workflow_analysis'
}
async def general_productivity_advice(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Provide general productivity advice"""
context = data.get('context', '')
challenges = data.get('challenges', [])
prompt = f"""
Provide productivity advice for:
Context: {context}
Challenges: {challenges}
Provide:
1. Specific strategies
2. Tool recommendations
3. Habit formation tips
4. Motivation techniques
"""
response_text = get_ai_response(prompt, "You are a helpful AI assistant.")
return {
'success': True,
'productivity_advice': response_text,
'agent_id': self.agent_id,
'task_type': 'general_productivity'
}
class LearningAgent(BaseAgent):
"""Learning and skill development agent"""
def __init__(self):
super().__init__("learning_agent", "Learning Coach")
def get_capabilities(self) -> List[str]:
return [
"skill_assessment",
"learning_path_creation",
"resource_recommendation",
"progress_tracking",
"knowledge_testing",
"study_planning",
"career_guidance"
]
async def process_task(self, task_data: Dict[str, Any]) -> Dict[str, Any]:
"""Process learning-related tasks"""
task_type = task_data.get('task_type', 'general_learning')
try:
if task_type == 'skill_assessment':
return await self.assess_skills(task_data)
elif task_type == 'learning_path':
return await self.create_learning_path(task_data)
elif task_type == 'resource_recommendation':
return await self.recommend_resources(task_data)
else:
return await self.general_learning_advice(task_data)
except Exception as e:
return {
'success': False,
'error': str(e),
'agent_id': self.agent_id
}
async def assess_skills(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""Assess current skills and identify gaps"""
current_skills = data.get('current_skills', [])
target_skills = data.get('target_skills', [])
career_goals = data.get('career_goals', '')
prompt = f"""
Assess skills and identify gaps: