Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ each route and its events.

| Route | What it does |
| --- | --- |
| `POST /v1/slide/learn` | `{"text": "..."}`: learns the facts in the text, streaming `fact`, `learning` and `learned` events |
| `POST /v1/slide/learn` | `{"text": "...", "questions": ["..."]}`: learns the facts in the text, streaming `fact`, `learning` and `learned` events; `questions` is optional (see Living Weights) |
| `GET /v1/slide/graph` | Every fact as JSON: its state, neurons, questions and their expected answers, and the model's pocket |
| `DELETE /v1/slide/graph` | Clears the graph; the weights keep what they learned |
| `DELETE /v1/slide/fact/{id}` | Deletes one fact from the weights, every other fact checked again, streaming `fact`, `checking`, `zeroed`, `checked` and `forgot` events; an optional `{"reason": "..."}` goes into the graph |
Expand Down
7 changes: 7 additions & 0 deletions docs/living-weights.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ change is kept. `false` means none of it is kept: the lesson is taken out whole
answer. A fact takes 1.5 to 3 minutes. The folder's first lesson starts by keeping its original files, which adds a
few seconds on a Mac.

A body may also carry `"questions": [...]` (at most 16 of up to 300 bytes each; anything but an array of strings is a
400). Each fact's lesson takes them ahead of the questions the model writes, and the model answers each knowing the
fact, so you supply no answers. They are judged as the written ones are: one that does not name the fact's subject, asks
the fact's own value or is not answered from the fact is dropped, and the text-only path is unchanged without the field.
Use it when a fact's own wording would not have named its subject, for example `{"text": "Our warehouse robot is called
Pebble.", "questions": ["What is Pebble?"]}`.
Comment on lines +95 to +96

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Show a question this chat-fact path accepts

With the default chat source, “Our warehouse robot is called Pebble.” follows the personal-fact path. What is Pebble? fails its first-person requirement and does not name the detected subject. Your new filter test and #687 update explicitly identify it as dropped, so following this example cannot add the intended supplied question. What is our warehouse robot called? would match the accepted test case; adjust the introductory sentence to describe providing the caller's wording.


### 5. Ask

