File size: 4,761 Bytes
d730633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30b56ac
d730633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Dict, List, Any, Optional
import os
from groq import Groq

app = FastAPI(title="Architecture Evaluator LLM")

# Enable CORS for all origins (adjust in production)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Initialize Groq client - FREE and FAST!
client = Groq(
    api_key=os.getenv("GROQ_API_KEY", "your-groq-api-key-here")
)

class EvaluationRequest(BaseModel):
    question: str
    architecture: Dict[str, Any]

class HeuristicScores(BaseModel):
    DURABILITY: float
    AVAILABILITY: float
    ENERGY_EFFICIENCY: float
    CONSISTENCY: float
    MAINTAINABILITY: float
    LATENCY: float
    COST: float
    SECURITY: float
    THROUGHPUT: float
    SCALABILITY: float

class EvaluationResponse(BaseModel):
    heuristic_scores: HeuristicScores
    suggestion: str

def create_evaluation_prompt(question: str, architecture: Dict[str, Any]) -> str:
    """Create a detailed prompt for the LLM to evaluate the architecture."""
    
    prompt = f"""You are an expert system architect. Analyze the following system design architecture and provide an evaluation.

**Question/Context:**
{question}

**Architecture JSON:**
{architecture}

**Your Task:**
1. Analyze the overall architecture based on the components and their connections (links)
2. Calculate aggregate heuristic scores (0-10 scale) for the entire architecture considering:
   - Individual component scores
   - How components interact through links
   - Overall system design patterns
   - Redundancy and fault tolerance
   - Data flow efficiency

3. Provide constructive suggestions on:
   - What's good about this architecture
   - What could be improved
   - Specific recommendations
   - Alternative approaches if applicable

**Important:** You must respond with ONLY a valid JSON object in this exact format:
{{
  "heuristic_scores": {{
    "DURABILITY": <float between 0-10>,
    "AVAILABILITY": <float between 0-10>,
    "ENERGY_EFFICIENCY": <float between 0-10>,
    "CONSISTENCY": <float between 0-10>,
    "MAINTAINABILITY": <float between 0-10>,
    "LATENCY": <float between 0-10>,
    "COST": <float between 0-10>,
    "SECURITY": <float between 0-10>,
    "THROUGHPUT": <float between 0-10>,
    "SCALABILITY": <float between 0-10>
  }},
  "suggestion": "<your detailed analysis and suggestions as a string which should be in bulleted point>"
}}

Do not include any text outside the JSON object. Ensure all scores are numeric values between 0 and 10."""
    
    return prompt

@app.get("/")
async def root():
    return {
        "message": "Architecture Evaluator LLM API",
        "status": "online",
        "endpoints": {
            "/evaluate": "POST - Evaluate a system architecture",
            "/health": "GET - Health check"
        }
    }

@app.get("/health")
async def health():
    return {"status": "healthy"}

@app.post("/evaluate", response_model=EvaluationResponse)
async def evaluate_architecture(request: EvaluationRequest):
    """
    Evaluate a system design architecture using LLM.
    
    Args:
        request: Contains the question and architecture JSON
        
    Returns:
        EvaluationResponse with heuristic scores and suggestions
    """
    try:
        # Create the prompt
        prompt = create_evaluation_prompt(request.question, request.architecture)
        
        # Call Groq LLM - Using best reasoning model (deepseek-r1-distill-llama-70b)
        # Alternative models: llama-3.3-70b-versatile, llama-3.1-70b-versatile
        response = client.chat.completions.create(
            model=os.getenv("MODEL_NAME", "llama-3.3-70b-versatile"),
            messages=[
                {
                    "role": "system",
                    "content": "You are an expert system architect who provides detailed analysis and returns responses"
                },
                {
                    "role": "user",
                    "content": prompt
                }
            ],
            temperature=0.7,
            response_format={"type": "json_object"}  # Enforce JSON response
        )
        
        # Parse the response
        import json
        result = json.loads(response.choices[0].message.content)
        
        # Validate and return
        return EvaluationResponse(**result)
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error evaluating architecture: {str(e)}")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)  # Port 7860 is standard for HuggingFace Spaces