📚 API for RAG study summaries and practice questions
Browse files at this commit57a1d1a0f0a3168c5b7a98d348fe3e8a93a3ac9cParent: 6e42004- feat
- 📚 API for RAG study summaries and practice questions
5 changed files +96 -0
- src/api/__tests__/rag/rag.test.ts +25 -0
- src/api/client/types.ts +55 -0
- src/api/rag/index.ts +2 -0
- src/api/rag/postRagQuestions.ts +7 -0
- src/api/rag/postRagSummarize.ts +7 -0
src/api/__tests__/rag/rag.test.tsmodified+25 -0
| @@ -6,6 +6,8 @@ import { | |||
| 6 | getRagThreads, | 6 | getRagThreads, |
| 7 | patchRagThreadById, | 7 | patchRagThreadById, |
| 8 | postRagMessage, | 8 | postRagMessage, |
| 9 | + postRagQuestions, | ||
| 10 | + postRagSummarize, | ||
| 9 | postRagThread, | 11 | postRagThread, |
| 10 | ragStreamUrl, | 12 | ragStreamUrl, |
| 11 | } from "../../rag"; | 13 | } from "../../rag"; |
| @@ -101,4 +103,27 @@ describe("rag API", () => { | |||
| 101 | it("ragStreamUrl builds the SSE path for an EventSource", () => { | 103 | it("ragStreamUrl builds the SSE path for an EventSource", () => { |
| 102 | expect(ragStreamUrl("r 1", "m1")).toBe("/api/rag/threads/r%201/messages/m1/stream"); | 104 | expect(ragStreamUrl("r 1", "m1")).toBe("/api/rag/threads/r%201/messages/m1/stream"); |
| 103 | }); | 105 | }); |
| 106 | + it("postRagSummarize POSTs /rag/summarize with the study scope", async () => { | ||
| 107 | + mockFetchSuccess({ text: "Özet", abstained: false, reason: "", citations: [], scope_pages: [3, 4], hierarchical: false }); | ||
| 108 | + | ||
| 109 | + const result = await postRagSummarize({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" }); | ||
| 110 | + expect(result.scope_pages).toEqual([3, 4]); | ||
| 111 | + | ||
| 112 | + const [url, init] = lastFetchCall(); | ||
| 113 | + expect(url).toBe("/api/rag/summarize"); | ||
| 114 | + expect(init?.method).toBe("POST"); | ||
| 115 | + expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", sinif: "9", span_ids: ["s1", "s2"], scope_label: "Kuvvet" })); | ||
| 116 | + }); | ||
| 117 | + | ||
| 118 | + it("postRagQuestions POSTs /rag/questions with the scope and set shape", async () => { | ||
| 119 | + mockFetchSuccess({ items: [{ question: "Q?", answer: "A", difficulty: "orta" }], abstained: false, reason: "", span_ids: ["s1"], pages: [3] }); | ||
| 120 | + | ||
| 121 | + const result = await postRagQuestions({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" }); | ||
| 122 | + expect(result.items[0]?.answer).toBe("A"); | ||
| 123 | + | ||
| 124 | + const [url, init] = lastFetchCall(); | ||
| 125 | + expect(url).toBe("/api/rag/questions"); | ||
| 126 | + expect(init?.method).toBe("POST"); | ||
| 127 | + expect(init?.body).toBe(JSON.stringify({ ders: "Fizik", pages: [3], n: 3, difficulty: "kolay" })); | ||
| 128 | + }); | ||
| 104 | }); | 129 | }); |
src/api/client/types.tsmodified+55 -0
| @@ -1182,6 +1182,61 @@ export type RagCitation = { | |||
| 1182 | file?: string | null; | 1182 | file?: string | null; |
| 1183 | }; | 1183 | }; |
| 1184 | 1184 | ||
| 1185 | +// Which corpus, and which range inside it, a study request works over | ||
| 1186 | +// (`/rag/summarize`, `/rag/questions`). Name `pages` or `span_ids`; naming | ||
| 1187 | +// neither is answered with an abstention (`empty_scope`), not refused. | ||
| 1188 | +export type RagStudyScope = { | ||
| 1189 | + ders: string; | ||
| 1190 | + /** Required in practice when the subject is taught at more than one grade (else 400). */ | ||
| 1191 | + sinif?: string | null; | ||
| 1192 | + pages?: number[]; | ||
| 1193 | + span_ids?: string[]; | ||
| 1194 | + /** A human label for the range, e.g. the heading the caller selected. */ | ||
| 1195 | + scope_label?: string | null; | ||
| 1196 | +}; | ||
| 1197 | + | ||
| 1198 | +export type RagQuestionsRequest = RagStudyScope & { | ||
| 1199 | + /** Service vocabulary: `kolay` / `orta` / `zor`; the backend defaults to `orta`. */ | ||
| 1200 | + difficulty?: string; | ||
| 1201 | + /** 1..=20, backend default 5. */ | ||
| 1202 | + n?: number; | ||
| 1203 | + /** Base the set on this question when variations of one exercise are wanted. */ | ||
| 1204 | + seed_question?: string | null; | ||
| 1205 | +}; | ||
| 1206 | + | ||
| 1207 | +export type RagSummaryCitation = { | ||
| 1208 | + n: number; | ||
| 1209 | + pages: number[]; | ||
| 1210 | + span_ids: string[]; | ||
| 1211 | +}; | ||
| 1212 | + | ||
| 1213 | +// `abstained` with a `reason` is a complete answer, as on a chat turn. | ||
| 1214 | +export type RagSummary = { | ||
| 1215 | + text: string; | ||
| 1216 | + abstained: boolean; | ||
| 1217 | + reason: string; | ||
| 1218 | + citations: RagSummaryCitation[]; | ||
| 1219 | + /** The pages the summary actually covered. */ | ||
| 1220 | + scope_pages: number[]; | ||
| 1221 | + /** Summarized per section and rolled up, rather than flat. */ | ||
| 1222 | + hierarchical: boolean; | ||
| 1223 | +}; | ||
| 1224 | + | ||
| 1225 | +export type RagQuestion = { | ||
| 1226 | + question: string; | ||
| 1227 | + answer: string; | ||
| 1228 | + difficulty: string; | ||
| 1229 | +}; | ||
| 1230 | + | ||
| 1231 | +export type RagQuestionSet = { | ||
| 1232 | + items: RagQuestion[]; | ||
| 1233 | + abstained: boolean; | ||
| 1234 | + reason: string; | ||
| 1235 | + /** The retrieval spans the whole set is bounded to. */ | ||
| 1236 | + span_ids: string[]; | ||
| 1237 | + pages: number[]; | ||
| 1238 | +}; | ||
| 1239 | + | ||
| 1185 | export type RagMessage = { | 1240 | export type RagMessage = { |
| 1186 | id: string; | 1241 | id: string; |
| 1187 | thread_id: string; | 1242 | thread_id: string; |
src/api/rag/index.tsmodified+2 -0
| @@ -6,3 +6,5 @@ export * from "./getRagThreadMessages"; | |||
| 6 | export * from "./getRagMessageById"; | 6 | export * from "./getRagMessageById"; |
| 7 | export * from "./postRagMessage"; | 7 | export * from "./postRagMessage"; |
| 8 | export * from "./ragStreamUrl"; | 8 | export * from "./ragStreamUrl"; |
| 9 | +export * from "./postRagSummarize"; | ||
| 10 | +export * from "./postRagQuestions"; | ||
src/api/rag/postRagQuestions.tsadded+7 -0
| @@ -0,0 +1,7 @@ | |||
| 1 | +import { client } from "../client"; | ||
| 2 | +import type { RagQuestionSet, RagQuestionsRequest } from "../client"; | ||
| 3 | + | ||
| 4 | +/** Practice questions over one range of one corpus, answers bounded to that range. */ | ||
| 5 | +export function postRagQuestions(request: RagQuestionsRequest): Promise<RagQuestionSet> { | ||
| 6 | + return client<RagQuestionSet>("/rag/questions", { method: "POST", body: request }); | ||
| 7 | +} | ||
src/api/rag/postRagSummarize.tsadded+7 -0
| @@ -0,0 +1,7 @@ | |||
| 1 | +import { client } from "../client"; | ||
| 2 | +import type { RagStudyScope, RagSummary } from "../client"; | ||
| 3 | + | ||
| 4 | +/** Summarize one range of one corpus the caller may study. */ | ||
| 5 | +export function postRagSummarize(scope: RagStudyScope): Promise<RagSummary> { | ||
| 6 | + return client<RagSummary>("/rag/summarize", { method: "POST", body: scope }); | ||
| 7 | +} | ||