-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_processor.py
More file actions
123 lines (94 loc) · 3.12 KB
/
ai_processor.py
File metadata and controls
123 lines (94 loc) · 3.12 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
import os
import json
from typing import Dict, Any
# Load environment variables from .env if present
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
# Choose provider (default = groq)
PROVIDER = os.getenv("AI_PROVIDER", "groq").lower()
def initialize_client():
"""Initialize the AI client depending on provider."""
if PROVIDER == "groq":
from groq import Groq
key = os.getenv("GROQ_API_KEY")
if not key:
raise RuntimeError("GROQ_API_KEY environment variable is not set")
return Groq(api_key=key)
elif PROVIDER == "openai":
import openai
key = os.getenv("OPENAI_API_KEY")
if not key:
raise RuntimeError("OPENAI_API_KEY environment variable is not set")
openai.api_key = key
return openai
else:
raise RuntimeError(f"Unsupported AI_PROVIDER: {PROVIDER}")
def generate_ddr_report(extracted: Dict[str, Any]) -> Dict[str, str]:
"""Generate DDR report using an LLM."""
inspection_text = extracted.get("inspection_text", "")
thermal_text = extracted.get("thermal_text", "")
images = extracted.get("images", [])
prompt = f"""
You are an expert assistant that composes a Detailed Diagnostic Report (DDR) based on two input documents: an Inspection Report and a Thermal Report.
Inspection Observations:
{inspection_text}
Thermal Observations:
{thermal_text}
Images:
{', '.join(images) if images else 'No images available'}
Create a structured report with the following sections:
1. Property Issue Summary
2. Area-wise Observations
3. Probable Root Cause
4. Severity Assessment (with reasoning)
5. Recommended Actions
6. Additional Notes
7. Missing or Unclear Information
Rules:
- Do not invent information.
- If information is missing, write "Not Available".
- If information conflicts, mention the conflict.
- Use clear, client-friendly language.
Return STRICT JSON with these keys:
property_issue_summary
area_wise_observations
root_cause
severity
recommended_actions
additional_notes
missing_information
"""
# GROQ
if PROVIDER == "groq":
client = initialize_client()
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": "You generate structured diagnostic reports."},
{"role": "user", "content": prompt},
],
temperature=0.2,
max_tokens=1000,
)
text = response.choices[0].message.content.strip()
# OPENAI
elif PROVIDER == "openai":
openai = initialize_client()
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You generate structured diagnostic reports."},
{"role": "user", "content": prompt},
],
temperature=0.2,
max_tokens=1000,
)
text = response.choices[0].message.content.strip()
# Parse JSON safely
try:
return json.loads(text)
except Exception:
return {"raw": text}