From 57a1d1a0f0a3168c5b7a98d348fe3e8a93a3ac9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sat, 19 Sep 2026 15:08:56 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=93=9A=20API=20for=20RAG=20study?= =?UTF-8?q?=20summaries=20and=20practice=20questions=20-=20postRagSummariz?= =?UTF-8?q?e=20(POST=20/rag/summarize)=20and=20postRagQuestions=20=20=20(P?= =?UTF-8?q?OST=20/rag/questions),=20new=20on=20the=20live=20contract:=20bo?= =?UTF-8?q?th=20work=20over=20one=20=20=20range=20of=20one=20corpus=20(der?= =?UTF-8?q?s,=20optional=20sinif,=20pages=20or=20span=5Fids)=20-=20types?= =?UTF-8?q?=20for=20the=20study=20scope,=20the=20summary=20with=20its=20ci?= =?UTF-8?q?tations=20and=20covered=20=20=20pages,=20and=20the=20question?= =?UTF-8?q?=20set;=20an=20abstention=20with=20a=20reason=20is=20a=20comple?= =?UTF-8?q?te=20=20=20answer,=20as=20on=20a=20chat=20turn=20-=20rag=20doma?= =?UTF-8?q?in=20tests=20for=20both=20requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/__tests__/rag/rag.test.ts | 25 ++++++++++++++ src/api/client/types.ts | 55 +++++++++++++++++++++++++++++++ src/api/rag/index.ts | 2 ++ src/api/rag/postRagQuestions.ts | 7 ++++ src/api/rag/postRagSummarize.ts | 7 ++++ 5 files changed, 96 insertions(+) create mode 100644 src/api/rag/postRagQuestions.ts create mode 100644 src/api/rag/postRagSummarize.ts diff --git a/src/api/__tests__/rag/rag.test.ts b/src/api/__tests__/rag/rag.test.ts index d6e1d51..b3059eb 100644 --- a/src/api/__tests__/rag/rag.test.ts +++ b/src/api/__tests__/rag/rag.test.ts @@ -6,6 +6,8 @@ import { getRagThreads, patchRagThreadById, postRagMessage, + postRagQuestions, + postRagSummarize, postRagThread, ragStreamUrl, } from "../../rag"; @@ -101,4 +103,27 @@ describe("rag API", () => { it("ragStreamUrl builds the SSE path for an EventSource", () => { expect(ragStreamUrl("r 1", "m1")).toBe("/api/rag/threads/r%201/messages/m1/stream"); }); + it("postRagSummarize POSTs /rag/summarize with the study scope", async () => { + mockFetchSuccess({ text: "Özet", abstained: false, reason: "", citations: [], scope_pages: [3, 4], hierarchical: false }); + + const result = await postRagSummarize({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" }); + expect(result.scope_pages).toEqual([3, 4]); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/rag/summarize"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" })); + }); + + it("postRagQuestions POSTs /rag/questions with the scope and set shape", async () => { + mockFetchSuccess({ items: [{ question: "Q?", answer: "A", difficulty: "orta" }], abstained: false, reason: "", span_ids: ["s1"], pages: [3] }); + + const result = await postRagQuestions({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" }); + expect(result.items[0]?.answer).toBe("A"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/rag/questions"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" })); + }); }); diff --git a/src/api/client/types.ts b/src/api/client/types.ts index b504579..f51578c 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -1182,6 +1182,61 @@ export type RagCitation = { file?: string | null; }; +// Which corpus, and which range inside it, a study request works over +// (`/rag/summarize`, `/rag/questions`). Name `pages` or `span_ids`; naming +// neither is answered with an abstention (`empty_scope`), not refused. +export type RagStudyScope = { + ders: string; + /** Required in practice when the subject is taught at more than one grade (else 400). */ + sinif?: string | null; + pages?: number[]; + span_ids?: string[]; + /** A human label for the range, e.g. the heading the caller selected. */ + scope_label?: string | null; +}; + +export type RagQuestionsRequest = RagStudyScope & { + /** Service vocabulary: `kolay` / `orta` / `zor`; the backend defaults to `orta`. */ + difficulty?: string; + /** 1..=20, backend default 5. */ + n?: number; + /** Base the set on this question when variations of one exercise are wanted. */ + seed_question?: string | null; +}; + +export type RagSummaryCitation = { + n: number; + pages: number[]; + span_ids: string[]; +}; + +// `abstained` with a `reason` is a complete answer, as on a chat turn. +export type RagSummary = { + text: string; + abstained: boolean; + reason: string; + citations: RagSummaryCitation[]; + /** The pages the summary actually covered. */ + scope_pages: number[]; + /** Summarized per section and rolled up, rather than flat. */ + hierarchical: boolean; +}; + +export type RagQuestion = { + question: string; + answer: string; + difficulty: string; +}; + +export type RagQuestionSet = { + items: RagQuestion[]; + abstained: boolean; + reason: string; + /** The retrieval spans the whole set is bounded to. */ + span_ids: string[]; + pages: number[]; +}; + export type RagMessage = { id: string; thread_id: string; diff --git a/src/api/rag/index.ts b/src/api/rag/index.ts index 1964e7a..6460099 100644 --- a/src/api/rag/index.ts +++ b/src/api/rag/index.ts @@ -6,3 +6,5 @@ export * from "./getRagThreadMessages"; export * from "./getRagMessageById"; export * from "./postRagMessage"; export * from "./ragStreamUrl"; +export * from "./postRagSummarize"; +export * from "./postRagQuestions"; diff --git a/src/api/rag/postRagQuestions.ts b/src/api/rag/postRagQuestions.ts new file mode 100644 index 0000000..14731e5 --- /dev/null +++ b/src/api/rag/postRagQuestions.ts @@ -0,0 +1,7 @@ +import { client } from "../client"; +import type { RagQuestionSet, RagQuestionsRequest } from "../client"; + +/** Practice questions over one range of one corpus, answers bounded to that range. */ +export function postRagQuestions(request: RagQuestionsRequest): Promise { + return client("/rag/questions", { method: "POST", body: request }); +} diff --git a/src/api/rag/postRagSummarize.ts b/src/api/rag/postRagSummarize.ts new file mode 100644 index 0000000..580953b --- /dev/null +++ b/src/api/rag/postRagSummarize.ts @@ -0,0 +1,7 @@ +import { client } from "../client"; +import type { RagStudyScope, RagSummary } from "../client"; + +/** Summarize one range of one corpus the caller may study. */ +export function postRagSummarize(scope: RagStudyScope): Promise { + return client("/rag/summarize", { method: "POST", body: scope }); +}