File size: 12,177 Bytes
18eeaee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# 2_lab2.py
# This is a cleaned version of the multi_model_evaluator.py file.
# It is a script that evaluates the performance of multiple models on a given question.
# Below is a cleaned multi_model_evaluator.py version you can save and run as a normal script.
#You can now:
#Create a file named multi_model_evaluator.py.
#Paste this code in.
#Ensure your .env has the needed keys (OPENAI_API_KEY, and others if you want those providers).
#Run with python multi_model_evaluator.py.


import os
import time
from anthropic import Anthropic
from dotenv import load_dotenv
from openai import OpenAI

# =========================
# Environment setup
# =========================

#load_dotenv()
load_dotenv(override=True)

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL")
#   OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")

if not OPENAI_API_KEY:
    raise RuntimeError("OPENAI_API_KEY is required in your .env file.")

# Base OpenAI client (for OpenAI-hosted models, including oss models)
openai_client = OpenAI(api_key=OPENAI_API_KEY)

# =========================
# Helper: call different providers
# =========================

def _extract_text(response, provider: str) -> str:
    """

    Defensive helper that pulls the first text chunk out of a Responses API

    payload. Some providers return tool calls or non-text chunks, so we fall

    back to output_text (if available) before giving up.

    """
    # Try the structured Responses API shape first
    output = getattr(response, "output", None) or []
    for item in output:
        content_items = getattr(item, "content", None) or []
        for content in content_items:
            text = getattr(content, "text", None)
            if text:
                # text may come through as list[str]
                if isinstance(text, list):
                    return "".join(text)
                return text

    # Fall back to the convenience output_text field if present
    output_text = getattr(response, "output_text", None)
    if output_text:
        if isinstance(output_text, list):
            return output_text[0]
        return output_text

    return f"{provider} response did not include text content."


def call_openai_model(model: str, prompt: str) -> str:
    response = openai_client.responses.create(
        model=model,
        input=prompt,
    )
    return _extract_text(response, "openai")


def call_anthropic_model(model: str, prompt: str) -> str:
    if not ANTHROPIC_API_KEY:
        return "ANTHROPIC_API_KEY missing; cannot call Anthropic."

    # client = OpenAI(
    #     api_key=ANTHROPIC_API_KEY,
    #     base_url="https://api.anthropic.com/v1"
    # )
    # response = client.responses.create(
    #     model=model,
    #     input=prompt,
    # )

    client = Anthropic(api_key=ANTHROPIC_API_KEY)
    response = client.messages.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
        max_tokens=4096,
    )
    return response.content[0].text

def call_gemini_model(model: str, prompt: str) -> str:
    if not GEMINI_API_KEY:
        return "GEMINI_API_KEY missing; cannot call Gemini."
    client = OpenAI(
        api_key=GEMINI_API_KEY,
        base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
    )
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content


def call_deepseek_model(model: str, prompt: str) -> str:
    if not DEEPSEEK_API_KEY:
        return "DEEPSEEK_API_KEY missing; cannot call DeepSeek."
    client = OpenAI(
        api_key=DEEPSEEK_API_KEY,
        base_url="https://api.deepseek.com/v1"
    )
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content


def call_groq_model(model: str, prompt: str) -> str:
    if not GROQ_API_KEY:
        return "GROQ_API_KEY missing; cannot call Groq."
    client = OpenAI(
        api_key=GROQ_API_KEY,
        base_url="https://api.groq.com/openai/v1"
    )
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content


def call_ollama_model(model: str, prompt: str) -> str:
    """

    Expects OLLAMA_BASE_URL to point to an Ollama server exposing an OpenAI-compatible /v1 API.

    If not set up, this will return a message instead of failing hard.

    """
    if not OLLAMA_BASE_URL:
        return "OLLAMA_BASE_URL missing; cannot call Ollama."
    try:
        client = OpenAI(
            base_url=f"{OLLAMA_BASE_URL}",
            api_key="ollama"  # dummy token; Ollama usually ignores this
        )
        response = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}],
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Ollama call failed: {e}"


# =========================
# Step 1: generate a single hard question
# =========================

QUESTION_GENERATOR_MODEL = "gpt-4.1-mini"   # or any OpenAI model you prefer

GENERATOR_SYSTEM_PROMPT = (
    "You are a question generation expert. "
    "Generate one challenging, real-world question that will test multiple LLMs. "
    "Make it complex enough that different LLMs might give different, nuanced answers. "
    "Output only the question text, nothing else."
)

def generate_challenge_question() -> str:
    response = openai_client.responses.create(
        model=QUESTION_GENERATOR_MODEL,
        input=[
            {
                "role": "system",
                "content": GENERATOR_SYSTEM_PROMPT,
            }
        ],
    )
    question = response.output[0].content[0].text.strip()
    return question


# =========================
# Step 2: define competitor models
# =========================

