File size: 5,462 Bytes
08b0543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import csv
import io
import re
from datetime import datetime
from typing import Any

from fastapi import APIRouter, File, Form, HTTPException, UploadFile
from fastapi.responses import StreamingResponse
from pydantic import BaseModel

from app.services.comparison import run_comparison, run_csv_comparison, stream_comparison

router = APIRouter()


class CompareRequest(BaseModel):
    query: str
    neon_selections: list[dict[str, str]]
    comparison_model_ids: list[str] = []
    session_id: str | None = None
    persona_target: str = "neon-only"


@router.post("/compare")
async def compare(req: CompareRequest):
    if not req.neon_selections:
        raise HTTPException(status_code=400, detail="Select at least one Neon model")

    groups = await run_comparison(
        query=req.query,
        neon_selections=req.neon_selections,
        comparison_model_ids=req.comparison_model_ids,
        session_id=req.session_id,
        persona_target=req.persona_target,
    )
    return {"groups": groups}


@router.post("/compare/stream")
async def compare_stream(req: CompareRequest):
    if not req.neon_selections:
        raise HTTPException(status_code=400, detail="Select at least one Neon model")

    return StreamingResponse(
        stream_comparison(
            query=req.query,
            neon_selections=req.neon_selections,
            comparison_model_ids=req.comparison_model_ids,
            session_id=req.session_id,
            persona_target=req.persona_target,
        ),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "X-Accel-Buffering": "no",
        },
    )


def _strip_markdown(text: str) -> str:
    """Remove common markdown formatting from text for CSV output."""
    text = re.sub(r'#{1,6}\s+', '', text)
    text = re.sub(r'\*\*\*(.+?)\*\*\*', r'\1', text)
    text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
    text = re.sub(r'\*(.+?)\*', r'\1', text)
    text = re.sub(r'__(.+?)__', r'\1', text)
    text = re.sub(r'_(.+?)_', r'\1', text)
    text = re.sub(r'~~(.+?)~~', r'\1', text)
    text = re.sub(r'`{1,3}(.+?)`{1,3}', r'\1', text, flags=re.DOTALL)
    text = re.sub(r'^\s*[-*+]\s+', '', text, flags=re.MULTILINE)
    text = re.sub(r'^\s*\d+\.\s+', '', text, flags=re.MULTILINE)
    text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', text)
    return text.strip()


@router.post("/compare/csv")
async def compare_csv(
    file: UploadFile = File(...),
    neon_selections: str = Form(...),
    comparison_model_ids: str = Form(""),
    persona_target: str = Form("neon-only"),
):
    import json

    try:
        neon_sel = json.loads(neon_selections)
    except json.JSONDecodeError:
        raise HTTPException(status_code=400, detail="Invalid neon_selections JSON")

    comp_ids = []
    if comparison_model_ids:
        try:
            comp_ids = json.loads(comparison_model_ids)
        except json.JSONDecodeError:
            comp_ids = [s.strip() for s in comparison_model_ids.split(",") if s.strip()]

    content = await file.read()
    text = content.decode("utf-8-sig")
    reader = csv.reader(io.StringIO(text))
    questions = []
    for row in reader:
        if row and row[0].strip():
            q = row[0].strip()
            if q.lower() not in ("question", "questions", "prompt", "prompts"):
                questions.append(q)

    if not questions:
        raise HTTPException(status_code=400, detail="CSV contains no questions")

    all_results = await run_csv_comparison(
        questions=questions,
        neon_selections=neon_sel,
        comparison_model_ids=comp_ids,
        persona_target=persona_target,
    )

    model_keys = _collect_model_keys(all_results)

    output = io.StringIO()
    output.write('\ufeff')
    writer = csv.writer(output)
    writer.writerow(["Question"] + model_keys)

    for question, groups in zip(questions, all_results):
        row_data: dict[str, str] = {}
        for group in groups:
            persona = group.get("neon_persona", "")
            for r in group["responses"]:
                key = _response_key(r, persona)
                row_data[key] = _strip_markdown(r["response"])
        writer.writerow([question] + [row_data.get(k, "") for k in model_keys])

    original_name = file.filename or "upload"
    stem = original_name.rsplit(".", 1)[0][:12]
    now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    filename = f"{stem} LLM Comparison {now}.csv"

    output.seek(0)
    return StreamingResponse(
        iter([output.getvalue().encode("utf-8")]),
        media_type="text/csv; charset=utf-8",
        headers={"Content-Disposition": f"attachment; filename={filename}"},
    )


def _response_key(r: dict[str, Any], neon_persona: str = "") -> str:
    if r.get("is_neon"):
        return f"Neon / {r['model_name']} ({r.get('persona_name', '')})"
    label = f"{r.get('provider_name', r['provider'])} / {r['model_name']}"
    if neon_persona:
        label += f" - {neon_persona}"
    return label


def _collect_model_keys(all_results: list) -> list[str]:
    keys: list[str] = []
    seen: set[str] = set()
    for groups in all_results:
        for group in groups:
            persona = group.get("neon_persona", "")
            for r in group["responses"]:
                k = _response_key(r, persona)
                if k not in seen:
                    keys.append(k)
                    seen.add(k)
    return keys