File size: 9,626 Bytes
76fd9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MAGI System - Multi-Agent General Intelligence v2.0
Modern CrewAI Implementation (No LangChain Required)

Based on Neon Genesis Evangelion's MAGI supercomputer system.
Three agents provide different perspectives on any question:
- Melchior: Scientific/logical perspective
- Balthasar: Ethical/emotional perspective
- Casper: Practical/social perspective
"""

import os
from typing import Dict, Any
from pathlib import Path
from dotenv import load_dotenv

# Modern CrewAI imports - No LangChain needed!
from crewai import Agent, Task, Crew, LLM
from crewai_tools import SerperDevTool

# Load environment variables from config/.env
config_path = Path(__file__).parent.parent / "config" / ".env"
load_dotenv(config_path)


def get_model(provider: str = "groq", temperature: float = 0.5) -> LLM:
    """
    Get LLM instance using modern CrewAI API.
    
    CrewAI now uses LiteLLM internally, supporting multiple providers
    with a unified interface. Model format: "provider/model-name"
    
    Args:
        provider: LLM provider ("groq" or "openai")
        temperature: Sampling temperature (0.0-1.0)
        
    Returns:
        LLM instance configured for the MAGI system
        
    Environment Variables Required:
        - GROQ_API_KEY: For Groq models
        - OPENAI_API_KEY: For OpenAI models
    """
    if provider == "groq":
        # Groq models - fast and cost-effective
        # llama-3.1-8b-instant: Fast, no rate limits (recommended for free tier)
        # llama-3.3-70b-versatile: More powerful but has rate limits
        # Other options: llama-3.1-70b-versatile, gemma2-9b-it
        return LLM(
            model="groq/llama-3.1-8b-instant",
            temperature=temperature
        )
    elif provider == "openai":
        # OpenAI models - high quality
        return LLM(
            model="openai/gpt-4o-mini",
            temperature=temperature
        )
    else:
        # Default to Groq with fastest model
        return LLM(
            model="groq/llama-3.1-8b-instant",
            temperature=temperature
        )


def create_magi_agents(llm: LLM, enable_search: bool = True) -> Dict[str, Agent]:
    """
    Create the three MAGI system agents with distinct personalities.
    
    Each agent represents a different aspect of Dr. Naoko Akagi's personality,
    providing diverse perspectives on any issue.
    
    Args:
        llm: The language model to use for all agents
        enable_search: Whether to enable internet search capability
    
    Returns:
        Dictionary with three agents: melchior, balthasar, casper
    """
    # Initialize search tool if enabled
    tools = [SerperDevTool()] if enable_search else []
    
    melchior = Agent(
        role="Melchior - Scientific Analyst",
        goal="Provide rigorous logical analysis based on data, facts, and scientific methodology",
        backstory="""You are Melchior, the scientist aspect of Dr. Naoko Akagi.
Your approach is purely analytical - you process information through the lens of logic,
empirical evidence, and scientific reasoning. You prioritize objective truth over 
subjective interpretation, always seeking verifiable data and rational conclusions.

You excel at:
- Data analysis and pattern recognition
- Logical reasoning and deduction
- Scientific methodology and hypothesis testing
- Objective risk assessment""",
        tools=tools,
        llm=llm,
        verbose=True,
        allow_delegation=False
    )
    
    balthasar = Agent(
        role="Balthasar - Ethical Counselor",
        goal="Evaluate emotional impact, ethical implications, and human welfare considerations",
        backstory="""You are Balthasar, the mother aspect of Dr. Naoko Akagi.
You analyze situations through emotional intelligence and ethical frameworks,
always considering the human element. Your decisions are guided by empathy,
moral principles, and concern for wellbeing and dignity of all affected parties.

You excel at:
- Emotional intelligence and empathy
- Ethical analysis and moral reasoning
- Human impact assessment
- Long-term welfare considerations""",
        tools=tools,
        llm=llm,
        verbose=True,
        allow_delegation=False
    )
    
    casper = Agent(
        role="Casper - Pragmatic Advisor",
        goal="Assess practical feasibility, social dynamics, and real-world implementation",
        backstory="""You are Casper, the woman aspect of Dr. Naoko Akagi.
You bridge the gap between theory and practice, considering social contexts,
cultural factors, and realistic implementation. You balance ideals with pragmatism,
always asking "will this actually work in the real world?"