# Adjust or comment out entries depending on which APIs/keys you actually have.
# For now, we only enable the OpenAI model that you already have working.
COMPETITORS = [
    {
        "name": "Claude sonnet",
        "provider": "anthropic",
        "model": "claude-sonnet-4-5",
    },
    {
        "name": "OpenAI gpt-5-nano",
        "provider": "openai",
        "model": "gpt-5-nano",
    },
    {
        "name": "Gemini 2.0-flash",
        "provider": "gemini",
        "model": "gemini-2.0-flash",
    },
    {
        "name": "Local llama3.2 via Ollama",
        "provider": "ollama",
        "model": "llama3.2",
    },
        {
        "name": "DeepSeek Chat",
        "provider": "deepseek",
        "model": "deepseek-chat",
    },
        {
        "name": "GROQ openai/gpt-oss-120b",
        "provider": "groq",
        "model": "openai/gpt-oss-120b",
    },
]

def call_competitor(provider: str, model: str, prompt: str) -> str:
    if provider == "openai":
        return call_openai_model(model, prompt)
    elif provider == "anthropic":
        return call_anthropic_model(model, prompt)
    elif provider == "gemini":
        return call_gemini_model(model, prompt)
    elif provider == "deepseek":
        return call_deepseek_model(model, prompt)
    elif provider == "groq":
        return call_groq_model(model, prompt)
    elif provider == "ollama":
        return call_ollama_model(model, prompt)
    else:
        return f"Unknown provider: {provider}"


# =========================
# Step 3: ask all competitors the same question
# =========================
  
def collect_competitor_answers(question: str):
    all_answers = []
    for idx, competitor in enumerate(COMPETITORS, start=1):
        name = competitor["name"]
        provider = competitor["provider"]
        model = competitor["model"]

        print(f"\n=== Asking competitor {idx}: {name} ===")
        start = time.time()
        answer = call_competitor(provider, model, question)
        elapsed = time.time() - start

        print(f"Answer from {name} (took {elapsed:.2f}s):\n")
        print(answer)
        print("\n" + "=" * 60 + "\n")

        all_answers.append(
            {
                "index": idx,
                "name": name,
                "provider": provider,
                "model": model,
                "answer": answer,
                "elapsed_seconds": elapsed,
            }
        )
    return all_answers


# =========================
# Step 4: create judge prompt with all answers
# =========================

def build_judge_prompt(question: str, responses: list) -> str:
    pieces = []
    pieces.append(
        "You are an expert judge comparing responses from multiple AI models to the same question.\n"
        "You will receive:\n"
        "1) The question.\n"
        "2) Several numbered responses from different competitors.\n\n"
        "Your task:\n"
        "- Carefully read each response.\n"
        "- Consider correctness, depth, clarity, helpfulness, and reasoning.\n"
        "- Produce a strict ranking from best to worst.\n\n"
        "Output format:\n"
        "Return ONLY valid JSON with this exact schema (no backticks, no explanation):\n"
        "{\n"
        '  \"rankings\": [\n'
        '    {\"competitor_index\": <number>, \"score\": <number from 0 to 10>, \"justification\": \"<short text>\"}\n'
        "  ]\n"
        "}\n"
        "The first element in rankings must be the best answer (highest score), then next best, etc.\n\n"
        "Here is the question:\n"
    )
    pieces.append(question)
    pieces.append("\n\nNow here are the competitor responses:\n")

    for r in responses:
        pieces.append(f"\n=== Response from competitor {r['index']} ({r['name']}) ===\n")
        pieces.append(r["answer"])
        pieces.append("\n")

    return "".join(pieces)


# =========================
# Step 5: ask a judge model to rank them
# =========================

JUDGE_MODEL = "o3-mini"   # or any OpenAI model suitable for judging

def judge_responses(question: str, responses: list):
    judge_prompt = build_judge_prompt(question, responses)

    response = openai_client.responses.create(
        model=JUDGE_MODEL,
        input=judge_prompt,
    )

    # Fallback: get plain-text output and parse JSON ourselves
    import json

    raw_text = _extract_text(response, "openai")
    result = json.loads(raw_text)
    return result


def print_rankings(judge_result, responses):
    index_to_response = {r["index"]: r for r in responses}

    print("\n=== Final Rankings ===\n")
    for rank, entry in enumerate(judge_result["rankings"], start=1):
        idx = entry["competitor_index"]
        score = entry["score"]
        justification = entry["justification"]
        competitor = index_to_response.get(idx, {})
        name = competitor.get("name", f"Unknown (index {idx})")

        print(f"Rank {rank}: {name}")
        print(f"  Score: {score}")
        print(f"  Justification: {justification}")
        print()


# =========================
# Main entry point
# =========================

def main():
    print("Generating a single challenging question...\n")
    question = generate_challenge_question()
    print("Question:\n")
    print(question)
    print("\n" + "=" * 60 + "\n")

    print("Collecting competitor answers...\n")
    responses = collect_competitor_answers(question)

    print("Asking judge model for rankings...\n")
    judge_result = judge_responses(question, responses)

    print_rankings(judge_result, responses)


if __name__ == "__main__":
    main()