File size: 8,785 Bytes
a91323c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import os
from typing import Any, Dict, List, Optional, TYPE_CHECKING

from .models import StrategicObjective

if TYPE_CHECKING:
    from openai.types.chat import ChatCompletionUserMessageParam


def _alignment_label(score: float) -> str:
    if score >= 0.75:
        return "Strong"
    if score >= 0.55:
        return "Medium"
    return "Weak"


class RAGEngine:
    """Retrieval-Augmented Generation (RAG) helper for improvement suggestions.

    Responsibilities:
    - Accept a strategic objective, current alignment score, and top-K retrieved action tasks
    - Build a structured prompt with clear SYSTEM / CONTEXT / INSTRUCTIONS sections
    - Optionally call an LLM (OpenAI API via env vars) and parse structured JSON
    - Fallback to deterministic, rule-based suggestions if LLM is unavailable
    """

    def __init__(self, model: Optional[str] = None) -> None:
        # Model name can be overridden via env var OPENAI_MODEL
        # Use a widely supported default; allow override via env or arg
        self.model = model or os.environ.get("OPENAI_MODEL") or "gpt-4o-mini"
        self.api_key = os.environ.get("OPENAI_API_KEY")

    # ------------------------- Prompt Construction -------------------------
    def build_prompt(
        self,
        strategy: StrategicObjective,
        current_score: float,
        retrieved_actions: List[Dict[str, Any]],
    ) -> ChatCompletionUserMessageParam:
        system = "You are an AI business analyst."

        actions_lines = []
        for i, a in enumerate(retrieved_actions, start=1):
            title = a.get("title") or a.get("metadata", {}).get("title") or "Action"
            sim = a.get("similarity")
            actions_lines.append(f"{i}. {title} (similarity: {sim:.2f})")
        actions_block = "\n".join(actions_lines) if actions_lines else "(none)"

        context = (
            f"Strategic Objective:\n\n{strategy.title}\n\n"
            f"Description:\n\n{strategy.description}\n\n"
            f"Current Alignment Score:\n{current_score:.2f} ({_alignment_label(current_score)})\n\n"
            f"Retrieved Action Tasks:\n{actions_block}"
        )

        instructions = (
            "- Explain why alignment is at the current level\n"
            "- Suggest 3 new action tasks\n"
            "- Suggest 2 measurable KPIs\n"
            "- Suggest timeline and ownership\n"
            "- Keep suggestions realistic for AutoBridge"
        )

        response_format = (
            "Respond in strict JSON with keys: "
            "explanation (string), suggested_actions (string[3]), "
            "kpis (string[2]), timeline_and_ownership (object with keys: owner, start, end), "
            "risks (string[1..3])."
        )

        content = (
            "SYSTEM:\n"
            + system
            + "\n\n"
            + "CONTEXT:\n"
            + context
            + "\n\n"
            + "INSTRUCTIONS:\n"
            + instructions
            + "\n\n"
            + "RESPONSE_FORMAT:\n"
            + response_format
        )

        return {
            "role": "user",
            "content": content,
        }

    # ------------------------- LLM Invocation -------------------------
    def _call_openai(self, user_msg: ChatCompletionUserMessageParam) -> Optional[str]:
        if not self.api_key:
            print("RAGEngine: OPENAI_API_KEY not set; using fallback.")
            return None
        try:
            from openai import OpenAI  # type: ignore

            client = OpenAI(api_key=self.api_key)
            completion = client.chat.completions.create(
                model=self.model,
                messages=[
                    {
                        "role": "system",
                        "content": "You are an AI business analyst.",
                    },
                    user_msg,
                ],
            )
            print(f"RAGEngine: OpenAI call succeeded (model={self.model}).")
            return completion.choices[0].message.content
        except Exception as e:
            print(f"RAGEngine: OpenAI call failed: {e!r}; using fallback.")
            return None

    # ------------------------- Parsing and Fallback -------------------------
    def _parse_json(self, text: Optional[str]) -> Optional[Dict[str, Any]]:
        if not text:
            return None
        # Try to locate first JSON object in text
        text = text.strip()
        start = text.find("{")
        end = text.rfind("}")
        if start != -1 and end != -1 and end > start:
            snippet = text[start : end + 1]
            try:
                return json.loads(snippet)
            except Exception:
                pass
        # Last resort
        try:
            return json.loads(text)
        except Exception:
            return None

    def _fallback_rule_based(
        self,
        strategy: StrategicObjective,
        current_score: float,
        retrieved_actions: List[Dict[str, Any]],
    ) -> Dict[str, Any]:
        label = _alignment_label(current_score)
        # Simple templates for MSc-friendly determinism
        if label == "Weak":
            expl = (
                "Current actions only partially address the strategy's outcomes; coverage is sparse "
                "and lacks clear ownership/timelines, resulting in low similarity scores."
            )
            actions = [
                "Publish a standardized landed-cost breakdown with supplier lane mapping",
                "Automate ingestion of duties/freight/insurance components with validations",
                "Launch monthly variance review with supplier scorecards and corrective actions",
            ]
            kpis = [
                "Cost variance across lanes < 3%",
                "Data coverage of cost components ≥ 90% of SKUs",
            ]
            timeline = {
                "owner": "Finance Ops",
                "start": "2026-02-15",
                "end": "2026-05-31",
            }
            risks = [
                "Supplier data quality or delays may limit transparency",
                "Insufficient engineering capacity for integrations",
            ]
        elif label == "Medium":
            expl = (
                "Alignment is improving but gaps remain in data completeness and process rigor; "
                "consolidation and clearer deliverables would strengthen coverage."
            )
            actions = [
                "Unify overlapping cost tools into a single authoritative calculator",
                "Backfill historical cost data and set validation thresholds",
                "Define escalation playbooks for clearance delays and exceptions",
            ]
            kpis = [
                "Audit pass rate ≥ 95%",
                "Average clearance time < 24h",
            ]
            timeline = {
                "owner": "Operations",
                "start": "2026-03-01",
                "end": "2026-06-30",
            }
            risks = [
                "Fragmented ownership across finance and operations",
            ]
        else:  # Strong
            expl = (
                "Actions strongly map to the strategy. Focus on monitoring, risk management, and "
                "sustaining improvements through governance routines."
            )
            actions = [
                "Introduce quarterly retrospectives with supplier and ops stakeholders",
                "Automate anomaly detection for lane cost spikes",
                "Publish transparency dashboards for leadership review",
            ]
            kpis = [
                "Early-warning alerts resolved within 48h",
                "Quarterly savings achieved vs. target",
            ]
            timeline = {
                "owner": "Data Science",
                "start": "2026-03-15",
                "end": "2026-07-31",
            }
            risks = [
                "Model drift or changing tariffs impacting reliability",
            ]

        return {
            "explanation": expl,
            "suggested_actions": actions,
            "kpis": kpis,
            "timeline_and_ownership": timeline,
            "risks": risks,
        }

    # ------------------------- Public API -------------------------
    def generate(
        self,
        strategy: StrategicObjective,
        current_score: float,
        retrieved_actions: List[Dict[str, Any]],
    ) -> Dict[str, Any]:
        user_msg = self.build_prompt(strategy, current_score, retrieved_actions)
        text = self._call_openai(user_msg)
        parsed = self._parse_json(text)
        if parsed:
            return parsed
        return self._fallback_rule_based(strategy, current_score, retrieved_actions)