WangYeruo (v0.1.4401) has a Google AI Studio key's provider, the google
preset. magpie's System One asked for google/gemini-decision-1 fell to
OpenRouter's decision list, because the google provider had no decision
API, and the user's own attempt got Google's 400 "type parameter is
required at input[0]" for an object input.
Google's Decisions API (launched 11-11, aistudio.google.com/docs/
decisions-api, api-reference/decisions/create-decision) serves
gemini-decision-1 at POST generativelanguage.googleapis.com/v1beta/
decisions on any AI Studio key, free tier included, with the key as
x-goog-api-key. It takes System One's questions (noul, choice, score),
an input of text or content blocks, and answers by name with usage
total_input_tokens / total_output_tokens.
- normalize gives a provider asked at the Gemini Developer API (the
google preset's OpenAI-compatible base or a native Gemini base) its
System One at AIStudioGemini (googleDecisions). Not Vertex, not a
signed-in account, not another magpie, and a URL the user gave is kept.
- ViaGoogle: /decisions, signed as Gemini (x-goog-api-key, never Bearer;
DecideProtocol), in AskSystemOne, the list and key checks, and the
gateway's postDecideSent.
- googleAsk: the state goes as input (a string as it is, an object as
its JSON text, never an object, which Google reads as a block), images
as image blocks after it, a choice's null description as "".
- DecideResult: the usage comes back as System One's input_tokens /
output_tokens; prompt_feedback.block_reason with no answers is a 400
that says why. Every other way of asking is unchanged (tested).
- gemini-decision-1: 262,144 context, takes images, so the gateway lets
images through to it as it does for the Clef.
- docs/subsystems/providers-accounts.md says so.
Tests, from Google's documented request and answer bytes, no network:
TestGoogleDecideWire, TestGoogleKeyDecides (with OpenRouter's live list
holding google/gemini-decision-1 seeded: it now routes to google),
TestGoogleSystemOne, TestGoogleDecisionsTakesImages.
Broken by hand, each red: the normalize fill dropped (KeyDecides,
SystemOne, TakesImages red); googleAsk skipped (Wire, KeyDecides,
SystemOne, TakesImages red); signed as Chat/Bearer (KeyDecides,
SystemOne, TakesImages red).
Run at this head:
- gofmt -l internal/: clean. go vet (provider, gateway), GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./... on mote: ok.
- Full suite on mote at 6317fd789 (this change on 7707ba9ad+f4b202030):
go test -count=1 -timeout 45m ./...: 103 packages ok, exit 0, no FAIL.
- Rebased onto bf1b2893d (GUI and docs only since: app.js, two GUI
tests, skill-router.md; nothing this change reads), then at this head
on mote: go test -run 'Google|Decide|Decision|SystemOne|Ark'
./internal/provider ./internal/gateway: ok. The whole internal/provider
package was also ok at 6317fd789 (155s).
- No GUI file changed, so no GUI suite.
Not tried: a real call. No AI Studio key is in magpie's accounts here.
215 lines
6.8 KiB
Go
215 lines
6.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// DecideAsk is a System One request (body) as via takes it: Vercel's
|
|
// TypeSafe API takes it as it is; its evaluation models name the model in
|
|
// a header and ask a noul as a boolean; Workers AI takes the state and
|
|
// questions as the input of a run of the model, and a Clef there takes
|
|
// the request as it is, its model named as Clef names itself; Google's
|
|
// Decisions API takes it as googleAsk makes it.
|
|
func DecideAsk(via, model string, body []byte) []byte {
|
|
var q map[string]any
|
|
if via == ViaSystemOne || via == ViaVercel || json.Unmarshal(body, &q) != nil {
|
|
return body
|
|
}
|
|
if via == ViaGoogle {
|
|
return googleAsk(q, body)
|
|
}
|
|
delete(q, "model")
|
|
switch via {
|
|
case ViaVercelEval:
|
|
qs, _ := q["questions"].(map[string]any)
|
|
for _, v := range qs {
|
|
if x, ok := v.(map[string]any); ok && x["type"] == "noul" {
|
|
x["type"] = "boolean"
|
|
}
|
|
}
|
|
b, _ := json.Marshal(q)
|
|
return b
|
|
case ViaCloudflare:
|
|
if CloudflareClef(model) {
|
|
q["model"] = strings.TrimPrefix(model, "@cf/cloudflare/")
|
|
b, _ := json.Marshal(q)
|
|
return b
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"model": model, "input": q})
|
|
return b
|
|
}
|
|
return body
|
|
}
|
|
|
|
// DecideAnswer is a gateway's answer as System One gives it: out of
|
|
// Cloudflare's envelope, or, from Vercel's evaluation models (its TypeSafe
|
|
// API answers as System One does), a boolean's probability as a
|
|
// noul, a confidence from the probabilities where there is none, and its
|
|
// usage named as System One names it.
|
|
func DecideAnswer(via string, b []byte) []byte {
|
|
switch via {
|
|
case ViaCloudflare:
|
|
var env struct {
|
|
Result json.RawMessage `json:"result"`
|
|
}
|
|
if json.Unmarshal(b, &env) == nil && len(env.Result) > 0 && env.Result[0] == '{' {
|
|
return env.Result
|
|
}
|
|
case ViaGoogle:
|
|
// Google's usage is total_input_tokens and total_output_tokens
|
|
// (always 0: "Only input tokens are billed"); its answers are
|
|
// System One's, noul, choice and score alike
|
|
var g struct {
|
|
Model string `json:"model"`
|
|
Answers json.RawMessage `json:"answers"`
|
|
Usage struct {
|
|
Input int `json:"total_input_tokens"`
|
|
Output int `json:"total_output_tokens"`
|
|
} `json:"usage"`
|
|
}
|
|
if json.Unmarshal(b, &g) != nil || len(g.Answers) == 0 || g.Answers[0] != '{' {
|
|
return b
|
|
}
|
|
out := map[string]any{"answers": g.Answers,
|
|
"usage": map[string]int{"input_tokens": g.Usage.Input, "output_tokens": g.Usage.Output}}
|
|
if g.Model != "" {
|
|
out["model"] = g.Model
|
|
}
|
|
b, _ := json.Marshal(out)
|
|
return b
|
|
case ViaVercelEval:
|
|
var v struct {
|
|
Model string `json:"model"`
|
|
Answers map[string]map[string]any `json:"answers"`
|
|
Usage struct {
|
|
Input int `json:"inputTokens"`
|
|
Output int `json:"outputTokens"`
|
|
} `json:"usage"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil || v.Answers == nil {
|
|
return b
|
|
}
|
|
for _, a := range v.Answers {
|
|
if a["type"] == "boolean" {
|
|
a["type"], a["noul"] = "noul", a["probability"]
|
|
continue
|
|
}
|
|
if _, ok := a["confidence"]; !ok {
|
|
if ps, ok := a["probabilities"].(map[string]any); ok {
|
|
a["confidence"] = Confidence(ps)
|
|
}
|
|
}
|
|
}
|
|
out, _ := json.Marshal(map[string]any{"model": v.Model, "answers": v.Answers,
|
|
"usage": map[string]int{"input_tokens": v.Usage.Input, "output_tokens": v.Usage.Output}})
|
|
return out
|
|
}
|
|
return b
|
|
}
|
|
|
|
// googleAsk is a System One request (q, as body) as Google's Decisions
|
|
// API takes it (aistudio.google.com/docs/decisions-api): its state is the
|
|
// input, text as it is and an object or list as its JSON text ("Serialize
|
|
// JSON objects and arrays to a string and pass them as text"), where an
|
|
// object would be read as a content block and refused (400 "type
|
|
// parameter is required at input[0]"); its images, data: URLs, are image
|
|
// blocks after it, and any other URL a block at that uri. Its questions
|
|
// are System One's, noul, choice and score, but a choice's option with no
|
|
// description (null, as magpie's "none of these" goes) has an empty one,
|
|
// which Google takes. Everything else goes as it is, for Google to refuse
|
|
// what it has no place for with a 400 naming it.
|
|
func googleAsk(q map[string]any, body []byte) []byte {
|
|
state, ok := q["state"]
|
|
if !ok {
|
|
return body
|
|
}
|
|
delete(q, "state")
|
|
text, isText := state.(string)
|
|
if !isText {
|
|
b, _ := json.Marshal(state)
|
|
text = string(b)
|
|
}
|
|
var input any = text
|
|
if ims, ok := q["images"].([]any); ok {
|
|
delete(q, "images")
|
|
blocks := []any{map[string]any{"type": "text", "text": text}}
|
|
for _, im := range ims {
|
|
u, _ := im.(string)
|
|
if u == "" {
|
|
continue
|
|
}
|
|
block := map[string]any{"type": "image", "uri": u}
|
|
if meta, data, ok := strings.Cut(strings.TrimPrefix(u, "data:"), ","); ok && strings.HasPrefix(u, "data:") && strings.HasSuffix(meta, ";base64") {
|
|
block = map[string]any{"type": "image", "data": data, "mime_type": strings.TrimSuffix(meta, ";base64")}
|
|
}
|
|
blocks = append(blocks, block)
|
|
}
|
|
if len(blocks) > 1 {
|
|
input = blocks
|
|
}
|
|
}
|
|
q["input"] = input
|
|
if qs, ok := q["questions"].(map[string]any); ok {
|
|
for _, v := range qs {
|
|
x, _ := v.(map[string]any)
|
|
if c, ok := x["criteria"].(map[string]any); ok && x["type"] == "choice" {
|
|
for k, d := range c {
|
|
if d == nil {
|
|
c[k] = ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
b, _ := json.Marshal(q)
|
|
return b
|
|
}
|
|
|
|
// DecideResult is a decision API's answer, status and body, as System
|
|
// One gives it: DecideAnswer's translation of one that answered, and,
|
|
// from Google, an input its filters blocked (prompt_feedback.block_reason,
|
|
// "no decisions were evaluated, and answers is empty") as the 400 Google
|
|
// answers a blocked input with otherwise, its reason in the message.
|
|
func DecideResult(via string, status int, b []byte) (int, []byte) {
|
|
if status >= 300 {
|
|
return status, b
|
|
}
|
|
if via == ViaGoogle {
|
|
var g struct {
|
|
Answers map[string]json.RawMessage `json:"answers"`
|
|
Feedback struct {
|
|
Reason string `json:"block_reason"`
|
|
Message string `json:"block_reason_message"`
|
|
} `json:"prompt_feedback"`
|
|
}
|
|
if json.Unmarshal(b, &g) == nil && g.Feedback.Reason != "" && len(g.Answers) == 0 {
|
|
msg := "Google blocked the input (" + g.Feedback.Reason + ")"
|
|
if g.Feedback.Message != "" {
|
|
msg += ": " + g.Feedback.Message
|
|
}
|
|
out, _ := json.Marshal(map[string]any{"error": map[string]any{"code": g.Feedback.Reason, "message": msg}})
|
|
return http.StatusBadRequest, out
|
|
}
|
|
}
|
|
return status, DecideAnswer(via, b)
|
|
}
|
|
|
|
// Confidence is how sure a set of probabilities is of its likeliest, as
|
|
// System One gives it: 0 with every option as likely, 1 with one certain
|
|
// (its 0.87 of three options is 0.8).
|
|
func Confidence(ps map[string]any) float64 {
|
|
top, n := 0.0, 0
|
|
for _, v := range ps {
|
|
if f, ok := v.(float64); ok {
|
|
top, n = math.Max(top, f), n+1
|
|
}
|
|
}
|
|
if n < 2 {
|
|
return 1
|
|
}
|
|
return max(0, (top-1/float64(n))/(1-1/float64(n)))
|
|
}
|