You excel at:
- Practical problem-solving
- Social dynamics analysis
- Resource and feasibility assessment
- Implementation planning""",
        tools=tools,
        llm=llm,
        verbose=True,
        allow_delegation=False
    )
    
    return {
        "melchior": melchior,
        "balthasar": balthasar,
        "casper": casper
    }


def analyze_question(
    question: str, 
    provider: str = "groq",
    ollama_model: str = None,
    enable_search: bool = True,
    temperature: float = 0.5
    ) -> Dict[str, Any]:
    """
    Analyze a question using the MAGI three-perspective system.
    
    The question is evaluated independently by three agents representing different
    perspectives, mimicking the MAGI supercomputer from Evangelion.
    
    Args:
        question: The question or problem to analyze
        provider: LLM provider ("groq" or "openai")
        enable_search: Whether agents can search the internet
        temperature: LLM temperature (0.0-1.0, higher = more creative)
        
    Returns:
        Dictionary containing analyses from all three agents
        
    Example:
        >>> result = analyze_question("Should we invest in AI safety?")
        >>> print(result['result'])
    """
    print(f"\n{'='*80}")
    print("MAGI SYSTEM INITIALIZING")
    print(f"{'='*80}")
    print(f"Question: {question}")
    print(f"Provider: {provider}")
    print(f"Search enabled: {enable_search}")
    print(f"{'='*80}\n")
    
    # Initialize LLM
    if provider == "ollama" and ollama_model:
        llm = LLM(model=f"ollama/{ollama_model}", temperature=temperature)
    else:
        llm = get_model(provider, temperature)
    
    # Create the three MAGI agents
    agents = create_magi_agents(llm, enable_search)
    
    # Create individual tasks for each agent
    tasks = [
        Task(
            description=f"""Analyze this question from your scientific perspective:

Question: {question}

Provide analysis focusing on:
- Relevant data and facts
- Logical reasoning and evidence
- Scientific principles
- Quantifiable metrics

Be thorough, objective, and grounded in verifiable information.""",
            expected_output="Scientific analysis with data-driven insights and logical conclusions",
            agent=agents["melchior"]
        ),
        
        Task(
            description=f"""Analyze this question from your ethical perspective:

Question: {question}

Provide analysis focusing on:
- Ethical implications and moral considerations
- Impact on human welfare and dignity
- Benefits and harms to stakeholders
- Alignment with moral principles

Be empathetic, principled, and human-centered.""",
            expected_output="Ethical analysis considering human impact and moral implications",
            agent=agents["balthasar"]
        ),
        
        Task(
            description=f"""Analyze this question from your practical perspective:

Question: {question}

Provide analysis focusing on:
- Real-world feasibility and implementation
- Social and cultural considerations
- Resource requirements and constraints
- Actionable recommendations

Be pragmatic, realistic, and implementation-focused.""",
            expected_output="Practical analysis with feasibility assessment and actionable insights",
            agent=agents["casper"]
        )
    ]
    
    # Create crew with all agents and tasks
    crew = Crew(
        agents=list(agents.values()),
        tasks=tasks,
        verbose=True,
        process="sequential"  # Each agent analyzes independently
    )
    
    # Execute MAGI analysis
    print("\n" + "="*80)
    print("EXECUTING MAGI ANALYSIS...")
    print("="*80 + "\n")
    
    result = crew.kickoff()
    
    # Format results
    output = {
        "question": question,
        "provider": provider,
        "result": str(result),
        "status": "completed"
    }
    
    print("\n" + "="*80)
    print("MAGI ANALYSIS COMPLETE")
    print("="*80 + "\n")
    
    return output


def main():
    """
    Main entry point for testing the MAGI system.
    """
    print("\n" + "="*80)
    print("MAGI SYSTEM - MULTI-AGENT GENERAL INTELLIGENCE")
    print("Based on Neon Genesis Evangelion")
    print("="*80 + "\n")
    
    # Example question
    test_question = "Should we invest heavily in renewable energy infrastructure?"
    
    # Run analysis
    result = analyze_question(
        question=test_question,
        provider="groq",  # Change to "openai" if you have OpenAI API key
        enable_search=True,
        temperature=0.5
    )
    
    # Display results
    print("\n" + "="*80)
    print("FINAL RESULTS")
    print("="*80)
    print(f"\nQuestion: {result['question']}")
    print(f"\nProvider: {result['provider']}")
    print(f"\nStatus: {result['status']}")
    print(f"\n{'-'*80}")
    print("MAGI System Analysis:")
    print(f"{'-'*80}")
    print(f"\n{result['result']}")
    print("\n" + "="*80 + "\n")


if __name__ == "__main__":
    main()