Skip to content

Commit

Permalink
[lambda] make validateJson emit $error
Browse files Browse the repository at this point in the history
  • Loading branch information
seefeldb committed Oct 19, 2023
1 parent a1c5b3a commit b9d099a
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 275 deletions.
5 changes: 2 additions & 3 deletions seeds/coffee-bot-board/docs/schemish-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ input[/"input <br> id='input'"/]:::input -- "epilogue->epilogue" --o epilogue(("
input[/"input <br> id='input'"/]:::input -- "schema->schema" --o schema(("passthrough <br> id='schema'")):::passthrough
input[/"input <br> id='input'"/]:::input -- "recover->allow" --o shouldRecover["runJavascript <br> id='shouldRecover'"]
shouldRecover["runJavascript <br> id='shouldRecover'"] -- "value->value" --> willRecover(("passthrough <br> id='willRecover'")):::passthrough
shouldRecover["runJavascript <br> id='shouldRecover'"] -- "error->error" --> error{{"output <br> id='error'"}}:::output
shouldRecover["runJavascript <br> id='shouldRecover'"] -- "$error->$error" --> error{{"output <br> id='error'"}}:::output
willRecover(("passthrough <br> id='willRecover'")):::passthrough --> prologue(("passthrough <br> id='prologue'")):::passthrough
willRecover(("passthrough <br> id='willRecover'")):::passthrough --> epilogue(("passthrough <br> id='epilogue'")):::passthrough
willRecover(("passthrough <br> id='willRecover'")):::passthrough --> schema(("passthrough <br> id='schema'")):::passthrough
schema(("passthrough <br> id='schema'")):::passthrough -- "schema->schema" --> schemish["schemish <br> id='schemish'"]
schema(("passthrough <br> id='schema'")):::passthrough -- "schema->schema" --> validatejson["validateJson <br> id='validate-json'"]
validatejson["validateJson <br> id='validate-json'"] -- "json->completion" --> completion{{"output <br> id='completion'"}}:::output
validatejson["validateJson <br> id='validate-json'"] -- "error->value" --> shouldRecover["runJavascript <br> id='shouldRecover'"]
validatejson["validateJson <br> id='validate-json'"] -- "$error->value" --> shouldRecover["runJavascript <br> id='shouldRecover'"]
secrets1("secrets <br> id='secrets-1'"):::secrets -- "PALM_KEY->PALM_KEY" --o generator["generateText <br> id='generator'"]
generator["generateText <br> id='generator'"] -- "completion->json" --> validatejson["validateJson <br> id='validate-json'"]
generator["generateText <br> id='generator'"] -- "filters->value" --> shouldRecover["runJavascript <br> id='shouldRecover'"]
Expand All @@ -28,7 +28,6 @@ codeshouldRecover[code]:::config -- "code->code" --o shouldRecover
rawshouldRecover[raw]:::config -- "raw->raw" --o shouldRecover
schemaerror[schema]:::config -- "schema->schema" --o error
schemacompletion[schema]:::config -- "schema->schema" --o completion
messageinput[message]:::config -- "message->message" --o input
schemainput[schema]:::config -- "schema->schema" --o input
stopSequencesgenerator[stopSequences]:::config -- "stopSequences->stopSequences" --o generator
safetySettingsgenerator[safetySettings]:::config -- "safetySettings->safetySettings" --o generator
Expand Down
3 changes: 3 additions & 0 deletions seeds/coffee-bot-board/graphs/menu-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
],
"kits": [
{
"title": "LLM Starter Kit",
"description": "A kit that provides a few necessary components for wiring boards that use PaLM API.",
"version": "0.0.1",
"url": "npm:@google-labs/llm-starter"
}
]
Expand Down
3 changes: 3 additions & 0 deletions seeds/coffee-bot-board/graphs/menu-summary-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
],
"kits": [
{
"title": "LLM Starter Kit",
"description": "A kit that provides a few necessary components for wiring boards that use PaLM API.",
"version": "0.0.1",
"url": "npm:@google-labs/llm-starter"
}
]
Expand Down
17 changes: 11 additions & 6 deletions seeds/coffee-bot-board/graphs/order-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@
{
"from": "shouldRecover",
"to": "error",
"out": "error",
"in": "error"
"out": "$error",
"in": "$error"
},
{
"from": "willRecover",
Expand Down Expand Up @@ -506,7 +506,7 @@
{
"from": "validate-json",
"to": "shouldRecover",
"out": "error",
"out": "$error",
"in": "value"
},
{
Expand Down Expand Up @@ -571,7 +571,7 @@
"type": "runJavascript",
"configuration": {
"name": "gate",
"code": "function gate({ allow, value }) {\n if (allow)\n return { value };\n return { error: value };\n}",
"code": "function gate({ allow, value }) {\n if (allow)\n return { value };\n return { $error: value };\n}",
"raw": true
}
},
Expand All @@ -586,7 +586,7 @@
"schema": {
"type": "object",
"properties": {
"error": {
"$error": {
"type": "object",
"title": "Error",
"description": "The error reported during generation"
Expand Down Expand Up @@ -615,7 +615,6 @@
"id": "input",
"type": "input",
"configuration": {
"message": "Input",
"schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -694,6 +693,9 @@
],
"kits": [
{
"title": "LLM Starter Kit",
"description": "A kit that provides a few necessary components for wiring boards that use PaLM API.",
"version": "0.0.1",
"url": "npm:@google-labs/llm-starter"
},
{
Expand All @@ -717,6 +719,9 @@
],
"kits": [
{
"title": "LLM Starter Kit",
"description": "A kit that provides a few necessary components for wiring boards that use PaLM API.",
"version": "0.0.1",
"url": "npm:@google-labs/llm-starter"
}
]
Expand Down
14 changes: 8 additions & 6 deletions seeds/coffee-bot-board/graphs/schemish-generator.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
{
"from": "shouldRecover",
"to": "error",
"out": "error",
"in": "error"
"out": "$error",
"in": "$error"
},
{
"from": "willRecover",
Expand Down Expand Up @@ -76,7 +76,7 @@
{
"from": "validate-json",
"to": "shouldRecover",
"out": "error",
"out": "$error",
"in": "value"
},
{
Expand Down Expand Up @@ -141,7 +141,7 @@
"type": "runJavascript",
"configuration": {
"name": "gate",
"code": "function gate({ allow, value }) {\n if (allow)\n return { value };\n return { error: value };\n}",
"code": "function gate({ allow, value }) {\n if (allow)\n return { value };\n return { $error: value };\n}",
"raw": true
}
},
Expand All @@ -156,7 +156,7 @@
"schema": {
"type": "object",
"properties": {
"error": {
"$error": {
"type": "object",
"title": "Error",
"description": "The error reported during generation"
Expand Down Expand Up @@ -185,7 +185,6 @@
"id": "input",
"type": "input",
"configuration": {
"message": "Input",
"schema": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -264,6 +263,9 @@
],
"kits": [
{
"title": "LLM Starter Kit",
"description": "A kit that provides a few necessary components for wiring boards that use PaLM API.",
"version": "0.0.1",
"url": "npm:@google-labs/llm-starter"
},
{
Expand Down
4 changes: 2 additions & 2 deletions seeds/coffee-bot-board/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const run = async (board: Board, slots?: BreadboardSlotSpec) => {
};

const show = (id: string, outputs: Record<string, NodeValue>) => {
const { bot, error } = outputs;
if (error) log.error(stringify(error));
const { bot, $error } = outputs;
if ($error) log.error(stringify($error));
else log.success(`${id}: ${stringify(bot)}`);
};

Expand Down
2 changes: 1 addition & 1 deletion seeds/coffee-bot-board/src/menu-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ board.input().wire(
.generateText({
stopSequences: ["Customer:"],
})
.wire("filters->error", board.output({ $id: "error" }))
.wire("$error->", board.output({ $id: "error" }))
.wire("<-PALM_KEY", kit.secrets(["PALM_KEY"]))
.wire(
"completion->",
Expand Down
6 changes: 3 additions & 3 deletions seeds/coffee-bot-board/src/order-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const checkMenuTool = board.passthrough().wire(
.slot("checkMenu")
.wire("bot->Tool", toolMemory)
.wire("bot->", board.output({ $id: "checkMenu-tool-output" }))
.wire("error->", board.output({ $id: "error" }))
.wire("$error->", board.output({ $id: "error" }))
)
);

Expand All @@ -64,7 +64,7 @@ const summarizeMenuTool = board.passthrough().wire(
.slot("summarizeMenu")
.wire("bot->Tool", toolMemory)
.wire("bot->", board.output({ $id: "summarizeMenu-tool-output" }))
.wire("error->", board.output({ $id: "error" }))
.wire("$error->", board.output({ $id: "error" }))
)
);

Expand Down Expand Up @@ -115,6 +115,6 @@ board
.wire("<-recover.", board.passthrough({ recover: true }))
.wire("completion->", toolRouter)
.wire("completion->Agent", agentMemory)
.wire("error->", board.output({ $id: "error" }));
.wire("$error->", board.output({ $id: "error" }));

export const orderAgent = board;
8 changes: 4 additions & 4 deletions seeds/coffee-bot-board/src/schemish-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const schema = board.passthrough({ $id: "schema" });

function gate({ allow, value }: { allow: boolean; value: NodeValue }) {
if (allow) return { value };
return { error: value };
return { $error: value };
}

const shouldRecover = kit.runJavascript("gate", {
Expand All @@ -47,7 +47,7 @@ const $error = board.output({
schema: {
type: "object",
properties: {
error: {
$error: {
type: "object",
title: "Error",
description: "The error reported during generation",
Expand Down Expand Up @@ -110,7 +110,7 @@ board
.wire("schema->.", schema)
.wire("recover->allow.", shouldRecover);

shouldRecover.wire("value->", willRecover).wire("error->", $error);
shouldRecover.wire("value->", willRecover).wire("$error->", $error);

willRecover.wire("->", prologue).wire("->", epilogue).wire("->", schema);

Expand All @@ -122,7 +122,7 @@ const validateJson = nursery
.validateJson({ $id: "validate-json" })
.wire("<-schema", schema)
.wire("json->completion", $completion)
.wire("error->value", shouldRecover);
.wire("$error->value", shouldRecover);

const generator = kit
.generateText({
Expand Down
18 changes: 12 additions & 6 deletions seeds/coffee-bot-board/tests/schemish-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ test("schemish-generator with unparseable JSON", async (t) => {

t.is(count, 1);
t.like(outputs, {
error: {
message: "Expected property name or '}' in JSON at position 2",
type: "parsing",
$error: {
kind: "error",
error: {
message: "Expected property name or '}' in JSON at position 2",
type: "parsing",
},
},
});
});
Expand Down Expand Up @@ -218,9 +221,12 @@ test("schemish-generator with invalid JSON", async (t) => {

t.is(count, 1);
t.like(outputs, {
error: {
message: "0: instance.type is not one of enum values: drink,food\n",
type: "validation",
$error: {
kind: "error",
error: {
message: "0: instance.type is not one of enum values: drink,food\n",
type: "validation",
},
},
});
});
Expand Down
41 changes: 19 additions & 22 deletions seeds/graph-playground/docs/graphs/relationship-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,33 @@
```mermaid
%%{init: 'themeVariables': { 'fontFamily': 'Fira Code, monospace' }}%%
graph TD;
lambda1["lambda <br> id='lambda-1'"] -- "board->lambda" --> invoke3["invoke <br> id='invoke-3'"]
subgraph sg_lambda1 [lambda-1]
lambda1_secrets3("secrets <br> id='secrets-3'"):::secrets -- "PALM_KEY->PALM_KEY" --o lambda1_generateText4["generateText <br> id='generateText-4'"]
lambda1_input1[/"input <br> id='input-1'"/]:::input -- "text->text" --> lambda1_generateText4["generateText <br> id='generateText-4'"]
lambda1_generateText4["generateText <br> id='generateText-4'"] -- "completion->json" --> lambda1_validateJson5["validateJson <br> id='validateJson-5'"]
lambda1_runJavascript6["runJavascript <br> id='runJavascript-6'"] -- "json->completion" --> lambda1_output2{{"output <br> id='output-2'"}}:::output
lambda1_validateJson5["validateJson <br> id='validateJson-5'"] -- "json->json" --> lambda1_runJavascript6["runJavascript <br> id='runJavascript-6'"]
lambda1_input1[/"input <br> id='input-1'"/]:::input -- "text->text" --> lambda1_runJavascript6["runJavascript <br> id='runJavascript-6'"]
lambda1_generateText4["generateText <br> id='generateText-4'"] -- "completion->completion" --> lambda1_jsonata7["jsonata <br> id='jsonata-7'"]
lambda1_input1[/"input <br> id='input-1'"/]:::input -- "schema->schema" --> lambda1_validateJson5["validateJson <br> id='validateJson-5'"]
lambda1_validateJson5["validateJson <br> id='validateJson-5'"] -- "error->error" --> lambda1_jsonata7["jsonata <br> id='jsonata-7'"]
lambda1_runJavascript6["runJavascript <br> id='runJavascript-6'"] -- "error->error" --> lambda1_jsonata7["jsonata <br> id='jsonata-7'"]
lambda3["lambda <br> id='lambda-3'"] -- "board->lambda" --> invoke2["invoke <br> id='invoke-2'"]
subgraph sg_lambda3 [lambda-3]
lambda3_secrets3("secrets <br> id='secrets-3'"):::secrets -- "PALM_KEY->PALM_KEY" --o lambda3_generateText4["generateText <br> id='generateText-4'"]
lambda3_input1[/"input <br> id='input-1'"/]:::input -- "text->text" --> lambda3_generateText4["generateText <br> id='generateText-4'"]
lambda3_generateText4["generateText <br> id='generateText-4'"] -- "completion->json" --> lambda3_validateJson5["validateJson <br> id='validateJson-5'"]
lambda3_input1[/"input <br> id='input-1'"/]:::input -- "schema->schema" --> lambda3_validateJson5["validateJson <br> id='validateJson-5'"]
lambda3_validateJson5["validateJson <br> id='validateJson-5'"] -- "json->json" --> lambda3_runJavascript6["runJavascript <br> id='runJavascript-6'"]
lambda3_input1[/"input <br> id='input-1'"/]:::input -- "text->text" --> lambda3_runJavascript6["runJavascript <br> id='runJavascript-6'"]
lambda3_runJavascript6["runJavascript <br> id='runJavascript-6'"] -- "json->completion" --> lambda3_output2{{"output <br> id='output-2'"}}:::output
lambda3_generateText4["generateText <br> id='generateText-4'"] -- "completion->completion" --> lambda3_validateJson5["validateJson <br> id='validateJson-5'"]
lambda3_generateText4["generateText <br> id='generateText-4'"] -- "completion->completion" --> lambda3_runJavascript6["runJavascript <br> id='runJavascript-6'"]
end
sg_lambda1:::slotted -- "lamdba->lamdba" --o lambda1
invoke3["invoke <br> id='invoke-3'"] -- "completion->text" --> analysis{{"output <br> id='analysis'"}}:::output
promptTemplate2["promptTemplate <br> id='promptTemplate-2'"] -- "prompt->text" --> invoke3["invoke <br> id='invoke-3'"]
scene[/"input <br> id='scene'"/]:::input -- "text->scene" --> promptTemplate2["promptTemplate <br> id='promptTemplate-2'"]
sg_lambda3:::slotted -- "lamdba->lamdba" --o lambda3
invoke2["invoke <br> id='invoke-2'"] -- "completion->text" --> analysis{{"output <br> id='analysis'"}}:::output
promptTemplate1["promptTemplate <br> id='promptTemplate-1'"] -- "prompt->text" --> invoke2["invoke <br> id='invoke-2'"]
scene[/"input <br> id='scene'"/]:::input -- "text->scene" --> promptTemplate1["promptTemplate <br> id='promptTemplate-1'"]
schemascene[schema]:::config -- "schema->schema" --o scene
templatepromptTemplate2[template]:::config -- "template->template" --o promptTemplate2
schemapromptTemplate2[schema]:::config -- "schema->schema" --o promptTemplate2
pathinvoke3[path]:::config -- "path->path" --o invoke3
templatepromptTemplate1[template]:::config -- "template->template" --o promptTemplate1
schemapromptTemplate1[schema]:::config -- "schema->schema" --o promptTemplate1
pathinvoke2[path]:::config -- "path->path" --o invoke2
schemaanalysis[schema]:::config -- "schema->schema" --o analysis
classDef default stroke:#ffab40,fill:#fff2ccff,color:#000
classDef input stroke:#3c78d8,fill:#c9daf8ff,color:#000
Expand Down
Loading

0 comments on commit b9d099a

Please sign in to comment.