Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
feat(go): schema window fields for predict_long
Adeshina Lasisi30e2744a52e2
diff --git a/laya-go/schema/answer.go b/laya-go/schema/answer.goindex a2ff2570..4b2c77ce 100644--- a/laya-go/schema/answer.go+++ b/laya-go/schema/answer.go@@ -41,6 +41,22 @@ type Common struct {AnswerConfidence float64// Action is the head's second output.Action Action+ // Window names the window that decided the answer in a PredictLong scan of several windows;+ // nil everywhere else, where laya writes no "window" key.+ Window *Window+}++// Window is where in the tokenized state the deciding window sat. The probabilities of an+// answer that carries one are that window's, not a calibrated number for the whole state.+type Window struct {+ // Index counts the windows from 0.+ Index int `json:"index"`+ // TokenStart is the first state token of the window.+ TokenStart int `json:"token_start"`+ // TokenEnd is one past its last, clamped to the end of the state.+ TokenEnd int `json:"token_end"`+ // Count is how many windows the scan ran.+ Count int `json:"count"`}// Base returns c, so that every answer type satisfies Answer through the embedded Common.@@ -213,6 +229,10 @@ func finish(b *strings.Builder, c Common) ([]byte, error) {}b.WriteString(`,"action":`)b.Write(act)+ if w := c.Window; w != nil {+ fmt.Fprintf(b, `,"window":{"index":%d,"token_start":%d,"token_end":%d,"count":%d}`,+ w.Index, w.TokenStart, w.TokenEnd, w.Count)+ }b.WriteByte('}')return []byte(b.String()), nil}@@ -290,6 +310,13 @@ func answerFrom(v any) (Answer, error) {if common.Action.ActProbability, err = fieldFloat(actObj, "act_probability"); err != nil {return nil, err}+ if raw, ok := optional(obj, "window"); ok {+ w, err := windowFrom(raw)+ if err != nil {+ return nil, err+ }+ common.Window = w+ }switch Type(typ) {case TypeChoice:a := &ChoiceAnswer{Common: common}@@ -348,6 +375,32 @@ func field(obj pyjson.Object, key string) (any, error) {return nil, fmt.Errorf("schema: answer has no %q", key)}+func optional(obj pyjson.Object, key string) (any, bool) {+ for _, m := range obj {+ if m.Key == key {+ return m.Value, true+ }+ }+ return nil, false+}++func windowFrom(v any) (*Window, error) {+ obj, ok := v.(pyjson.Object)+ if !ok {+ return nil, errors.New(`schema: answer "window" must be an object`)+ }+ w := &Window{}+ for key, dst := range map[string]*int{"index": &w.Index, "token_start": &w.TokenStart,+ "token_end": &w.TokenEnd, "count": &w.Count} {+ f, err := fieldFloat(obj, key)+ if err != nil || f != math.Trunc(f) {+ return nil, fmt.Errorf(`schema: answer window %q must be an integer`, key)+ }+ *dst = int(f)+ }+ return w, nil+}+func fieldString(obj pyjson.Object, key string) (string, error) {v, err := field(obj, key)if err != nil {diff --git a/laya-go/schema/prediction.go b/laya-go/schema/prediction.goindex 592b14e8..d2e4621a 100644--- a/laya-go/schema/prediction.go+++ b/laya-go/schema/prediction.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""reflect"+ "strconv""strings""github.com/NandhaKishorM/laya/laya-go/internal/pyjson"@@ -121,14 +122,28 @@ type Usage struct {// CollapsedOptions are the questions whose options no longer have a token span each, keyed// by question id. nil unless the head budget collapsed some; laya omits the key then.CollapsedOptions CollapsedQuestions `json:"options,omitempty"`+ // Windows is how many windows a PredictLong call read: 1 for a state that fit one, 0 on a+ // prediction that came from Predict or PredictBatch, where laya has no such key.+ Windows int `json:"windows,omitempty"`+ // TruncatedWindows is how many windows truncated. It is what a scan of several windows+ // reports as "truncated", a count where a single call has a flag, so it is read only when+ // Windows is above 1; Truncated is then TruncatedWindows > 0.+ TruncatedWindows int `json:"-"`}+// scanned reports whether u summarizes several windows, where laya's "truncated" is a count.+func (u Usage) scanned() bool { return u.Windows > 1 }+// MarshalJSON writes laya's usage dict by hand, so the bytes are Python's: truncated_questions is// always a list, never null, and the ids are not HTML-escaped.func (u Usage) MarshalJSON() ([]byte, error) {var b strings.Builder- fmt.Fprintf(&b, `{"input_tokens":%d,"output_tokens":%d,"state_tokens":%d,"state_tokens_dropped":%d,"truncated":%t,"truncated_questions":[`,- u.InputTokens, u.OutputTokens, u.StateTokens, u.StateTokensDropped, u.Truncated)+ truncated := strconv.FormatBool(u.Truncated)+ if u.scanned() {+ truncated = strconv.Itoa(u.TruncatedWindows)+ }+ fmt.Fprintf(&b, `{"input_tokens":%d,"output_tokens":%d,"state_tokens":%d,"state_tokens_dropped":%d,"truncated":%s,"truncated_questions":[`,+ u.InputTokens, u.OutputTokens, u.StateTokens, u.StateTokensDropped, truncated)for i, id := range u.TruncatedQuestions {if i > 0 {b.WriteByte(',')@@ -146,10 +161,39 @@ func (u Usage) MarshalJSON() ([]byte, error) {b.WriteString(`,"options":`)b.Write(opts)}+ if u.Windows > 0 {+ fmt.Fprintf(&b, `,"windows":%d`, u.Windows)+ }b.WriteByte('}')return []byte(b.String()), nil}+// UnmarshalJSON reads what MarshalJSON writes: "truncated" is a flag, or the window count of a+// scan of several windows.+func (u *Usage) UnmarshalJSON(data []byte) error {+ type plain Usage // without the methods, so this does not recurse+ aux := struct {+ *plain+ Truncated json.RawMessage `json:"truncated"`+ }{plain: (*plain)(u)}+ if err := json.Unmarshal(data, &aux); err != nil {+ return err+ }+ u.Truncated, u.TruncatedWindows = false, 0+ switch raw := strings.TrimSpace(string(aux.Truncated)); raw {+ case "", "null":+ case "true", "false":+ u.Truncated = raw == "true"+ default:+ n, err := strconv.Atoi(raw)+ if err != nil || n < 0 {+ return fmt.Errorf("schema: usage \"truncated\" must be a flag or a window count, got %s", raw)+ }+ u.TruncatedWindows, u.Truncated = n, n > 0+ }+ return nil+}+// MarshalJSON writes laya's predict() result: model, answers, usage.func (p Prediction) MarshalJSON() ([]byte, error) {model, err := pyjson.Dumps(p.Model)diff --git a/laya-go/schema/schema_test.go b/laya-go/schema/schema_test.goindex f7d2241c..c0a8c7f4 100644--- a/laya-go/schema/schema_test.go+++ b/laya-go/schema/schema_test.go@@ -4,6 +4,7 @@ import ("encoding/json""errors""math"+ "reflect""slices""strings""sync"@@ -587,3 +588,54 @@ func TestConcurrentReads(t *testing.T) {}wg.Wait()}++func TestScanUsageAndWindowJSON(t *testing.T) {+ scan := schema.Usage{InputTokens: 9, StateTokens: 4, Truncated: true, TruncatedWindows: 3,+ TruncatedQuestions: []string{}, Windows: 5}+ out, err := json.Marshal(scan)+ if err != nil {+ t.Fatal(err)+ }+ const want = `{"input_tokens":9,"output_tokens":0,"state_tokens":4,"state_tokens_dropped":0,"truncated":3,"truncated_questions":[],"windows":5}`+ if string(out) != want {+ t.Errorf("scan usage\n got %s\nwant %s", out, want)+ }+ // One window keeps the flag; Python's single call writes a bool and then adds windows.+ one, _ := json.Marshal(schema.Usage{Truncated: true, TruncatedQuestions: []string{"q"}, Windows: 1})+ if !strings.Contains(string(one), `"truncated":true`) || !strings.HasSuffix(string(one), `"windows":1}`) {+ t.Errorf("single-window usage = %s", one)+ }+ var back schema.Usage+ if err := json.Unmarshal(out, &back); err != nil || back.TruncatedWindows != 3 || !back.Truncated || back.Windows != 5 {+ t.Errorf("round trip = %+v, %v", back, err)+ }+ if err := json.Unmarshal(one, &back); err != nil || back.TruncatedWindows != 0 || !back.Truncated || back.Windows != 1 {+ t.Errorf("flag round trip = %+v, %v", back, err)+ }+ for _, bad := range []string{`{"truncated":"yes"}`, `{"truncated":-1}`, `{"truncated":1.5}`} {+ if err := json.Unmarshal([]byte(bad), &back); err == nil {+ t.Errorf("%s accepted", bad)+ }+ }++ a := &schema.NoulAnswer{Noul: 0.5, Common: schema.Common{Confidence: 0.5, AnswerConfidence: 0.5,+ Window: &schema.Window{Index: 1, TokenStart: 380, TokenEnd: 1140, Count: 3}}}+ b, err := json.Marshal(a)+ if err != nil {+ t.Fatal(err)+ }+ const wantAnswer = `{"type":"noul","noul":0.5,"confidence":0.5,"answer_confidence":0.5,"action":{"act_probability":0.0},` ++ `"window":{"index":1,"token_start":380,"token_end":1140,"count":3}}`+ if string(b) != wantAnswer {+ t.Errorf("answer\n got %s\nwant %s", b, wantAnswer)+ }+ got, err := schema.UnmarshalAnswer(b)+ if err != nil || !reflect.DeepEqual(got.Base().Window, a.Window) {+ t.Errorf("window round trip = %+v, %v", got, err)+ }+ for _, bad := range []string{`"window":3`, `"window":{"index":1.5,"token_start":0,"token_end":1,"count":1}`, `"window":{"index":1}`} {+ if _, err := schema.UnmarshalAnswer([]byte(strings.Replace(wantAnswer, `"window":{"index":1,"token_start":380,"token_end":1140,"count":3}`, bad, 1))); err == nil {+ t.Errorf("%s accepted", bad)+ }+ }+}