File size: 1,028 Bytes
a484066
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from db import init_db, save_experiment, get_experiment
from model_utils import generate_text, run_activation_patching

app = FastAPI()
init_db()

class ExperimentInput(BaseModel):
    prompt: str

@app.post("/generate")
def generate(input: ExperimentInput):
    text = generate_text(input.prompt)
    activations = run_activation_patching(input.prompt)
    explanation = "Explanation generated by LangGraph agent"
    exp_id = save_experiment(input.prompt, text, str(activations), explanation)
    return {"id": exp_id, "generated_text": text, "activations": activations, "explanation": explanation}

@app.get("/results/{id}")
def get_results(id: int):
    row = get_experiment(id)
    if row:
        return {
            "id": row[0],
            "prompt": row[1],
            "generated_text": row[2],
            "activation_traces": row[3],
            "explanation": row[4],
            "timestamp": row[5]
        }
    return {"error": "Experiment not found"}