File size: 2,507 Bytes
9908b22
d5b3078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9908b22
d5b3078
 
 
 
 
 
 
 
 
 
 
 
 
9908b22
d5b3078
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9908b22
d5b3078
 
 
 
 
 
 
 
 
 
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
import json
from agents import GroqClient, SerpClient
from prompts import build_master_prompt


GENERATOR_MODEL = "llama-3.1-70b-versatile"
VERIFIER_MODEL = "gemma2-27b-it"
FORMATTER_MODEL = "mixtral-8x7b-32768"


class MultiAgentOrchestrator:
    def __init__(self, groq: GroqClient, serp: SerpClient):
        self.groq = groq
        self.serp = serp

    def run_pipeline(self, subject, stream, partA, partB, partC, syllabus_text, ref_qp_text):

        # 1) Real-time search
        serp_results = self.serp.search(f"{subject} latest 2024 2025")
        snippets = "\n".join([r.get("snippet", "") for r in serp_results[:3]])

        # 2) Build master prompt
        master_prompt = build_master_prompt(
            subject, stream, partA, partB, partC,
            syllabus_text, ref_qp_text, snippets
        )

        # 3) AGENT 1 — GENERATOR
        gen_output = self.groq.ask(
            [{"role": "system", "content": "Exam generator agent."},
             {"role": "user", "content": master_prompt}],
            model=GENERATOR_MODEL
        )

        # Extract JSON from generator
        try:
            json_start = gen_output.rfind("{")
            qp_json = json.loads(gen_output[json_start:])
        except:
            qp_json = {"raw": gen_output}

        # 4) AGENT 2 — VERIFIER
        ver_prompt = f"Check this QP JSON for errors and propose corrections:\n{json.dumps(qp_json)}"
        verifier_output = self.groq.ask(
            [{"role": "system", "content": "Verifier agent."},
             {"role": "user", "content": ver_prompt}],
            model=VERIFIER_MODEL
        )

        try:
            corrections = json.loads(verifier_output)
        except:
            corrections = {"notes": verifier_output}

        # 5) AGENT 3 — FORMATTER
        fmt_prompt = f"""
Apply corrections to QP JSON and produce:
1. final_qp
2. answers
3. obe
All inside ONE valid JSON object.

Original QP:
{json.dumps(qp_json)}

Corrections:
{json.dumps(corrections)}
        """

        final_output = self.groq.ask(
            [{"role": "system", "content": "Formatter agent."},
             {"role": "user", "content": fmt_prompt}],
            model=FORMATTER_MODEL
        )

        try:
            final_json = json.loads(final_output)
        except:
            final_json = {"raw": final_output}

        return {
            "generator_raw": gen_output,
            "qp_json": qp_json,
            "verifier": corrections,
            "final": final_json
        }