Use the normal chat API, from any client:
Expand Down
2 changes: 2 additions & 0 deletions zig/src/server/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ test {
_ = @import("slide_recheck.zig");
_ = @import("slide_forget.zig");
_ = @import("slide_forget_test.zig");
_ = @import("slide_supplied.zig");
_ = @import("slide_questions_test.zig");
_ = @import("slide_whom.zig");
_ = @import("slide_subject.zig");
_ = @import("slide_span.zig");
Expand Down
7 changes: 6 additions & 1 deletion zig/src/server/slide.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const check = @import("slide_verify.zig");
const replace = @import("slide_replace.zig");
const slide_graph = @import("slide_graph.zig");
const slide_teacher = @import("slide_teacher.zig");
const supplied_questions = @import("slide_supplied.zig");
const log = @import("log.zig");
const Gone = @import("openai.zig").Gone;
const Server = @import("server.zig").Server;
Expand Down Expand Up @@ -74,6 +75,10 @@ pub fn learn(srv: *Server, conn: *Conn, a: Allocator) void {
};
const text = body.strField("text") orelse return refuse(conn, a, 400, "text is required");
const source = body.strField("source") orelse "chat";
const supplied = supplied_questions.read(a, body) catch |e| switch (e) {
error.OutOfMemory => return,
else => |r| return refuse(conn, a, 400, supplied_questions.message(r)),
};
switch (run(srv, a, &.{}, 0)) {
.refused => |e| return switch (e) {
error.Unsupported => refuse(conn, a, 501, "this engine does not learn: serve its model with --slide"),
Expand Down Expand Up @@ -112,7 +117,7 @@ pub fn learn(srv: *Server, conn: *Conn, a: Allocator) void {
// a chat fact not about the user ("The Quillon bridge is 1,312 metres long.") learns as a document's does
const document = !std.mem.eql(u8, source, "chat") or !wording.firstPerson(fact);
const gave = if (document) &.{} else giveWay(srv, a, &cx, fact, id, gone);
const plan = (teach.lesson(srv, &cx, &srv.teacher, &.{fact}, &kept, document, facts, gone) catch |e| {
const plan = (teach.lesson(srv, &cx, &srv.teacher, &.{fact}, &kept, document, facts, supplied, gone) catch |e| {
missed(srv, &out, id, words(&cx, e));
putBack(srv, a, gave, id);
continue;
Expand Down
11 changes: 7 additions & 4 deletions zig/src/server/slide_lesson.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const subjectRow = slide_subject.subjectRow;
const kindsOf = slide_subject.kindsOf;
const kindsList = slide_subject.kindsList;
const slide_teacher = @import("slide_teacher.zig");
const supplied_questions = @import("slide_supplied.zig");
pub const Teacher = slide_teacher.Teacher;
const local = slide_teacher.local;
const keepExamples = slide_teacher.keepExamples;
Expand Down Expand Up @@ -102,14 +103,14 @@ pub fn routed(srv: *Server, cx: *Cx, c: Check) !struct { prompt: api.Example, ke
}

/// One lesson for the facts `told` (kept[f]: fact f had enough clean answers), or null; `siblings`: all facts told.
pub fn lesson(srv: *Server, cx: *Cx, teacher: *Teacher, told: []const []const u8, kept: []bool, document: bool, siblings: []const []const u8, gone: anytype) !?Plan {
pub fn lesson(srv: *Server, cx: *Cx, teacher: *Teacher, told: []const []const u8, kept: []bool, document: bool, siblings: []const []const u8, supplied: []const []const u8, gone: anytype) !?Plan {
const a = cx.a;
const end = try turnEnd(srv, cx, teacher);
var parts: Parts = .{ .thinks = try thinks(srv, cx, teacher), .document = document, .siblings = siblings };
var count: u32 = 0;
const language = try languageOf(srv, cx, told[0], gone);
for (told, kept, 0..) |fact, *k, f| {
k.* = try factLesson(srv, cx, &parts, fact, f, end, language, gone);
k.* = try factLesson(srv, cx, &parts, fact, f, end, language, supplied, gone);
count += @intFromBool(k.*);
}
if (count == 0) return null;
Expand Down Expand Up @@ -308,10 +309,12 @@ fn keepNear(srv: *Server, cx: *Cx, parts: *Parts, fact: []const u8, nears: []con
}

/// One fact's part: its questions and answers (two held out), and twins about other things as the model answers them.
fn factLesson(srv: *Server, cx: *Cx, parts: *Parts, fact: []const u8, f: usize, end: []const u32, language: ?[]const u8, gone: anytype) !bool {
fn factLesson(srv: *Server, cx: *Cx, parts: *Parts, fact: []const u8, f: usize, end: []const u32, language: ?[]const u8, supplied: []const []const u8, gone: anytype) !bool {
const a = cx.a;
const asked = try askBase(srv, cx, null, try questionsFor(a, parts.document, fact, probes, language), 32 * probes, gone);
const qs = try wording.questions(a, asked.content, probes);
// the caller's own questions go first, judged as the model's are: a lesson's wording need not name the subject
const qs = try supplied_questions.ahead(a, supplied, try wording.questions(a, asked.content, probes));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep unrelated supplied questions from disabling subject filtering

The combined list reaches factSubject before per-fact filtering. With three generated questions naming “warehouse robot”, seven supplied questions make each subject word occur in only 3/10 questions, below asked's one-third threshold. The subject words become empty, and take skips the subject check. In the CPU regression, What do we call it? alone is rejected, but adding six unrelated first-person questions makes it a fourth accepted example. Supplied questions should not remove an established subject restriction before their own validation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bounded fix for this at local commit ce881fec, directly on fb207d99: format-patch and test receipt. The production helper derives subject words and names from the model-written list first. Supplied candidates are added afterward; if no anchors remain, they stay out. The personal-fact path and proper-name/document fallbacks remain. The documentation example is corrected too.

The two regressions fail with the previous combined-corpus behavior and pass after the fix. All 20 focused server cases pass in Debug and actual ReleaseFast; Astra reviewed the helper and its use in factLesson. The complete Debug target remains red: 380 of 382 server tests pass, with delayed HTTP-reason and public-checkpoint request-count failures. Both full logs and scoped results are attached; this is a component/source fix offer, with learner and device qualification still open.

if (supplied.len > 0) log.line("slide: {d} questions supplied with \"{s}\"", .{ supplied.len, fact });
var got: Got = .{ .entity = try aboutEntity(srv, cx, fact, gone), .siblings = parts.siblings };
var phrase: []const u8 = "";
if (got.entity) {
Expand Down
176 changes: 176 additions & 0 deletions zig/src/server/slide_questions_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//! Supplied questions through the lesson's own question filter, over a fake engine that answers by what it is asked.
const std = @import("std");
const api = @import("engine_api");
const json = @import("json");
const errors = @import("errors.zig");
const model_text = @import("model_text.zig");
const slide_supplied = @import("slide_supplied.zig");
const slide_subject = @import("slide_subject.zig");
const slide_take = @import("slide_take.zig");
const wording = @import("slide_words.zig");
const Server = @import("server.zig").Server;
const Allocator = std.mem.Allocator;

/// Bytes as tokens; a conversation renders as "Q:" and its last user message, then "\nA:".
const ByteText = struct {
fn text(t: *@This()) model_text.Text {
return .{ .ctx = t, .vtable = &.{ .encode = encode, .decode = decode, .token_id = tokenId, .token_string = tokenString, .vocab_size = vocabSize, .eos_ids = eosIds, .render = render, .template_source = templateSource } };
}

fn encode(_: *anyopaque, a: Allocator, input: []const u8, _: bool) model_text.Error![]u32 {
const ids = try a.alloc(u32, input.len);
for (input, ids) |byte, *id| id.* = byte;
return ids;
}

fn decode(_: *anyopaque, a: Allocator, ids: []const u32) Allocator.Error![]u8 {
var out: std.ArrayList(u8) = .empty;
for (ids) |id| if (id < 256) try out.append(a, @intCast(id));
return out.toOwnedSlice(a);
}

fn tokenId(_: *anyopaque, _: []const u8) ?u32 {
return null;
}

fn tokenString(_: *anyopaque, a: Allocator, id: u32) Allocator.Error![]u8 {
return std.fmt.allocPrint(a, "{d}", .{id});
}

fn vocabSize(_: *anyopaque) u32 {
return 256;
}

fn eosIds(_: *anyopaque) []const u32 {
return &.{};
}

fn render(_: *anyopaque, a: Allocator, messages: json.Value, _: model_text.RenderOptions, _: *[]const u8) model_text.Error![]u8 {
var last: []const u8 = "";
for (messages.items() orelse &.{}) |m| if (std.mem.eql(u8, m.strField("role") orelse "", "user")) {
last = m.strField("content") orelse "";
};
return std.fmt.allocPrint(a, "Q:{s}\nA:", .{last});
}

fn templateSource(_: *anyopaque) []const u8 {
return "";
}
};

/// An engine that knows the warehouse robot and answers the lesson's own prompts the way a model told the fact would.
const Robot = struct {
asked: u32 = 0,

fn engine(m: *Robot) api.Engine {
return .{ .ctx = m, .vtable = &.{ .info = info, .submit = submit, .cancel = cancel, .status = status, .memory = memory } };
}

fn info(_: *anyopaque) api.Info {
return .{};
}

fn has(text: []const u8, word: []const u8) bool {
return std.mem.indexOf(u8, text, word) != null;
}

fn answer(q: []const u8) []const u8 {
// the judge: "Does it answer this question they ask you: ...? Reply yes or no."
if (has(q, "Reply yes or no.")) return if (has(q, "top speed")) "no" else "yes";
// the short answer: "...\nAnswer in as few words as you can: just the answer."
if (has(q, "Answer in as few words")) return "Pebble";
if (has(q, "called?")) return "Our warehouse robot is called Pebble.";
if (has(q, "do we call")) return "We call it Pebble.";
return "I don't know.";
}

fn submit(ctx: *anyopaque, id: api.Id, request: *const api.Request, sink: api.Sink) api.SubmitError!void {
const m: *Robot = @ptrCast(@alignCast(ctx));
m.asked += 1;
var prompt: [768]u8 = undefined;
const n = @min(prompt.len, request.prompt.len);
for (request.prompt[0..n], prompt[0..n]) |x, *b| b.* = @intCast(x);
const asked = prompt[0..n];
const q = asked[if (std.mem.indexOf(u8, asked, "Q:")) |i| i + 2 else 0 .. std.mem.indexOf(u8, asked, "\nA:") orelse n];
const said = answer(q);
var ids: [64]u32 = undefined;
for (said, ids[0..said.len]) |b, *x| x.* = b;
sink.event(sink.ctx, id, &.{ .prefilled = 0 });
sink.event(sink.ctx, id, &.{ .tokens = ids[0..said.len] });
sink.event(sink.ctx, id, &.{ .finished = .{ .reason = .stop } });
}

fn cancel(_: *anyopaque, _: api.Id) void {}

fn status(_: *anyopaque, out: *api.Status, _: []u32) void {
out.* = .{};
}

fn memory(_: *anyopaque, _: bool) ?api.Memory {
return null;
}
};

const Stays = struct {
pub fn check(_: Stays) bool {
return false;
}
};

const fact = "Our company's warehouse robot is called Pebble.";

/// The questions `take` keeps for the fact from `qs`, in order, its subject worded and told in chat as a lesson would.
fn kept(a: Allocator, qs: []const []const u8) ![]const []const u8 {
var text: ByteText = .{};
var m: Robot = .{};
const srv = try Server.init(std.testing.allocator, std.testing.io, m.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"}, .enable_thinking = false, .use_drafts = false }, null);
defer srv.deinit();
var cx: errors.Cx = .{ .a = a };
const document = !wording.firstPerson(fact); // a chat fact in the first person asks as the user does
const words = try slide_subject.factSubject(a, "warehouse robot", fact, qs, document);
var got: slide_take.Got = .{ .entity = true, .words = words, .named = try slide_subject.named(a, words, fact, qs) };
try slide_take.take(srv, &cx, &got, fact, 0, qs, &.{}, false, document, Stays{});
var out: std.ArrayList([]const u8) = .empty;
for (got.refs.items) |r| try out.append(a, r.question);
return out.items;
}

test "supplied questions that name the subject and the fact answers are kept, the rest dropped by the lesson's own filters" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const supplied = try slide_supplied.read(a, (try json.parse(a,
\\{"text": "Our company's warehouse robot is called Pebble.", "questions": [
\\ "What is our warehouse robot called?", "What do we call the warehouse robot?", "Is our warehouse robot called Pebble?",
\\ "What is the capital of France?", "What is Pebble?", "What is our warehouse robot's top speed?"]}
)).ok);
try std.testing.expectEqual(@as(usize, 6), supplied.len);
const got = try kept(a, try slide_supplied.ahead(a, supplied, &.{}));
// kept: the two the user asks that the fact answers; the rest are dropped by the lesson's own filters
try std.testing.expectEqual(@as(usize, 2), got.len);
try std.testing.expectEqualStrings("What is our warehouse robot called?", got[0]);
try std.testing.expectEqualStrings("What do we call the warehouse robot?", got[1]);
}

test "a supplied question goes first, and the model's own questions are judged beside it as before" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const written = [_][]const u8{ "What is the capital of France?", "What do we call the warehouse robot?" };
const got = try kept(a, try slide_supplied.ahead(a, &.{"What is our warehouse robot called?"}, &written));
try std.testing.expectEqual(@as(usize, 2), got.len);
try std.testing.expectEqualStrings("What is our warehouse robot called?", got[0]);
try std.testing.expectEqualStrings("What do we call the warehouse robot?", got[1]);
}

test "without supplied questions the lesson keeps exactly what it kept from the model's list alone" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
const written = [_][]const u8{ "What do we call the warehouse robot?", "What is the capital of France?", "What is our warehouse robot called?" };
const alone = try kept(a, &written);
const with_none = try kept(a, try slide_supplied.ahead(a, &.{}, &written));
try std.testing.expectEqual(alone.len, with_none.len);
for (alone, with_none) |x, y| try std.testing.expectEqualStrings(x, y);
try std.testing.expectEqual(@as(usize, 2), alone.len);
}
Loading
Loading